1
0

apps.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 addedApps = {};
  411. var navEntries = response.nav_entries;
  412. var container = $('#apps ul');
  413. // remove disabled apps
  414. for (var i = 0; i < navEntries.length; i++) {
  415. var entry = navEntries[i];
  416. if(container.children('li[data-id="' + entry.id + '"]').length === 0) {
  417. addedApps[entry.id] = true;
  418. }
  419. }
  420. container.children('li[data-id]').each(function (index, el) {
  421. var id = $(el).data('id');
  422. // remove all apps that are not in the correct order
  423. if ((navEntries[index] && navEntries[index].id !== $(el).data('id'))) {
  424. $(el).remove();
  425. $('#appmenu li[data-id='+id+']').remove();
  426. }
  427. });
  428. var previousEntry;
  429. // add enabled apps to #navigation and #appmenu
  430. for (var i = 0; i < navEntries.length; i++) {
  431. var entry = navEntries[i];
  432. if (container.children('li[data-id="' + entry.id + '"]').length === 0) {
  433. var li = $('<li></li>');
  434. li.attr('data-id', entry.id);
  435. var img = '<svg width="32" height="32" viewBox="0 0 32 32">';
  436. 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>';
  437. img += '<image x="0" y="0" width="32" height="32" preserveAspectRatio="xMinYMin meet" filter="url(#invert)" xlink:href="' + entry.icon + '" class="app-icon" /></svg>';
  438. var a = $('<a></a>').attr('href', entry.href);
  439. var filename = $('<span></span>');
  440. var loading = $('<div class="icon-loading-dark"></div>').css('display', 'none');
  441. filename.text(entry.name);
  442. a.prepend(filename);
  443. a.prepend(loading);
  444. a.prepend(img);
  445. li.append(a);
  446. $('#navigation li[data-id=' + previousEntry.id + ']').after(li);
  447. // draw attention to the newly added app entry
  448. // by flashing it twice
  449. if(addedApps[entry.id]) {
  450. $('#header .menutoggle')
  451. .animate({opacity: 0.5})
  452. .animate({opacity: 1})
  453. .animate({opacity: 0.5})
  454. .animate({opacity: 1})
  455. .animate({opacity: 0.75});
  456. }
  457. }
  458. if ($('#appmenu ul').children('li[data-id="' + entry.id + '"]').length === 0) {
  459. // add apps to #appmenu until it is full
  460. if ($('#appmenu li').not('.hidden').length < 8) {
  461. var li = $('<li></li>');
  462. li.attr('data-id', entry.id);
  463. var img = '<img src="' + entry.icon + '" class="app-icon">';
  464. var a = $('<a></a>').attr('href', entry.href);
  465. var filename = $('<span></span>');
  466. var loading = $('<div class="icon-loading-dark"></div>').css('display', 'none');
  467. filename.text(entry.name);
  468. a.prepend(filename);
  469. a.prepend(loading);
  470. a.prepend(img);
  471. li.append(a);
  472. $('#appmenu li[data-id='+ previousEntry.id+']').after(li);
  473. if(addedApps[entry.id]) {
  474. li.animate({opacity: 0.5})
  475. .animate({opacity: 1})
  476. .animate({opacity: 0.5})
  477. .animate({opacity: 1});
  478. }
  479. }
  480. }
  481. previousEntry = entry;
  482. // do not show apps from #appmenu in #navigation
  483. if(i <= 7) {
  484. $('#navigation li').eq(i).addClass('in-header');
  485. } else {
  486. $('#navigation li').eq(i).removeClass('in-header');
  487. }
  488. }
  489. if (navEntries.length > 8) {
  490. $('#more-apps').show();
  491. } else {
  492. $('#more-apps').hide();
  493. }
  494. }
  495. });
  496. },
  497. showErrorMessage: function(appId, message) {
  498. $('div#app-'+appId+' .warning')
  499. .show()
  500. .text(message);
  501. },
  502. hideErrorMessage: function(appId) {
  503. $('div#app-'+appId+' .warning')
  504. .hide()
  505. .text('');
  506. },
  507. showReloadMessage: function() {
  508. OC.dialogs.info(
  509. t(
  510. 'settings',
  511. 'The app has been enabled but needs to be updated. You will be redirected to the update page in 5 seconds.'
  512. ),
  513. t('settings','App update'),
  514. function () {
  515. window.location.reload();
  516. },
  517. true
  518. );
  519. },
  520. /**
  521. * Splits the query by spaces and tries to find all substring in the app
  522. * @param {string} string
  523. * @param {string} query
  524. * @returns {boolean}
  525. */
  526. _search: function(string, query) {
  527. var keywords = query.split(' '),
  528. stringLower = string.toLowerCase(),
  529. found = true;
  530. _.each(keywords, function(keyword) {
  531. found = found && stringLower.indexOf(keyword) !== -1;
  532. });
  533. return found;
  534. },
  535. filter: function(query) {
  536. var $appList = $('#apps-list'),
  537. $emptyList = $('#apps-list-empty');
  538. $appList.removeClass('hidden');
  539. $appList.find('.section').removeClass('hidden');
  540. $emptyList.addClass('hidden');
  541. if (query === '') {
  542. return;
  543. }
  544. query = query.toLowerCase();
  545. $appList.find('.section').addClass('hidden');
  546. // App Name
  547. var apps = _.filter(OC.Settings.Apps.State.apps, function (app) {
  548. return OC.Settings.Apps._search(app.name, query);
  549. });
  550. // App ID
  551. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  552. return OC.Settings.Apps._search(app.id, query);
  553. }));
  554. // App Description
  555. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  556. return OC.Settings.Apps._search(app.description, query);
  557. }));
  558. // Author Name
  559. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  560. var authors = [];
  561. if (_.isArray(app.author)) {
  562. _.each(app.author, function (author) {
  563. if (typeof author === 'string') {
  564. authors.push(author);
  565. } else {
  566. authors.push(author['@value']);
  567. if (!_.isUndefined(author['@attributes']['homepage'])) {
  568. authors.push(author['@attributes']['homepage']);
  569. }
  570. if (!_.isUndefined(author['@attributes']['mail'])) {
  571. authors.push(author['@attributes']['mail']);
  572. }
  573. }
  574. });
  575. return OC.Settings.Apps._search(authors.join(' '), query);
  576. } else if (typeof app.author !== 'string') {
  577. authors.push(app.author['@value']);
  578. if (!_.isUndefined(app.author['@attributes']['homepage'])) {
  579. authors.push(app.author['@attributes']['homepage']);
  580. }
  581. if (!_.isUndefined(app.author['@attributes']['mail'])) {
  582. authors.push(app.author['@attributes']['mail']);
  583. }
  584. return OC.Settings.Apps._search(authors.join(' '), query);
  585. }
  586. return OC.Settings.Apps._search(app.author, query);
  587. }));
  588. // App status
  589. if (t('settings', 'Official').toLowerCase().indexOf(query) !== -1) {
  590. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  591. return app.level === 200;
  592. }));
  593. }
  594. if (t('settings', 'Approved').toLowerCase().indexOf(query) !== -1) {
  595. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  596. return app.level === 100;
  597. }));
  598. }
  599. if (t('settings', 'Experimental').toLowerCase().indexOf(query) !== -1) {
  600. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  601. return app.level !== 100 && app.level !== 200;
  602. }));
  603. }
  604. apps = _.uniq(apps, function(app){return app.id;});
  605. if (apps.length === 0) {
  606. $appList.addClass('hidden');
  607. $emptyList.removeClass('hidden');
  608. $emptyList.removeClass('hidden').find('h2').text(t('settings', 'No apps found for {query}', {
  609. query: query
  610. }));
  611. } else {
  612. _.each(apps, function (app) {
  613. $('#app-' + app.id).removeClass('hidden');
  614. });
  615. $('#searchresults').hide();
  616. }
  617. },
  618. _onPopState: function(params) {
  619. params = _.extend({
  620. category: 'enabled'
  621. }, params);
  622. OC.Settings.Apps.loadCategory(params.category);
  623. },
  624. /**
  625. * Initializes the apps list
  626. */
  627. initialize: function($el) {
  628. var renderer = new marked.Renderer();
  629. renderer.link = function(href, title, text) {
  630. try {
  631. var prot = decodeURIComponent(unescape(href))
  632. .replace(/[^\w:]/g, '')
  633. .toLowerCase();
  634. } catch (e) {
  635. return '';
  636. }
  637. if (prot.indexOf('http:') !== 0 && prot.indexOf('https:') !== 0) {
  638. return '';
  639. }
  640. var out = '<a href="' + href + '" rel="noreferrer noopener"';
  641. if (title) {
  642. out += ' title="' + title + '"';
  643. }
  644. out += '>' + text + '</a>';
  645. return out;
  646. };
  647. renderer.image = function(href, title, text) {
  648. if (text) {
  649. return text;
  650. }
  651. return title;
  652. };
  653. renderer.blockquote = function(quote) {
  654. return quote;
  655. };
  656. OC.Settings.Apps.markedOptions = {
  657. renderer: renderer,
  658. gfm: false,
  659. highlight: false,
  660. tables: false,
  661. breaks: false,
  662. pedantic: false,
  663. sanitize: true,
  664. smartLists: true,
  665. smartypants: false
  666. };
  667. OC.Plugins.register('OCA.Search', OC.Settings.Apps.Search);
  668. OC.Settings.Apps.loadCategories();
  669. OC.Util.History.addOnPopStateHandler(_.bind(this._onPopState, this));
  670. $(document).on('click', 'ul#apps-categories li', function () {
  671. var categoryId = $(this).data('categoryId');
  672. OC.Settings.Apps.loadCategory(categoryId);
  673. OC.Util.History.pushState({
  674. category: categoryId
  675. });
  676. $('#searchbox').val('');
  677. });
  678. $(document).on('click', '.app-description-toggle-show', function () {
  679. $(this).addClass('hidden');
  680. $(this).siblings('.app-description-toggle-hide').removeClass('hidden');
  681. $(this).siblings('.app-description-container').slideDown();
  682. });
  683. $(document).on('click', '.app-description-toggle-hide', function () {
  684. $(this).addClass('hidden');
  685. $(this).siblings('.app-description-toggle-show').removeClass('hidden');
  686. $(this).siblings('.app-description-container').slideUp();
  687. });
  688. $(document).on('click', '#apps-list input.enable', function () {
  689. var appId = $(this).data('appid');
  690. var element = $(this);
  691. var active = $(this).data('active');
  692. OC.Settings.Apps.enableApp(appId, active, element);
  693. });
  694. $(document).on('click', '#apps-list input.uninstall', function () {
  695. var appId = $(this).data('appid');
  696. var element = $(this);
  697. OC.Settings.Apps.uninstallApp(appId, element);
  698. });
  699. $(document).on('click', '#apps-list input.update', function () {
  700. var appId = $(this).data('appid');
  701. var element = $(this);
  702. OC.Settings.Apps.updateApp(appId, element);
  703. });
  704. $(document).on('change', '#group_select', function() {
  705. var element = $(this).parent().find('input.enable');
  706. var groups = $(this).val();
  707. if (groups && groups !== '') {
  708. groups = groups.split('|');
  709. } else {
  710. groups = [];
  711. }
  712. var appId = element.data('appid');
  713. if (appId) {
  714. OC.Settings.Apps.enableApp(appId, false, element, groups);
  715. OC.Settings.Apps.State.apps[appId].groups = groups;
  716. }
  717. });
  718. $(document).on('change', ".groups-enable__checkbox", function() {
  719. var $select = $(this).closest('.section').find('#group_select');
  720. $select.val('');
  721. if (this.checked) {
  722. OC.Settings.Apps.setupGroupsSelect($select);
  723. } else {
  724. $select.select2('destroy');
  725. }
  726. $select.change();
  727. });
  728. $(document).on('click', '#enable-experimental-apps', function () {
  729. var state = $(this).prop('checked');
  730. $.ajax(OC.generateUrl('settings/apps/experimental'), {
  731. data: {state: state},
  732. type: 'POST',
  733. success:function () {
  734. location.reload();
  735. }
  736. });
  737. });
  738. }
  739. };
  740. OC.Settings.Apps.Search = {
  741. attach: function (search) {
  742. search.setFilter('settings', OC.Settings.Apps.filter);
  743. }
  744. };
  745. $(document).ready(function () {
  746. // HACK: FIXME: use plugin approach
  747. if (!window.TESTING) {
  748. OC.Settings.Apps.initialize($('#apps-list'));
  749. }
  750. });