apps.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 + '.png';
  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">Official</span>');
  14. } else if(this.level === 100) {
  15. return new Handlebars.SafeString('<span class="approved">Approved</span>');
  16. } else {
  17. return new Handlebars.SafeString('<span class="experimental">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: 'Enabled', id: '0'}
  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(0);
  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. this._loadCategoryCall = $.ajax(OC.generateUrl('settings/apps/list?category={categoryId}', {
  73. categoryId: categoryId
  74. }), {
  75. type:'GET',
  76. success: function (apps) {
  77. OC.Settings.Apps.State.apps = _.indexBy(apps.apps, 'id');
  78. var source = $("#app-template").html();
  79. var template = Handlebars.compile(source);
  80. if (apps.apps.length) {
  81. apps.apps.sort(function(a,b) {
  82. return b.level - a.level;
  83. });
  84. var firstExperimental = false;
  85. _.each(apps.apps, function(app) {
  86. if(app.level === 0 && firstExperimental === false) {
  87. firstExperimental = true;
  88. OC.Settings.Apps.renderApp(app, template, null, true);
  89. } else {
  90. OC.Settings.Apps.renderApp(app, template, null, false);
  91. }
  92. });
  93. } else {
  94. $('#apps-list').addClass('hidden');
  95. $('#apps-list-empty').removeClass('hidden');
  96. }
  97. $('.app-level .official').tipsy({fallback: t('core', 'Official apps are developed by and within the ownCloud community. They offer functionality central to ownCloud and are ready for production use.')});
  98. $('.app-level .approved').tipsy({fallback: t('core', '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.')});
  99. $('.app-level .experimental').tipsy({fallback: t('core', 'This app is not checked for security issues and is new or known to be unstable. Install on your own risk.')});
  100. },
  101. complete: function() {
  102. $('#apps-list').removeClass('icon-loading');
  103. }
  104. });
  105. },
  106. renderApp: function(app, template, selector, firstExperimental) {
  107. if (!template) {
  108. var source = $("#app-template").html();
  109. template = Handlebars.compile(source);
  110. }
  111. if (typeof app === 'string') {
  112. app = OC.Settings.Apps.State.apps[app];
  113. }
  114. app.firstExperimental = firstExperimental;
  115. var html = template(app);
  116. if (selector) {
  117. selector.html(html);
  118. } else {
  119. $('#apps-list').append(html);
  120. }
  121. var page = $('#app-' + app.id);
  122. // image loading kung-fu
  123. if (app.preview) {
  124. var currentImage = new Image();
  125. currentImage.src = app.preview;
  126. currentImage.onload = function() {
  127. page.find('.app-image')
  128. .append(this)
  129. .fadeIn();
  130. };
  131. }
  132. // set group select properly
  133. if(OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
  134. OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
  135. page.find(".groups-enable").hide();
  136. page.find("label[for='groups_enable-"+app.id+"']").hide();
  137. page.find(".groups-enable").attr('checked', null);
  138. } else {
  139. page.find('#group_select').val((app.groups || []).join('|'));
  140. if (app.active) {
  141. if (app.groups.length) {
  142. OC.Settings.Apps.setupGroupsSelect(page.find('#group_select'));
  143. page.find(".groups-enable").attr('checked','checked');
  144. } else {
  145. page.find(".groups-enable").attr('checked', null);
  146. }
  147. page.find(".groups-enable").show();
  148. page.find("label[for='groups_enable-"+app.id+"']").show();
  149. } else {
  150. page.find(".groups-enable").hide();
  151. page.find("label[for='groups_enable-"+app.id+"']").hide();
  152. }
  153. }
  154. },
  155. isType: function(app, type){
  156. return app.types && app.types.indexOf(type) !== -1;
  157. },
  158. enableApp:function(appId, active, element, groups) {
  159. OC.Settings.Apps.hideErrorMessage(appId);
  160. groups = groups || [];
  161. var appItem = $('div#app-'+appId+'');
  162. element.val(t('settings','Please wait....'));
  163. if(active && !groups.length) {
  164. $.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appId},function(result) {
  165. if(!result || result.status !== 'success') {
  166. if (result.data && result.data.message) {
  167. OC.Settings.Apps.showErrorMessage(appId, result.data.message);
  168. appItem.data('errormsg', result.data.message);
  169. } else {
  170. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while disabling app'));
  171. appItem.data('errormsg', t('settings', 'Error while disabling app'));
  172. }
  173. element.val(t('settings','Disable'));
  174. appItem.addClass('appwarning');
  175. } else {
  176. appItem.data('active',false);
  177. appItem.data('groups', '');
  178. element.data('active',false);
  179. OC.Settings.Apps.removeNavigation(appId);
  180. appItem.removeClass('active');
  181. element.val(t('settings','Enable'));
  182. element.parent().find(".groups-enable").hide();
  183. element.parent().find("#groups_enable-"+appId).hide();
  184. element.parent().find("label[for='groups_enable-"+appId+"']").hide();
  185. element.parent().find('#group_select').hide().val(null);
  186. OC.Settings.Apps.State.apps[appId].active = false;
  187. }
  188. },'json');
  189. } else {
  190. $.post(OC.filePath('settings','ajax','enableapp.php'),{appid: appId, groups: groups},function(result) {
  191. if(!result || result.status !== 'success') {
  192. if (result.data && result.data.message) {
  193. OC.Settings.Apps.showErrorMessage(appId, result.data.message);
  194. appItem.data('errormsg', result.data.message);
  195. } else {
  196. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
  197. appItem.data('errormsg', t('settings', 'Error while disabling app'));
  198. }
  199. element.val(t('settings','Enable'));
  200. appItem.addClass('appwarning');
  201. } else {
  202. OC.Settings.Apps.addNavigation(appId);
  203. appItem.data('active',true);
  204. element.data('active',true);
  205. appItem.addClass('active');
  206. element.val(t('settings','Disable'));
  207. var app = OC.Settings.Apps.State.apps[appId];
  208. app.active = true;
  209. if (OC.Settings.Apps.isType(app, 'filesystem') || OC.Settings.Apps.isType(app, 'prelogin') ||
  210. OC.Settings.Apps.isType(app, 'authentication') || OC.Settings.Apps.isType(app, 'logging')) {
  211. element.parent().find(".groups-enable").attr('checked', null);
  212. element.parent().find("#groups_enable-"+appId).hide();
  213. element.parent().find("label[for='groups_enable-"+appId+"']").hide();
  214. element.parent().find(".groups-enable").hide();
  215. element.parent().find("#groups_enable-"+appId).hide();
  216. element.parent().find("label[for='groups_enable-"+appId+"']").hide();
  217. element.parent().find('#group_select').hide().val(null);
  218. } else {
  219. element.parent().find("#groups_enable-"+appId).show();
  220. element.parent().find("label[for='groups_enable-"+appId+"']").show();
  221. if (groups) {
  222. appItem.data('groups', JSON.stringify(groups));
  223. } else {
  224. appItem.data('groups', '');
  225. }
  226. }
  227. }
  228. },'json')
  229. .fail(function() {
  230. OC.Settings.Apps.showErrorMessage(appId, t('settings', 'Error while enabling app'));
  231. appItem.data('errormsg', t('settings', 'Error while enabling app'));
  232. appItem.data('active',false);
  233. appItem.addClass('appwarning');
  234. OC.Settings.Apps.removeNavigation(appId);
  235. element.val(t('settings','Enable'));
  236. });
  237. }
  238. },
  239. updateApp:function(appId, element) {
  240. var oldButtonText = element.val();
  241. element.val(t('settings','Updating....'));
  242. OC.Settings.Apps.hideErrorMessage(appId);
  243. $.post(OC.filePath('settings','ajax','updateapp.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. } else {
  248. OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while updating app'));
  249. }
  250. element.val(oldButtonText);
  251. }
  252. else {
  253. element.val(t('settings','Updated'));
  254. element.hide();
  255. }
  256. },'json');
  257. },
  258. uninstallApp:function(appId, element) {
  259. OC.Settings.Apps.hideErrorMessage(appId);
  260. element.val(t('settings','Uninstalling ....'));
  261. $.post(OC.filePath('settings','ajax','uninstallapp.php'),{appid:appId},function(result) {
  262. if(!result || result.status !== 'success') {
  263. OC.Settings.Apps.showErrorMessage(appId, t('settings','Error while uninstalling app'));
  264. element.val(t('settings','Uninstall'));
  265. } else {
  266. OC.Settings.Apps.removeNavigation(appId);
  267. element.parent().fadeOut(function() {
  268. element.remove();
  269. });
  270. }
  271. },'json');
  272. },
  273. removeNavigation: function(appId){
  274. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appId}).done(function(response){
  275. if(response.status === 'success'){
  276. var navIds=response.nav_ids;
  277. for(var i=0; i< navIds.length; i++){
  278. $('#apps ul').children('li[data-id="'+navIds[i]+'"]').remove();
  279. }
  280. }
  281. });
  282. },
  283. addNavigation: function(appid){
  284. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  285. if(response.status === 'success'){
  286. var navEntries=response.nav_entries;
  287. for(var i=0; i< navEntries.length; i++){
  288. var entry = navEntries[i];
  289. var container = $('#apps ul');
  290. if(container.children('li[data-id="'+entry.id+'"]').length === 0){
  291. var li=$('<li></li>');
  292. li.attr('data-id', entry.id);
  293. var img= $('<img class="app-icon"/>').attr({ src: entry.icon});
  294. var a=$('<a></a>').attr('href', entry.href);
  295. var filename=$('<span></span>');
  296. var loading = $('<div class="icon-loading-dark"></div>').css('display', 'none');
  297. filename.text(entry.name);
  298. a.prepend(filename);
  299. a.prepend(loading);
  300. a.prepend(img);
  301. li.append(a);
  302. // append the new app as last item in the list
  303. // which is the "add apps" entry with the id
  304. // #apps-management
  305. $('#apps-management').before(li);
  306. // scroll the app navigation down
  307. // so the newly added app is seen
  308. $('#navigation').animate({
  309. scrollTop: $('#navigation').height()
  310. }, 'slow');
  311. // draw attention to the newly added app entry
  312. // by flashing it twice
  313. $('#header .menutoggle')
  314. .animate({opacity: 0.5})
  315. .animate({opacity: 1})
  316. .animate({opacity: 0.5})
  317. .animate({opacity: 1})
  318. .animate({opacity: 0.75});
  319. if (!OC.Util.hasSVGSupport() && entry.icon.match(/\.svg$/i)) {
  320. $(img).addClass('svg');
  321. OC.Util.replaceSVG();
  322. }
  323. }
  324. }
  325. }
  326. });
  327. },
  328. showErrorMessage: function(appId, message) {
  329. $('div#app-'+appId+' .warning')
  330. .show()
  331. .text(message);
  332. },
  333. hideErrorMessage: function(appId) {
  334. $('div#app-'+appId+' .warning')
  335. .hide()
  336. .text('');
  337. },
  338. filter: function(query) {
  339. query = query.toLowerCase();
  340. $('#apps-list').find('.section').addClass('hidden');
  341. var apps = _.filter(OC.Settings.Apps.State.apps, function (app) {
  342. return app.name.toLowerCase().indexOf(query) !== -1;
  343. });
  344. apps = apps.concat(_.filter(OC.Settings.Apps.State.apps, function (app) {
  345. return app.description.toLowerCase().indexOf(query) !== -1;
  346. }));
  347. apps = _.uniq(apps, function(app){return app.id;});
  348. _.each(apps, function (app) {
  349. $('#app-' + app.id).removeClass('hidden');
  350. });
  351. $('#searchresults').hide();
  352. },
  353. /**
  354. * Initializes the apps list
  355. */
  356. initialize: function($el) {
  357. OC.Plugins.register('OCA.Search', OC.Settings.Apps.Search);
  358. OC.Settings.Apps.loadCategories();
  359. $(document).on('click', 'ul#apps-categories li', function () {
  360. var categoryId = $(this).data('categoryId');
  361. OC.Settings.Apps.loadCategory(categoryId);
  362. });
  363. $(document).on('click', '.app-description-toggle-show', function () {
  364. $(this).addClass('hidden');
  365. $(this).siblings('.app-description-toggle-hide').removeClass('hidden');
  366. $(this).siblings('.app-description-container').slideDown();
  367. });
  368. $(document).on('click', '.app-description-toggle-hide', function () {
  369. $(this).addClass('hidden');
  370. $(this).siblings('.app-description-toggle-show').removeClass('hidden');
  371. $(this).siblings('.app-description-container').slideUp();
  372. });
  373. $(document).on('click', '#apps-list input.enable', function () {
  374. var appId = $(this).data('appid');
  375. var element = $(this);
  376. var active = $(this).data('active');
  377. OC.Settings.Apps.enableApp(appId, active, element);
  378. });
  379. $(document).on('click', '#apps-list input.uninstall', function () {
  380. var appId = $(this).data('appid');
  381. var element = $(this);
  382. OC.Settings.Apps.uninstallApp(appId, element);
  383. });
  384. $(document).on('click', '#apps-list input.update', function () {
  385. var appId = $(this).data('appid');
  386. var element = $(this);
  387. OC.Settings.Apps.updateApp(appId, element);
  388. });
  389. $(document).on('change', '#group_select', function() {
  390. var element = $(this).parent().find('input.enable');
  391. var groups = $(this).val();
  392. if (groups && groups !== '') {
  393. groups = groups.split('|');
  394. } else {
  395. groups = [];
  396. }
  397. var appId = element.data('appid');
  398. if (appId) {
  399. OC.Settings.Apps.enableApp(appId, false, element, groups);
  400. OC.Settings.Apps.State.apps[appId].groups = groups;
  401. }
  402. });
  403. $(document).on('change', ".groups-enable", function() {
  404. var $select = $(this).parent().find('#group_select');
  405. $select.val('');
  406. if (this.checked) {
  407. OC.Settings.Apps.setupGroupsSelect($select);
  408. } else {
  409. $select.select2('destroy');
  410. }
  411. $select.change();
  412. });
  413. $(document).on('click', '#enable-experimental-apps', function () {
  414. var state = $(this).prop('checked')
  415. $.ajax(OC.generateUrl('settings/apps/experimental'), {
  416. data: {state: state},
  417. type: 'POST',
  418. success:function () {
  419. location.reload();
  420. }
  421. });
  422. });
  423. }
  424. };
  425. OC.Settings.Apps.Search = {
  426. attach: function (search) {
  427. search.setFilter('settings', OC.Settings.Apps.filter);
  428. }
  429. };
  430. $(document).ready(function () {
  431. // HACK: FIXME: use plugin approach
  432. if (!window.TESTING) {
  433. OC.Settings.Apps.initialize($('#apps-list'));
  434. }
  435. });