aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/admin/tabs/files.js
blob: c3737d0885095bd83a5ab23d0928f9e3cdd17090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
jQuery(function($) {
  "use strict";

  var TEMPLATES = new LuigiTemplate.Cache({
    file: [
      "<a ",
        "href='#' ",
        "class='list-group-item' ",
        "data-type='%{type|h}' ",
        "data-name='%{name|h}' ",
        "data-size='%{size|h}' ",
        "data-path='%{path|h}' ",
      ">",
        "<i class='fa fa-file-o'></i>",
        " ",
        "%{name|h}",
        " ",
        "(%{size|h} bytes)",
      "</a>",
    ],

    dir: [
      "<a ",
        "href='#' ",
        "class='list-group-item' ",
        "data-type='%{type|h}' ",
        "data-name='%{name|h}' ",
        "data-size='%{size|h}' ",
        "data-path='%{path|h}' ",
      ">",
        "<i class='fa fa-folder-o'></i>",
        " ",
        "%{name|h}",
      "</a>",
    ],

    no_files: [
      "<span class='list-group-item disabled'>",
        "<i class='fa fa-ban'></i>",
        " ",
        "No files.",
      "</span>",
    ],

    crumb: [
      "<div class='btn-group btn-group-sm'>",
        "<a ",
          "href='#' ",
          "class='btn btn-default btn-sm' ",
          "data-path='%{path|h}' ",
        ">",
          "%{name|h}",
        "</a>",
      "</div><!-- btn-group -->",
    ],
  });

  function get_crumbs(path) {
    var r = [];

    $.each(path.replace(/^\//, '').split(/\//), function(i, part) {
      if (part.length > 0)
        r.push('/' + r.concat([part]).join('/'));
    })

    // prepend root
    r.unshift('/');

    return r;
  }

  var NO_FILES = TEMPLATES.run('no_files');

  function reload() {
    var btn = $('#files-reload'),
        list = $('#files');

    // show loading
    btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
    // list.html(TEMPLATES.run('loading'));

    send('file/list', {
      path: $('#files').data('path')
    }).always(function() {
      btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
      // list.html('');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        var data = $.parseJSON(r.responseText);
        if (data.error)
          error = data.error;
      } catch (e) {}

      alert("Error: " + error);
    }).done(function(r) {
      // disable action buttons
      $('#file-actions li').addClass('disabled');

      // draw crumbs
      var crumbs = get_crumbs($('#files').data('path'));
      $('#files-crumbs').html($.map(crumbs, function(crumb) {
        return TEMPLATES.run('crumb', {
          path: crumb,
          name: crumb.replace(/^.*\/(.+)/, '$1'),
        });
      }).join(''));

      // draw files
      $('#files').html((r.length > 0) ? $.map(r, function(row) {
        return TEMPLATES.run(row.type, row);
      }).join('') : NO_FILES);

      // update file count
      $('#files-count').text(r.length);
    });

    // stop event
    return false;
  }

  $('#files').on('dblclick', 'a.list-group-item', function() {
    var data = $(this).data();

    if (data.type == 'dir') {
      // load directory
      $('#files').data('path', data.path);
      reload();
    } else {
      // select and download file
      $(this).click();
      $('#files-download').click();
    }

    // stop event
    return false;
  }).on('click', 'a.list-group-item', function() {
    var data = $(this).data();

    // disable file actions
    $('#file-actions li').removeClass('disabled');

    // update highlight
    $('#files .active').removeClass('active');
    $(this).addClass('active');

    // stop event
    return false;
  });

  $('#files-crumbs').on('click', 'a.btn', function() {
    $('#files').data('path', $(this).data('path'));
    reload();

    // stop event
    return false;
  });

  $('#file-actions').on('click', 'a', function() {
    var data = $(this).data();

    // hide dropdown
    $('body').trigger('click');

    if (data.id == 'download') {
      alert('TODO: download file');
    } else if (data.id == 'move') {
      alert('TODO: move file');
    } else if (data.id == 'delete') {
      alert('TODO: delete file');
    }

    // stop event
    return false;
  });

  $('#files-upload').click(function() {
    alert('TODO: upload');

    // stop event
    return false;
  });

  $('#files-mkdir').click(function() {
    var me = $(this),
        path = prompt('Name of new folder:', 'New Folder');

    if (!path || !path.match(/\S/))
      return false;

    if (path.match(/\//)) {
      alert("Error: Folder name cannot contain '/' characters");
      return false;
    }

    // disable button, show spinner
    me.toggleClass('disabled').find('.loading').toggleClass('hidden');

    send('file/mkdir', {
      path: ($('#files').data('path') + '/' +  path).replace(/\/\/+/, '/')
    }).always(function() {
      // enable button, hide spinner
      me.toggleClass('disabled').find('.loading').toggleClass('hidden');
    }).fail(function(r) {
      var error = r.responseText;

      try {
        var data = $.parseJSON(r.responseText);
        if (data.error)
          error = data.error;
      } catch (e) {}

      alert("Error: " + error);
    }).done(function(r) {
      // reload file list
      reload();
    });

    // stop event
    return false;
  });

  $('#files').data('path', '/');
  $('#files-reload').click(reload);

  // load users
  $('#admin-tab-files').on('show.bs.tab', function() {
    reload();
  });
});