1
0

apps.js 24 KB

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