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 20KB

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