You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apps.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /* global Handlebars */
  2. Handlebars.registerHelper('score', function() {
  3. if(this.score) {
  4. var score = Math.round( this.score * 10 );
  5. var imageName = 'rating/s' + score + '.svg';
  6. return new Handlebars.SafeString('<img src="' + OC.imagePath('core', imageName) + '">');
  7. }
  8. return new Handlebars.SafeString('');
  9. });
  10. Handlebars.registerHelper('level', function() {
  11. if(typeof this.level !== 'undefined') {
  12. if(this.level === 200) {
  13. return new Handlebars.SafeString('<span class="official icon-checkmark">' + t('settings', 'Official') + '</span>');
  14. }
  15. }
  16. });
  17. OC.Settings = OC.Settings || {};
  18. OC.Settings.Apps = OC.Settings.Apps || {
  19. setupGroupsSelect: function($elements) {
  20. OC.Settings.setupGroupsSelect($elements, {
  21. placeholder: t('core', 'All')
  22. });
  23. },
  24. State: {
  25. currentCategory: null,
  26. apps: null,
  27. $updateNotification: null,
  28. availableUpdates: 0
  29. },
  30. loadCategories: function() {
  31. if (this._loadCategoriesCall) {
  32. this._loadCategoriesCall.abort();
  33. }
  34. var categories = [
  35. {displayName: t('settings', 'Enabled'), ident: 'enabled', id: '0'},
  36. {displayName: t('settings', 'Not enabled'), ident: 'disabled', id: '1'}
  37. ];
  38. var source = $("#categories-template").html();
  39. var template = Handlebars.compile(source);
  40. var html = template(categories);
  41. $('#apps-categories').html(html);
  42. OC.Settings.Apps.loadCategory($('#app-navigation').attr('data-category'));
  43. this._loadCategoriesCall = $.ajax(OC.generateUrl('settings/apps/categories'), {
  44. data:{},
  45. type:'GET',
  46. success:function (jsondata) {
  47. var html = template(jsondata);
  48. $('#apps-categories').html(html);
  49. $('#app-category-' + OC.Settings.Apps.State.currentCategory).addClass('active');
  50. },
  51. complete: function() {
  52. $('#app-navigation').removeClass('icon-loading');
  53. }
  54. });
  55. },
  56. loadCategory: function(categoryId) {
  57. if (OC.Settings.Apps.State.currentCategory === categoryId) {
  58. return;
  59. }
  60. if (this._loadCategoryCall) {
  61. this._loadCategoryCall.abort();
  62. }
  63. $('#apps-list')
  64. .addClass('icon-loading')
  65. .removeClass('hidden')
  66. .html('');
  67. $('#apps-list-empty').addClass('hidden');
  68. $('#app-category-' + OC.Settings.Apps.State.currentCategory).removeClass('active');
  69. $('#app-category-' + categoryId).addClass('active');
  70. OC.Settings.Apps.State.currentCategory = categoryId;
  71. OC.Settings.Apps.State.availableUpdates = 0;
  72. this._loadCategoryCall = $.ajax(OC.generateUrl('settings/apps/list?category={categoryId}', {
  73. categoryId: categoryId
  74. }), {
  75. type:'GET',
  76. success: function (apps) {
  77. var appListWithIndex = _.indexBy(apps.apps, 'id');
  78. OC.Settings.Apps.State.apps = appListWithIndex;
  79. var appList = _.map(appListWithIndex, function(app) {
  80. // default values for missing fields
  81. return _.extend({level: 0}, app);
  82. });
  83. var source = $("#app-template").html();
  84. var template = Handlebars.compile(source);
  85. if (appList.length) {
  86. appList.sort(function(a,b) {
  87. var levelDiff = b.level - a.level;
  88. if (levelDiff === 0) {
  89. return OC.Util.naturalSortCompare(a.name, b.name);
  90. }
  91. return levelDiff;
  92. });
  93. var firstExperimental = false;
  94. _.each(appList, function(app) {
  95. if(app.level === 0 && firstExperimental === false) {
  96. firstExperimental = true;
  97. OC.Settings.Apps.renderApp(app, template, null, true);
  98. } else {
  99. OC.Settings.Apps.renderApp(app, template, null, false);
  100. }
  101. if (app.update) {
  102. var $update = $('#app-' + app.id + ' .update');
  103. $update.removeClass('hidden');
  104. $update.val(t('settings', 'Update to %s').replace(/%s/g, app.update));
  105. OC.Settings.Apps.State.availableUpdates++;
  106. }
  107. });
  108. if (OC.Settings.Apps.State.availableUpdates > 0) {
  109. OC.Settings.Apps.State.$updateNotification = OC.Notification.show(n('settings', 'You have %n app update pending', 'You have %n app updates pending', OC.Settings.Apps.State.availableUpdates));
  110. }
  111. } else {
  112. $('#apps-list').addClass('hidden');
  113. $('#apps-list-empty').removeClass('hidden').find('h2').text(t('settings', 'No apps found for your version'));
  114. }
  115. $('.enable.needs-download').tooltip({
  116. title: t('settings', 'The app will be downloaded from the app store'),
  117. placement: 'bottom',
  118. container: 'body'
  119. });
  120. $('.app-level .official').tooltip({
  121. title: t('settings', 'Official apps are developed by and within the community. They offer central functionality and are ready for production use.'),
  122. placement: 'bottom',
  123. container: 'body'
  124. });
  125. $('.app-level .approved').tooltip({
  126. title: t('settings', 'Approved apps are developed by trusted developers and have passed a cursory security check. They are actively maintained in an open code repository and their maintainers deem them to be stable for casual to normal use.'),
  127. placement: 'bottom',
  128. container: 'body'
  129. });
  130. $('.app-level .experimental').tooltip({
  131. title: t('settings', 'This app is not checked for security issues and is new or known to be unstable. Install at your own risk.'),
  132. placement: 'bottom',
  133. container: 'body'
  134. });
  135. },
  136. complete: function() {
  137. $('#apps-list').removeClass('icon-loading');
  138. }
  139. });
  140. },
  141. renderApp: function(app, template, selector, firstExperimental) {
  142. if (!template) {
  143. var source = $("#app-template").html();
  144. template = Handlebars.compile(source);
  145. }
  146. if (typeof app === 'string') {
  147. app = OC.Settings.Apps.State.apps[app];
  148. }
  149. app.firstExperimental = firstExperimental;
  150. if (!app.preview) {
  151. app.preview = OC.imagePath('core', 'default-app-icon');
  152. app.previewAsIcon = true;
  153. }
  154. if (_.isArray(app.author)) {
  155. var authors = [];
  156. _.each(app.author, function (author) {
  157. if (typeof author === 'string') {
  158. authors.push(author);
  159. } else {
  160. authors.push(author['@value']);
  161. }
  162. });
  163. app.author = authors.join(', ');
  164. } else if (typeof app.author !== 'string') {
  165. app.author = app.author['@value'];
  166. }
  167. var html = template(app);
  168. if (selector) {
  169. selector.html(html);
  170. } else {
  171. $('#apps-list').append(html);
  172. }
  173. var page = $('#app-' + app.id);
  174. // image loading kung-fu (IE doesn't properly scale SVGs, so disable app icons)
  175. if (app.preview && !OC.Util.isIE()) {
  176. var currentImage = new Image();
  177. currentImage.src = app.preview;
  178. currentImage.onload = function() {
  179. page.find('.app-image')
  180. .append(OC.Settings.Apps.imageUrl(app.preview, app.fromAppStore))
  181. .fadeIn();
  182. };
  183. }
  184. // set group select properly
  185. if(OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
  186. OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging') ||
  187. OC.Settings.Apps.isType(app, 'prevent_group_restriction')) {
  188. page.find(".groups-enable").hide();
  189. page.find(".groups-enable__checkbox").prop('checked', false);
  190. } else {
  191. page.find('#group_select').val((app.groups || []).join('|'));
  192. if (app.active) {
  193. if (app.groups.length) {
  194. OC.Settings.Apps.setupGroupsSelect(page.find('#group_select'));
  195. page.find(".groups-enable__checkbox").prop('checked', true);
  196. } else {
  197. page.find(".groups-enable__checkbox").prop('checked', false);
  198. }
  199. page.find(".groups-enable").show();
  200. } else {
  201. page.find(".groups-enable").hide();
  202. }
  203. }
  204. },
  205. /**
  206. * Returns the image for apps listing
  207. * url : the url of the image
  208. * appfromstore: bool to check whether the app is fetched from store or not.
  209. */
  210. imageUrl : function (url, appfromstore) {
  211. var img = '<svg width="72" height="72" viewBox="0 0 72 72">';
  212. if (appfromstore) {
  213. img += '<image x="0" y="0" width="72" height="72" preserveAspectRatio="xMinYMin meet" xlink:href="' + url + '" class="app-icon" /></svg>';
  214. } else {
  215. img += '<image x="0" y="0" width="72" height="72" preserveAspectRatio="xMinYMin meet" filter="url(#invertIcon)" xlink:href="' + url + '?v=' + oc_config.version + '" class="app-icon"></image></svg>';
  216. }
  217. return img;
  218. },
  219. isType: function(app, type){
  220. return app.types && app.types.indexOf(type) !== -1;
  221. },
  222. /**
  223. * Checks the server health.
  224. *
  225. * If the promise fails, the server is broken.
  226. *
  227. * @return {Promise} promise
  228. */
  229. _checkServerHealth: function() {
  230. return $.get(OC.generateUrl('apps/files'));
  231. },
  232. enableApp:function(appId, active, element, groups) {
  233. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  234. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.enableApp, this, appId, active, element, groups));
  235. return;
  236. }
  237. var self = this;
  238. OC.Settings.Apps.hideErrorMessage(appId);
  239. groups = groups || [];
  240. var appItem = $('div#app-'+appId+'');
  241. element.val(t('settings','Enabling app …'));
  242. if(active && !groups.length) {
  243. $.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appId},function(result) {
  244. if(!result || result.status !== 'success') {
  245. if (result.data && result.data.message) {
  246. OC.Settings.Apps.showErrorMessage(appId, result.data.message);
  247. appItem.data('errormsg', result.data.message);
  248. } else {
  249. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while disabling app'));
  250. appItem.data('errormsg', t('settings', 'Error while disabling app'));
  251. }
  252. element.val(t('settings','Disable'));
  253. appItem.addClass('appwarning');
  254. } else {
  255. OC.Settings.Apps.rebuildNavigation();
  256. appItem.data('active',false);
  257. appItem.data('groups', '');
  258. element.data('active',false);
  259. appItem.removeClass('active');
  260. element.val(t('settings','Enable'));
  261. element.parent().find(".groups-enable").hide();
  262. element.parent().find('#group_select').hide().val(null);
  263. OC.Settings.Apps.State.apps[appId].active = false;
  264. }
  265. },'json');
  266. } else {
  267. // TODO: display message to admin to not refresh the page!
  268. // TODO: lock UI to prevent further operations
  269. $.post(OC.filePath('settings','ajax','enableapp.php'),{appid: appId, groups: groups},function(result) {
  270. if(!result || result.status !== 'success') {
  271. if (result.data && result.data.message) {
  272. OC.Settings.Apps.showErrorMessage(appId, result.data.message);
  273. appItem.data('errormsg', result.data.message);
  274. } else {
  275. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
  276. appItem.data('errormsg', t('settings', 'Error while disabling app'));
  277. }
  278. element.val(t('settings','Enable'));
  279. appItem.addClass('appwarning');
  280. } else {
  281. self._checkServerHealth().done(function() {
  282. if (result.data.update_required) {
  283. OC.Settings.Apps.showReloadMessage();
  284. setTimeout(function() {
  285. location.reload();
  286. }, 5000);
  287. }
  288. OC.Settings.Apps.rebuildNavigation();
  289. appItem.data('active',true);
  290. element.data('active',true);
  291. appItem.addClass('active');
  292. element.val(t('settings','Disable'));
  293. var app = OC.Settings.Apps.State.apps[appId];
  294. app.active = true;
  295. if (OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
  296. OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
  297. element.parent().find(".groups-enable").prop('checked', true);
  298. element.parent().find(".groups-enable").hide();
  299. element.parent().find('#group_select').hide().val(null);
  300. } else {
  301. element.parent().find("#groups-enable").show();
  302. if (groups) {
  303. appItem.data('groups', JSON.stringify(groups));
  304. } else {
  305. appItem.data('groups', '');
  306. }
  307. }
  308. }).fail(function() {
  309. // server borked, emergency disable app
  310. $.post(OC.webroot + '/index.php/disableapp', {appid: appId}, function() {
  311. OC.Settings.Apps.showErrorMessage(
  312. appId,
  313. t('settings', 'Error: this app cannot be enabled because it makes the server unstable')
  314. );
  315. appItem.data('errormsg', t('settings', 'Error while enabling app'));
  316. element.val(t('settings','Enable'));
  317. appItem.addClass('appwarning');
  318. }).fail(function() {
  319. OC.Settings.Apps.showErrorMessage(
  320. appId,
  321. t('settings', 'Error: could not disable broken app')
  322. );
  323. appItem.data('errormsg', t('settings', 'Error while disabling broken app'));
  324. element.val(t('settings','Enable'));
  325. });
  326. });
  327. }
  328. },'json')
  329. .fail(function() {
  330. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
  331. appItem.data('errormsg', t('settings', 'Error while enabling app'));
  332. appItem.data('active',false);
  333. appItem.addClass('appwarning');
  334. element.val(t('settings','Enable'));
  335. });
  336. }
  337. },
  338. updateApp:function(appId, element) {
  339. var oldButtonText = element.val();
  340. element.val(t('settings','Updating....'));
  341. OC.Settings.Apps.hideErrorMessage(appId);
  342. $.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appId},function(result) {
  343. if(!result || result.status !== 'success') {
  344. if (result.data && result.data.message) {
  345. OC.Settings.Apps.showErrorMessage(appId, result.data.message);
  346. } else {
  347. OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while updating app'));
  348. }
  349. element.val(oldButtonText);
  350. }
  351. else {
  352. element.val(t('settings','Updated'));
  353. element.hide();
  354. var $update = $('#app-' + appId + ' .update');
  355. $update.addClass('hidden');
  356. var $version = $('#app-' + appId + ' .app-version');
  357. $version.text(OC.Settings.Apps.State.apps[appId]['update']);
  358. if (OC.Settings.Apps.State.$updateNotification) {
  359. OC.Notification.hide(OC.Settings.Apps.State.$updateNotification);
  360. }
  361. OC.Settings.Apps.State.availableUpdates--;
  362. if (OC.Settings.Apps.State.availableUpdates > 0) {
  363. OC.Settings.Apps.State.$updateNotification = OC.Notification.show(n('settings', 'You have %n app update pending', 'You have %n app updates pending', OC.Settings.Apps.State.availableUpdates));
  364. }
  365. }
  366. },'json');
  367. },
  368. uninstallApp:function(appId, element) {
  369. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  370. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.uninstallApp, this, appId, element));
  371. return;
  372. }
  373. OC.Settings.Apps.hideErrorMessage(appId);
  374. element.val(t('settings','Uninstalling ....'));
  375. $.post(OC.filePath('settings','ajax','uninstallapp.php'),{appid:appId},function(result) {
  376. if(!result || result.status !== 'success') {
  377. OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while uninstalling app'));
  378. element.val(t('settings','Uninstall'));
  379. } else {
  380. OC.Settings.Apps.rebuildNavigation();
  381. element.parent().fadeOut(function() {
  382. element.remove();
  383. });
  384. }
  385. },'json');
  386. },
  387. rebuildNavigation: function() {
  388. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php')).done(function(response){
  389. if(response.status === 'success'){
  390. var idsToKeep = {};
  391. var navEntries=response.nav_entries;
  392. var container = $('#apps ul');
  393. for(var i=0; i< navEntries.length; i++){
  394. var entry = navEntries[i];
  395. idsToKeep[entry.id] = true;
  396. if(container.children('li[data-id="'+entry.id+'"]').length === 0){
  397. var li=$('<li></li>');
  398. li.attr('data-id', entry.id);
  399. var img = '<svg width="32" height="32" viewBox="0 0 32 32">';
  400. img += '<defs><filter id="invert"><feColorMatrix in="SourceGraphic" type="matrix" values="-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 1 0" /></filter></defs>';
  401. img += '<image x="0" y="0" width="32" height="32" preserveAspectRatio="xMinYMin meet" filter="url(#invert)" xlink:href="' + entry.icon + '" class="app-icon" /></svg>';
  402. var a=$('<a></a>').attr('href', entry.href);
  403. var filename=$('<span></span>');
  404. var loading = $('<div class="icon-loading-dark"></div>').css('display', 'none');
  405. filename.text(entry.name);
  406. a.prepend(filename);
  407. a.prepend(loading);
  408. a.prepend(img);
  409. li.append(a);
  410. // append the new app as last item in the list
  411. // which is the "add apps" entry with the id
  412. // #apps-management
  413. $('#apps-management').before(li);
  414. // scroll the app navigation down
  415. // so the newly added app is seen
  416. $('#navigation').animate({
  417. scrollTop: $('#navigation').height()
  418. }, 'slow');
  419. // draw attention to the newly added app entry
  420. // by flashing it twice
  421. $('#header .menutoggle')
  422. .animate({opacity: 0.5})
  423. .animate({opacity: 1})
  424. .animate({opacity: 0.5})
  425. .animate({opacity: 1})
  426. .animate({opacity: 0.75});
  427. }
  428. }
  429. container.children('li[data-id]').each(function(index, el) {
  430. if (!idsToKeep[$(el).data('id')]) {
  431. $(el).remove();
  432. }
  433. });
  434. }
  435. });
  436. },
  437. showErrorMessage: function(appId, message) {
  438. $('div#app-'+appId+' .warning')
  439. .show()
  440. .text(message);
  441. },
  442. hideErrorMessage: function(appId) {
  443. $('div#app-'+appId+' .warning')
  444. .hide()
  445. .text('');
  446. },
  447. showReloadMessage: function() {
  448. OC.dialogs.info(
  449. t(
  450. 'settings',
  451. 'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.'
  452. ),
  453. t('settings','App update'),
  454. function () {
  455. window.location.reload();
  456. },
  457. true
  458. );
  459. },
  460. /**
  461. * Splits the query by spaces and tries to find all substring in the app
  462. * @param {string} string
  463. * @param {string} query
  464. * @returns {boolean}
  465. */
  466. _search: function(string, query) {
  467. var keywords = query.split(' '),
  468. stringLower = string.toLowerCase(),
  469. found = true;
  470. _.each(keywords, function(keyword) {
  471. found = found && stringLower.indexOf(keyword) !== -1;
  472. });
  473. return found;
  474. },
  475. filter: function(query) {
  476. var $appList = $('#apps-list'),
  477. $emptyList = $('#apps-list-empty');
  478. $appList.removeClass('hidden');
  479. $appList.find('.section').removeClass('hidden');
  480. $emptyList.addClass('hidden');
  481. if (query === '') {
  482. return;
  483. }
  484. query = query.toLowerCase();
  485. $appList.find('.section').addClass('hidden');
  486. // App Name
  487. var apps = _.filter(OC.Settings.Apps.State.apps, function (app) {
  488. return OC.Settings.Apps._search(app.name, query);
  489. });
  490. // App ID
  491. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  492. return OC.Settings.Apps._search(app.id, query);
  493. }));
  494. // App Description
  495. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  496. return OC.Settings.Apps._search(app.description, query);
  497. }));
  498. // Author Name
  499. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  500. var authors = [];
  501. if (_.isArray(app.author)) {
  502. _.each(app.author, function (author) {
  503. if (typeof author === 'string') {
  504. authors.push(author);
  505. } else {
  506. authors.push(author['@value']);
  507. if (!_.isUndefined(author['@attributes']['homepage'])) {
  508. authors.push(author['@attributes']['homepage']);
  509. }
  510. if (!_.isUndefined(author['@attributes']['mail'])) {
  511. authors.push(author['@attributes']['mail']);
  512. }
  513. }
  514. });
  515. return OC.Settings.Apps._search(authors.join(' '), query);
  516. } else if (typeof app.author !== 'string') {
  517. authors.push(app.author['@value']);
  518. if (!_.isUndefined(app.author['@attributes']['homepage'])) {
  519. authors.push(app.author['@attributes']['homepage']);
  520. }
  521. if (!_.isUndefined(app.author['@attributes']['mail'])) {
  522. authors.push(app.author['@attributes']['mail']);
  523. }
  524. return OC.Settings.Apps._search(authors.join(' '), query);
  525. }
  526. return OC.Settings.Apps._search(app.author, query);
  527. }));
  528. // App status
  529. if (t('settings', 'Official').toLowerCase().indexOf(query) !== -1) {
  530. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  531. return app.level === 200;
  532. }));
  533. }
  534. if (t('settings', 'Approved').toLowerCase().indexOf(query) !== -1) {
  535. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  536. return app.level === 100;
  537. }));
  538. }
  539. if (t('settings', 'Experimental').toLowerCase().indexOf(query) !== -1) {
  540. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  541. return app.level !== 100 && app.level !== 200;
  542. }));
  543. }
  544. apps = _.uniq(apps, function(app){return app.id;});
  545. if (apps.length === 0) {
  546. $appList.addClass('hidden');
  547. $emptyList.removeClass('hidden');
  548. $emptyList.removeClass('hidden').find('h2').text(t('settings', 'No apps found for {query}', {
  549. query: query
  550. }));
  551. } else {
  552. _.each(apps, function (app) {
  553. $('#app-' + app.id).removeClass('hidden');
  554. });
  555. $('#searchresults').hide();
  556. }
  557. },
  558. _onPopState: function(params) {
  559. params = _.extend({
  560. category: 'enabled'
  561. }, params);
  562. OC.Settings.Apps.loadCategory(params.category);
  563. },
  564. /**
  565. * Initializes the apps list
  566. */
  567. initialize: function($el) {
  568. OC.Plugins.register('OCA.Search', OC.Settings.Apps.Search);
  569. OC.Settings.Apps.loadCategories();
  570. OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
  571. $(document).on('click', 'ul#apps-categories li', function () {
  572. var categoryId = $(this).data('categoryId');
  573. OC.Settings.Apps.loadCategory(categoryId);
  574. OC.Util.History.pushState({
  575. category: categoryId
  576. });
  577. $('#searchbox').val('');
  578. });
  579. $(document).on('click', '.app-description-toggle-show', function () {
  580. $(this).addClass('hidden');
  581. $(this).siblings('.app-description-toggle-hide').removeClass('hidden');
  582. $(this).siblings('.app-description-container').slideDown();
  583. });
  584. $(document).on('click', '.app-description-toggle-hide', function () {
  585. $(this).addClass('hidden');
  586. $(this).siblings('.app-description-toggle-show').removeClass('hidden');
  587. $(this).siblings('.app-description-container').slideUp();
  588. });
  589. $(document).on('click', '#apps-list input.enable', function () {
  590. var appId = $(this).data('appid');
  591. var element = $(this);
  592. var active = $(this).data('active');
  593. OC.Settings.Apps.enableApp(appId, active, element);
  594. });
  595. $(document).on('click', '#apps-list input.uninstall', function () {
  596. var appId = $(this).data('appid');
  597. var element = $(this);
  598. OC.Settings.Apps.uninstallApp(appId, element);
  599. });
  600. $(document).on('click', '#apps-list input.update', function () {
  601. var appId = $(this).data('appid');
  602. var element = $(this);
  603. OC.Settings.Apps.updateApp(appId, element);
  604. });
  605. $(document).on('change', '#group_select', function() {
  606. var element = $(this).parent().find('input.enable');
  607. var groups = $(this).val();
  608. if (groups && groups !== '') {
  609. groups = groups.split('|');
  610. } else {
  611. groups = [];
  612. }
  613. var appId = element.data('appid');
  614. if (appId) {
  615. OC.Settings.Apps.enableApp(appId, false, element, groups);
  616. OC.Settings.Apps.State.apps[appId].groups = groups;
  617. }
  618. });
  619. $(document).on('change', ".groups-enable__checkbox", function() {
  620. var $select = $(this).closest('.section').find('#group_select');
  621. $select.val('');
  622. if (this.checked) {
  623. OC.Settings.Apps.setupGroupsSelect($select);
  624. } else {
  625. $select.select2('destroy');
  626. }
  627. $select.change();
  628. });
  629. $(document).on('click', '#enable-experimental-apps', function () {
  630. var state = $(this).prop('checked');
  631. $.ajax(OC.generateUrl('settings/apps/experimental'), {
  632. data: {state: state},
  633. type: 'POST',
  634. success:function () {
  635. location.reload();
  636. }
  637. });
  638. });
  639. }
  640. };
  641. OC.Settings.Apps.Search = {
  642. attach: function (search) {
  643. search.setFilter('settings', OC.Settings.Apps.filter);
  644. }
  645. };
  646. $(document).ready(function () {
  647. // HACK: FIXME: use plugin approach
  648. if (!window.TESTING) {
  649. OC.Settings.Apps.initialize($('#apps-list'));
  650. }
  651. });