summaryrefslogtreecommitdiff
path: root/src/static/js/admin/plugins.js
blob: 885c028d3765d415a284b4d1adf759f4326136d6 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
$(document).ready(function () {
  
  var socket,
    loc = document.location,
    port = loc.port == "" ? (loc.protocol == "https:" ? 443 : 80) : loc.port,
    url = loc.protocol + "//" + loc.hostname + ":" + port + "/",
    pathComponents = location.pathname.split('/'),
    // Strip admin/plugins
    baseURL = pathComponents.slice(0,pathComponents.length-2).join('/') + '/',
    resource = baseURL.substring(1) + "socket.io";

  //connect
  socket = io.connect(url, {resource : resource}).of("/pluginfw/installer");

  function search(searchTerm, limit) {
    if(search.searchTerm != searchTerm) {
      search.offset = 0
      search.results = []
      search.end = false
    }
    limit = limit? limit : search.limit
    search.searchTerm = searchTerm;
    socket.emit("search", {searchTerm: searchTerm, offset:search.offset, limit: limit, sortBy: search.sortBy, sortDir: search.sortDir});
    search.offset += limit;
    
    $('#search-progress').show()
    search.messages.show('fetching')
    storeScrollPosition()
    search.searching = true
  }
  search.searching = false;
  search.offset = 0;
  search.limit = 25;
  search.results = [];
  search.sortBy = 'name';
  search.sortDir = /*DESC?*/true;
  search.end = true;// have we received all results already?
  search.messages = {
    show: function(msg) {
      //$('.search-results .messages').show()
      $('.search-results .messages .'+msg+'').show()
      $('.search-results .messages .'+msg+' *').show()
    },
    hide: function(msg) {
      //$('.search-results .messages').hide()
      $('.search-results .messages .'+msg+'').hide()
      $('.search-results .messages .'+msg+' *').hide()
    }
  }

  var installed = {
    progress: {
      show: function(plugin, msg) {
        $('.installed-results .'+plugin+' .progress').show()
        $('.installed-results .'+plugin+' .progress .message').text(msg)
        if($(window).scrollTop() > $('.'+plugin).offset().top)$(window).scrollTop($('.'+plugin).offset().top-100)
      },
      hide: function(plugin) {
        $('.installed-results .'+plugin+' .progress').hide()
        $('.installed-results .'+plugin+' .progress .message').text('')
      }
    },
    messages: {
      show: function(msg) {
        $('.installed-results .messages').show()
        $('.installed-results .messages .'+msg+'').show()
      },
      hide: function(msg) {
        $('.installed-results .messages').hide()
        $('.installed-results .messages .'+msg+'').hide()
      }
    },
    list: []
  }

  function displayPluginList(plugins, container, template) {
    plugins.forEach(function(plugin) {
      var row = template.clone();
      
      for (attr in plugin) {
        if(attr == "name"){ // Hack to rewrite URLS into name
          row.find(".name").html("<a target='_blank' title='Plugin details' href='https://npmjs.org/package/"+plugin['name']+"'>"+plugin['name'].substr(3)+"</a>"); // remove 'ep_'
        }else{
          row.find("." + attr).html(plugin[attr]);
        }
      }
      row.find(".version").html( plugin.version );
      row.addClass(plugin.name)
      row.data('plugin', plugin.name)
      container.append(row);
    })
    updateHandlers();
  }
  
  function sortPluginList(plugins, property, /*ASC?*/dir) {
    return plugins.sort(function(a, b) {
      if (a[property] < b[property])
         return dir? -1 : 1;
      if (a[property] > b[property])
         return dir? 1 : -1;
      // a must be equal to b
      return 0;
    })
  }

  // Infinite scroll
  var scrollPosition
  function storeScrollPosition() {
    scrollPosition = $(window).scrollTop()
  }
  function restoreScrollPosition() {
    setTimeout(function() {
      $(window).scrollTop(scrollPosition)
    }, 0)
  }
  
  $(window).scroll(checkInfiniteScroll)
  function checkInfiniteScroll() {
    if(search.end || search.searching) return;// don't keep requesting if there are no more results
    setTimeout(function() {
      try{
        var top = $('.results>tr:last').offset().top
        if($(window).scrollTop()+$(window).height() > top) search(search.searchTerm)
      }catch(e){}
    }, 1)
  }

  function updateHandlers() {
    // Search
    $("#search-query").unbind('keyup').keyup(function () {
      search($("#search-query").val());
    });

    // update & install
    $(".do-install, .do-update").unbind('click').click(function (e) {
      var $row = $(e.target).closest("tr")
        , plugin = $row.data('plugin');
      if($(this).hasClass('do-install')) {
        $row.remove().appendTo('#installed-plugins')
        installed.progress.show(plugin, 'Installing')
      }else{
        installed.progress.show(plugin, 'Updating')
      }
      socket.emit("install", plugin);
      installed.messages.hide("nothing-installed")
    });

    // uninstall
    $(".do-uninstall").unbind('click').click(function (e) {
      var $row = $(e.target).closest("tr")
        , pluginName = $row.data('plugin');
      socket.emit("uninstall", pluginName);
      installed.progress.show(pluginName, 'Uninstalling')
      installed.list = installed.list.filter(function(plugin) {
        return plugin.name != pluginName
      })
    });

    // Sort
    $('.sort.up').unbind('click').click(function() {
      search.sortBy = $(this).text().toLowerCase();
      search.sortDir = false;
      search.offset = 0;
      search(search.searchTerm, search.results.length);
      search.results = [];
    })
    $('.sort.down, .sort.none').unbind('click').click(function() {
      search.sortBy = $(this).text().toLowerCase();
      search.sortDir = true;
      search.offset = 0;
      search(search.searchTerm, search.results.length);
      search.results = [];
    })
  }

  socket.on('results:search', function (data) {
    if(!data.results.length) search.end = true;
    search.messages.hide('nothing-found')
    search.messages.hide('fetching')
    $("#search-query").removeAttr('disabled')
    
    console.log('got search results', data)

    // add to results
    search.results = search.results.concat(data.results);

    // Update sorting head
    $('.sort')
      .removeClass('up down')
      .addClass('none');
    $('.search-results thead th[data-label='+data.query.sortBy+']')
      .removeClass('none')
      .addClass(data.query.sortDir? 'up' : 'down');

    // re-render search results
    var searchWidget = $(".search-results");
    searchWidget.find(".results *").remove();
    if(search.results.length > 0) {
      displayPluginList(search.results, searchWidget.find(".results"), searchWidget.find(".template tr"))
    }else {
      search.messages.show('nothing-found')
    }
    search.messages.hide('fetching')
    $('#search-progress').hide()
    restoreScrollPosition()
    checkInfiniteScroll()
    search.searching = false
  });

  socket.on('results:installed', function (data) {
    installed.messages.hide("fetching")
    installed.messages.hide("nothing-installed")

    installed.list = data.installed
    sortPluginList(installed.list, 'name', /*ASC?*/true);

    // filter out epl
    installed.list = installed.list.filter(function(plugin) {
      return plugin.name != 'ep_etherpad-lite'
    })

    // remove all installed plugins (leave plugins that are still being installed)
    installed.list.forEach(function(plugin) {
      $('#installed-plugins .'+plugin.name).remove()
    })

    if(installed.list.length > 0) {
      displayPluginList(installed.list, $("#installed-plugins"), $("#installed-plugin-template"));
      socket.emit('checkUpdates');
    }else {
      installed.messages.show("nothing-installed")
    }
  });
  
  socket.on('results:updatable', function(data) {
    data.updatable.forEach(function(pluginName) {
      var $row = $('#installed-plugins > tr.'+pluginName)
        , actions = $row.find('.actions')
      actions.append('<input class="do-update" type="button" value="Update" />')
    })
    updateHandlers();
  })

  socket.on('finished:install', function(data) {
    if(data.error) {
      alert('An error occured while installing '+data.plugin+' \n'+data.error)
      $('#installed-plugins .'+data.plugin).remove()
    }

    socket.emit("getInstalled");

    // update search results
    search.offset = 0;
    search(search.searchTerm, search.results.length);
    search.results = [];
  })

  socket.on('finished:uninstall', function(data) {
    if(data.error) alert('An error occured while uninstalling the '+data.plugin+' \n'+data.error)

    // remove plugin from installed list
    $('#installed-plugins .'+data.plugin).remove()
    
    socket.emit("getInstalled");

    // update search results
    search.offset = 0;
    search(search.searchTerm, search.results.length);
    search.results = [];
  })

  // init
  updateHandlers();
  socket.emit("getInstalled");
  search('');

  // check for updates every 5mins
  setInterval(function() {
    socket.emit('checkUpdates');
  }, 1000*60*5)
});