From 7b638bce76bb28f222c4c509c3e7a3f84aea3694 Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Thu, 28 Jul 2016 14:47:48 -0400
Subject: add site dropdown to files tab

---
 data/assets/js/admin/tabs/files.js | 116 +++++++++++++++++++++++++++++++++----
 1 file changed, 104 insertions(+), 12 deletions(-)

(limited to 'data/assets/js')

diff --git a/data/assets/js/admin/tabs/files.js b/data/assets/js/admin/tabs/files.js
index 56fb6c9..bfd1943 100644
--- a/data/assets/js/admin/tabs/files.js
+++ b/data/assets/js/admin/tabs/files.js
@@ -49,7 +49,7 @@ jQuery(function($) {
       "<div class='btn-group btn-group-sm'>",
         "<a ",
           "href='#' ",
-          "class='btn btn-default btn-sm' ",
+          "class='crumb btn btn-default btn-sm' ",
           "data-path='%{path|h}' ",
         ">",
           "%{name|h}",
@@ -64,8 +64,62 @@ jQuery(function($) {
     delete_confirm: [
       "Are you sure you want to delete \"%{name|h}\"?",
     ],
+
+    sites: [
+      "<div class='btn-group btn-group-sm'>",
+        "<a ",
+          "href='#' ",
+          "class='btn btn-default' ",
+          "title='Choose site.' ",
+          "data-toggle='dropdown' ",
+        ">",
+          "Site: <span>%{name|h}</span>",
+          " ",
+          "<i class='fa fa-caret-down'></i>",
+        "</a>",
+
+        "<ul class='dropdown-menu'>",
+          "%{body}",
+        "</ul>",
+      "</div><!-- btn-group -->",
+    ],
+
+    site: [
+      "<li class='%{css|h}'>",
+        "<a ",
+          "href='#' ",
+          "title='%{text|h}' ",
+          "data-id='%{site_id|h}' ",
+          "data-name='%{name|h}' ",
+        ">",
+          "%{name|h}",
+        "</a>",
+      "</li>",
+    ],
   });
 
+  function get_selected_site() {
+    var site_id = $('#files').data('site_id');
+
+    return $.grep(DATA.sites, function(row) {
+      return site_id == row.site_id;
+    })[0];
+  }
+
+  function make_site_dropdown() {
+    var curr_site = get_selected_site();
+
+    return TEMPLATES.run('sites', {
+      name: curr_site.name,
+      body: $.map(DATA.sites, function(row) {
+        return TEMPLATES.run('site', $.extend({}, row, {
+          css:  (row.site_id == curr_site.site_id) ? 'active' : '',
+          text: 'Show files for this site.', // FIXME
+        }));
+      }).join(''),
+    });
+  }
+
   function get_crumbs(path) {
     var r = [];
 
@@ -83,17 +137,18 @@ jQuery(function($) {
   var NO_FILES = TEMPLATES.run('no_files');
 
   function reload() {
-    var btn = $('#files-reload'),
+    var btns = $('#files-reload, #files-crumbs .btn'),
         list = $('#files');
 
     // show loading
-    btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
+    btns.toggleClass('disabled').find('.loading').toggleClass('hidden');
     // list.html(TEMPLATES.run('loading'));
 
     send('file/list', {
-      path: $('#files').data('path')
+      site_id:  $('#files').data('site_id'),
+      path:     $('#files').data('path'),
     }).always(function() {
-      btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
+      btns.toggleClass('disabled').find('.loading').toggleClass('hidden');
       // list.html('');
     }).fail(function(r) {
       var error = r.responseText;
@@ -111,12 +166,14 @@ jQuery(function($) {
 
       // draw crumbs
       var crumbs = get_crumbs($('#files').data('path'));
-      $('#files-crumbs').html($.map(crumbs, function(crumb) {
+      $('#files-crumbs').html([
+        make_site_dropdown()
+      ].concat($.map(crumbs, function(crumb) {
         return TEMPLATES.run('crumb', {
           path: crumb,
           name: crumb.replace(/^.*\/(.+)/, '$1'),
         });
-      }).join(''));
+      })).join(''));
 
       // draw files
       $('#files').html((r.length > 0) ? $.map(r, function(row) {
@@ -160,7 +217,7 @@ jQuery(function($) {
     return false;
   });
 
-  $('#files-crumbs').on('click', 'a.btn', function() {
+  $('#files-crumbs').on('click', 'a.crumb', function() {
     $('#files').data('path', $(this).data('path'));
     reload();
 
@@ -168,6 +225,33 @@ jQuery(function($) {
     return false;
   });
 
+  $('#files-crumbs').on('click', 'ul.dropdown-menu a', function() {
+    var data = $(this).data(),
+        ul = $(this).parents('ul');
+
+    // hide dropdown
+    $('body').trigger('click');
+
+    // refresh selection
+    ul.find('li.active').removeClass('active');
+    $(this).parent('li').addClass('active');
+
+    // update button text
+    ul.prev('a').find('span').text(data.name);
+
+    // save site id and path
+    $('#files').data({
+      site_id:  data.id,
+      path:     '/',
+    });
+
+    // reload file list
+    reload();
+
+    // stop event
+    return false;
+  });
+
   $('#file-actions').on('click', 'a', function() {
     var action_id = $(this).data('id'),
         data = $('#files .active').data();
@@ -178,11 +262,12 @@ jQuery(function($) {
     if (data) {
       if (action_id == 'download') {
         if (data.url)
-          location.href = url;
+          location.href = data.url;
       } else if (action_id == 'move') {
         var dst_path = prompt(TEMPLATES.run('move_prompt', data), data.path);
         if (dst_path) {
           send('file/move', {
+            site_id:  $('#files').data('site_id'),
             src_path: data.path,
             dst_path: dst_path,
           }).always(function() {
@@ -205,7 +290,8 @@ jQuery(function($) {
       } else if (action_id == 'delete') {
         if (confirm(TEMPLATES.run('delete_confirm', data))) {
           send('file/delete', {
-            path: data.path,
+            site_id:  $('#files').data('site_id'),
+            path:     data.path,
           }).always(function() {
             // TODO: need loading handler
           }).fail(function(r) {
@@ -253,7 +339,8 @@ jQuery(function($) {
     me.toggleClass('disabled').find('.loading').toggleClass('hidden');
 
     send('file/mkdir', {
-      path: ($('#files').data('path') + '/' +  path).replace(/\/\/+/, '/')
+      site_id:  $('#files').data('site_id'),
+      path:     ($('#files').data('path') + '/' +  path).replace(/\/\/+/, '/')
     }).always(function() {
       // enable button, hide spinner
       me.toggleClass('disabled').find('.loading').toggleClass('hidden');
@@ -276,7 +363,12 @@ jQuery(function($) {
     return false;
   });
 
-  $('#files').data('path', '/');
+  // set defaults
+  $('#files').data({
+    path:     '/',
+    site_id:  DATA.default_site_id,
+  });
+
   $('#files-reload').click(reload);
 
   // load users
-- 
cgit v1.2.3