{{extend 'default.mobile/layout.html'}}
{{
def A_info(*a,**b):
    b['_data-role'] = 'button'
    b['_data-icon'] = 'info'
    b['_data-theme'] = 'b'
    return A(*a,**b)

def A_gear(*a,**b):
    b['_data-role'] = 'button'
    b['_data-icon'] = 'gear'
    return A(*a,**b)

def A_delete(*a,**b):
    b['_data-role'] = 'button'
    b['_data-icon'] = 'delete'
    return A(*a,**b)

def A_check(*a,**b):
    b['_data-role'] = 'button'
    b['_data-icon'] = 'check'
    return A(*a,**b)
    
def all(items):
    return reduce(lambda a,b:a and b,items,True)
def peekfile(path,file):
    return A_info(file.replace('\\\\','/'),_href=URL('peek', args=(app, path, file)))
def editfile(path,file):
    return A_gear(SPAN(T('Edit')),_href=URL('edit', args=(app, path, file)))
def testfile(path,file):
    return A_check(SPAN(T('Run tests')), _href=URL('test', args=(app, file)))
def editlanguagefile(path,file):
    return A_gear(SPAN(T('Edit')),_href=URL('edit_language', args=(app, path, file)))
def file_upload_form(location):
    form=FORM(T("upload file:")," ",
              INPUT(_type="file",_name="file")," ",T("and rename it:")," ",
              INPUT(_type="text",_name="filename",requires=IS_NOT_EMPTY()),
              INPUT(_type="hidden",_name="location",_value=location),
              INPUT(_type="hidden",_name="sender",_value=URL('design',args=app)),
              INPUT(_type="submit",_value=T("upload")),_action=URL('upload_file'))
    return form
def file_create_form(location):
    form=FORM(T("create file with filename:")," ",
              INPUT(_type="text",_name="filename",requires=IS_NOT_EMPTY()),
              INPUT(_type="hidden",_name="location",_value=location),
              INPUT(_type="hidden",_name="sender",_value=URL('design',args=app)))
    return form
def upload_plugin_form(app):
    form=FORM(T("upload plugin file:")," ",
              INPUT(_type="file",_name="pluginfile"),
              INPUT(_type="submit",_value=T("upload")))
    return form
def deletefile(arglist):
    return A_delete(SPAN(T('Delete')), _href=URL('delete',args=arglist,vars=dict(sender=request.function+'/'+app)))
}}

{{block sectionclass}}design {{=app}}{{end}}

<div data-role="collapsible-set" data-content-theme="c">

<!-- MODELS -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="models">
  {{=T("Models")}}
</h3>
<div id="models_inner">
  {{if not models:}}<p><strong>{{=T("There are no models")}}</strong></p>{{else:}}
  <div data-role="controlgroup" data-type="horizontal">
    {{=button(URL(a=app,c='appadmin',f='index'), T('database administration'))}}
    {{if os.access(os.path.join(request.folder,'..',app,'databases','sql.log'),os.R_OK):}}
      {{=button(URL('peek/%s/databases/sql.log'%app), 'sql.log')}}
    {{pass}}
  </div>
  {{pass}}
    {{for m in models:}}
    <span data-role="controlgroup">
      {{=peekfile('models',m)}}
      {{=editfile('models',m)}}
      {{=deletefile([app, 'models', m])}}
    </span>
    </br>
    {{pass}}
  <div data-role="fieldcontain">{{=file_create_form('%s/models/' % app)}}</div>
</div>
</div>

<!-- FIND CONTROLLER FUNCTIONS -->
{{
controller_functions=[]
for c in controllers: controller_functions+=[c[:-3]+'/%s.html'%x for x in functions[c]]
}}

<!-- CONTROLLERS -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="controllers">
  {{=T("Controllers")}}
</h3>
<div id="controllers_inner">
  {{if not controllers:}}<p><strong>{{=T("There are no controllers")}}</strong></p>{{else:}}
  <div data-role="controlgroup" data-type="horizontal">
      {{=button(URL('test',args=app), T("test"))}}
      {{=button(URL('edit',args=[app,'cron','crontab']), T("crontab"))}}
  </div>
  {{pass}}
    {{for c in controllers:}}
    <span data-role="controlgroup">
      {{=peekfile('controllers',c)}}
      {{=editfile('controllers',c)}}
      {{=deletefile([app, 'controllers', c])}}
      {{=testfile('controllers',c)}}
      </br>
    </span>
    {{pass}}
  <div data-role="fieldcontain">{{=file_create_form('%s/controllers/' % app)}}</div>
</div>
</div>

<!-- VIEWS -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="views">
  {{=T("Views")}}
</h3>
<div id="views_inner">
  <div data-role="controlgroup" data-type="horizontal">
    {{=button(LAYOUTS_APP, T("download layouts"))}}
  </div>
  {{if not views:}}<p><strong>{{=T("There are no views")}}</strong></p>{{pass}}
    {{for c in views:}}
    <span data-role="controlgroup">
      {{=peekfile('views',c)}}
      {{=editfile('views',c)}}
      {{=deletefile([app, 'views', c])}}
      </br>
    </span>
    {{pass}}
  <div data-role="fieldcontain">{{=file_create_form('%s/views/' % app)}}</div>
</div>
</div>

<!-- LANGUAGES -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="languages">
  {{=T("Languages")}}
</h3>
<div id="languages_inner">
  <div data-role="controlgroup" data-type="horizontal">
    {{=button(URL('update_languages/'+app), T('update all languages'))}}
  </div>
  {{if not languages:}}<p><strong>{{=T("There are no translators")}}</strong></p>{{pass}}
    {{for file in languages:}}
    <span data-role="controlgroup">
      {{=peekfile('languages',file)}}
      {{=editlanguagefile('languages',file)}}
      {{=deletefile([app, 'languages', file])}}
      </br>
    </span>
    {{pass}}
  <div data-role="fieldcontain">{{=file_create_form('%s/languages/' % app)}}</div>
</div>
</div>

<!-- STATIC -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="static">
  {{=T("Static files")}}
  </h3>
<div id="static_inner">
  {{if not statics:}}<p><strong>{{=T("There are no static files")}}</strong></p>{{pass}}
  <ul data-role="listview" data-inset="true">
    {{
    tree={}
    for file in statics+['']:
      items=file.partition('/')
      if not items[0] in tree:
        tree[items[0]] = []
        pass
      tree[items[0]].append(items[2])
      pass
    }}
    {{for k, v in sorted(tree.items()):}}
    {{if k:}}
    <li><a>{{=k}}</a>
        <ul data-theme='c'>
            </br>
            {{if len(v) == 1 and v[0] =='':}}
                <span data-role="controlgroup">
                  {{=peekfile('static', k)}}
                  {{=editfile('static', k)}}
                  {{=deletefile([app, 'static', k])}}
                  </br>
                </span>
                </ul>
                {{continue}}
            {{for f in v:}}
                <span data-role="controlgroup">
                  {{=peekfile('static/'+k, f)}}
                  {{=editfile('static/'+k, f)}}
                  {{=deletefile([app, 'static/'+k, f])}}
                  </br>
                </span>
            {{pass}}
        </ul>
    </li>
    {{pass}}
    {{pass}}
    </ul>
  <div data-role="fieldcontain">{{=file_create_form('%s/static/' % app)}}
  {{#=file_upload_form('%s/static/' % app)}}</div>
</div>
</div>

<!-- MODULES -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="modules">
  {{=T("Modules")}}</h3>
<div id="modules_inner">
  {{if not modules:}}<p><strong>{{=T("There are no modules")}}</strong></p>{{pass}}
    {{for m in modules:}}
    <span data-role="controlgroup">
      {{=peekfile('modules',m)}}
      {{=editfile('modules',m)}}
      {{if m!='__init__.py':}}{{=deletefile([app, 'modules', m])}}{{pass}}
    </span>
    {{pass}}
  <div data-role="fieldcontain">{{=file_create_form('%s/modules/' % app)}}
  {{#=file_upload_form('%s/modules/' % app)}}</div>
</div>
</div>

<!-- PLUGINS -->
<div data-role="collapsible" data-content-theme="c">
<h3 id="plugins" >
  {{=T("Plugins")}}
</h3>
<div id="plugins_inner">
  <div data-role="controlgroup" data-type="horizontal">
    {{=button(PLUGINS_APP, T('download plugins'))}}
  </div>
  {{if plugins:}}
      <div data-role="controlgroup">
      {{for plugin in plugins:}}
      {{=button(URL('plugin', args=[app, plugin]), 'plugin_%s' % plugin)}}
      {{pass}}
      </div>
  {{else:}}
  <p><strong>{{=T('There are no plugins')}}</strong></p>
  {{pass}}
  <div data-role="fieldcontain">{{=upload_plugin_form(app)}}</div>
</div>
</div>

</div>

