apps.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2011, Robin Appelman <icewind1991@gmail.com>
  3. * Copyright (c) 2012, Thomas Tanghus <thomas@tanghus.net>
  4. * This file is licensed under the Affero General Public License version 3 or later.
  5. * See the COPYING-README file.
  6. */
  7. OC.Settings = OC.Settings || {};
  8. OC.Settings.Apps = OC.Settings.Apps || {
  9. loadApp:function(app) {
  10. var page = $('#rightcontent');
  11. page.find('p.license').show();
  12. page.find('span.name').text(app.name);
  13. page.find('small.externalapp').text(app.internallabel);
  14. if (app.version) {
  15. page.find('span.version').text(app.version);
  16. } else {
  17. page.find('span.version').text('');
  18. }
  19. page.find('span.score').html(app.score);
  20. page.find('p.description').text(app.description);
  21. page.find('img.preview').attr('src', app.preview);
  22. if (app.preview && app.preview.length) {
  23. page.find('img.preview').show();
  24. } else {
  25. page.find('img.preview').hide();
  26. }
  27. page.find('small.externalapp').attr('style', 'visibility:visible');
  28. page.find('span.author').text(app.author);
  29. // FIXME licenses of downloaded apps go into app.licence, licenses of not-downloaded apps into app.license
  30. var appLicense = '';
  31. if (typeof(app.licence) !== 'undefined') {
  32. appLicense = app.licence;
  33. } else if (typeof(app.license) !== 'undefined') {
  34. appLicense = app.license;
  35. }
  36. page.find('span.licence').text(appLicense);
  37. if (app.update !== false) {
  38. page.find('input.update').show();
  39. page.find('input.update').data('appid', app.id);
  40. page.find('input.update').attr('value',t('settings', 'Update to {appversion}', {appversion:app.update}));
  41. } else {
  42. page.find('input.update').hide();
  43. }
  44. page.find('input.enable').show();
  45. page.find('input.enable').val((app.active) ? t('settings', 'Disable') : t('settings', 'Enable'));
  46. page.find('input.enable').data('appid', app.id);
  47. page.find('input.enable').data('active', app.active);
  48. if (app.internal === false) {
  49. page.find('span.score').show();
  50. page.find('p.appslink').show();
  51. page.find('a').attr('href', 'http://apps.owncloud.com/content/show.php?content=' + app.id);
  52. page.find('small.externalapp').hide();
  53. } else {
  54. page.find('p.appslink').hide();
  55. page.find('span.score').hide();
  56. }
  57. if (typeof($('#leftcontent li[data-id="'+app.id+'"]').data('errormsg')) !== "undefined") {
  58. page.find(".warning").show();
  59. page.find(".warning").text($('#leftcontent li[data-id="'+app.id+'"]').data('errormsg'));
  60. } else {
  61. page.find(".warning").hide();
  62. }
  63. },
  64. enableApp:function(appid, active, element) {
  65. console.log('enableApp:', appid, active, element);
  66. var appitem=$('#leftcontent li[data-id="'+appid+'"]');
  67. element.val(t('settings','Please wait....'));
  68. if(active) {
  69. $.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appid},function(result) {
  70. if(!result || result.status !== 'success') {
  71. if (result.data && result.data.message) {
  72. OC.Settings.Apps.showErrorMessage(result.data.message);
  73. appitem.data('errormsg', result.data.message);
  74. } else {
  75. OC.Settings.Apps.showErrorMessage(t('settings', 'Error while disabling app'));
  76. appitem.data('errormsg', t('settings', 'Error while disabling app'));
  77. }
  78. element.val(t('settings','Disable'));
  79. appitem.addClass('appwarning');
  80. }
  81. else {
  82. appitem.data('active',false);
  83. element.data('active',false);
  84. OC.Settings.Apps.removeNavigation(appid);
  85. appitem.removeClass('active');
  86. element.val(t('settings','Enable'));
  87. }
  88. },'json');
  89. } else {
  90. $.post(OC.filePath('settings','ajax','enableapp.php'),{appid:appid},function(result) {
  91. if(!result || result.status !== 'success') {
  92. if (result.data && result.data.message) {
  93. OC.Settings.Apps.showErrorMessage(result.data.message);
  94. appitem.data('errormsg', result.data.message);
  95. } else {
  96. OC.Settings.Apps.showErrorMessage(t('settings', 'Error while enabling app'));
  97. appitem.data('errormsg', t('settings', 'Error while disabling app'));
  98. }
  99. element.val(t('settings','Enable'));
  100. appitem.addClass('appwarning');
  101. } else {
  102. OC.Settings.Apps.addNavigation(appid);
  103. appitem.data('active',true);
  104. element.data('active',true);
  105. appitem.addClass('active');
  106. element.val(t('settings','Disable'));
  107. }
  108. },'json')
  109. .fail(function() {
  110. OC.Settings.Apps.showErrorMessage(t('settings', 'Error while enabling app'));
  111. appitem.data('errormsg', t('settings', 'Error while enabling app'));
  112. appitem.data('active',false);
  113. appitem.addClass('appwarning');
  114. OC.Settings.Apps.removeNavigation(appid);
  115. element.val(t('settings','Enable'));
  116. });
  117. }
  118. },
  119. updateApp:function(appid, element) {
  120. console.log('updateApp:', appid, element);
  121. element.val(t('settings','Updating....'));
  122. $.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appid},function(result) {
  123. if(!result || result.status !== 'success') {
  124. OC.Settings.Apps.showErrorMessage(t('settings','Error while updating app'),t('settings','Error'));
  125. element.val(t('settings','Update'));
  126. }
  127. else {
  128. element.val(t('settings','Updated'));
  129. element.hide();
  130. }
  131. },'json');
  132. },
  133. insertApp:function(appdata) {
  134. var applist = $('#leftcontent li');
  135. var app =
  136. $('<li data-id="' + appdata.id + '" data-type="external" data-installed="0">'
  137. + '<a class="app externalapp" href="' + OC.filePath('settings', 'apps', 'index.php') + '&appid=' + appdata.id+'">'
  138. + appdata.name+'</a><small class="externalapp list">3rd party</small></li>');
  139. app.data('app', appdata);
  140. var added = false;
  141. applist.each(function() {
  142. if(!parseInt($(this).data('installed')) && $(this).find('a').text().toLowerCase() > appdata.name.toLowerCase()) {
  143. $(this).before(app);
  144. added = true;
  145. return false; // dang, remember this to get out of loop
  146. }
  147. });
  148. if(!added) {
  149. applist.last().after(app);
  150. }
  151. return app;
  152. },
  153. removeNavigation: function(appid){
  154. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  155. if(response.status === 'success'){
  156. var navIds=response.nav_ids;
  157. for(var i=0; i< navIds.length; i++){
  158. $('#apps .wrapper').children('li[data-id="'+navIds[i]+'"]').remove();
  159. }
  160. }
  161. });
  162. },
  163. addNavigation: function(appid){
  164. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  165. if(response.status === 'success'){
  166. var navEntries=response.nav_entries;
  167. for(var i=0; i< navEntries.length; i++){
  168. var entry = navEntries[i];
  169. var container = $('#apps .wrapper');
  170. if(container.children('li[data-id="'+entry.id+'"]').length === 0){
  171. var li=$('<li></li>');
  172. li.attr('data-id', entry.id);
  173. var img= $('<img class="icon"/>').attr({ src: entry.icon});
  174. var a=$('<a></a>').attr('href', entry.href);
  175. var filename=$('<span></span>');
  176. filename.text(entry.name);
  177. a.prepend(filename);
  178. a.prepend(img);
  179. li.append(a);
  180. // append the new app as last item in the list (.push is from sticky footer)
  181. $('#apps .wrapper .push').before(li);
  182. // scroll the app navigation down so the newly added app is seen
  183. $('#navigation').animate({ scrollTop: $('#navigation').height() }, 'slow');
  184. // draw attention to the newly added app entry by flashing it twice
  185. container.children('li[data-id="'+entry.id+'"]').animate({opacity:.3}).animate({opacity:1}).animate({opacity:.3}).animate({opacity:1});
  186. if (!SVGSupport() && entry.icon.match(/\.svg$/i)) {
  187. $(img).addClass('svg');
  188. replaceSVG();
  189. }
  190. }
  191. }
  192. }
  193. });
  194. },
  195. showErrorMessage: function(message) {
  196. $('.appinfo .warning').show();
  197. $('.appinfo .warning').text(message);
  198. }
  199. };
  200. $(document).ready(function(){
  201. $('#leftcontent li').each(function(index,li){
  202. var app = OC.get('appData_'+$(li).data('id'));
  203. $(li).data('app',app);
  204. $(this).find('span.hidden').remove();
  205. });
  206. $('#leftcontent li').keydown(function(event) {
  207. if (event.which === 13 || event.which === 32) {
  208. $(event.target).click();
  209. }
  210. return false;
  211. });
  212. $(document).on('click', '#leftcontent', function(event){
  213. var tgt = $(event.target);
  214. if (tgt.is('li') || tgt.is('a')) {
  215. var item = tgt.is('li') ? $(tgt) : $(tgt).parent();
  216. var app = item.data('app');
  217. OC.Settings.Apps.loadApp(app);
  218. }
  219. return false;
  220. });
  221. $('#rightcontent input.enable').click(function(){
  222. var element = $(this);
  223. var appid=$(this).data('appid');
  224. var active=$(this).data('active');
  225. if(appid) {
  226. OC.Settings.Apps.enableApp(appid, active, element);
  227. }
  228. });
  229. $('#rightcontent input.update').click(function(){
  230. var element = $(this);
  231. var appid=$(this).data('appid');
  232. if(appid) {
  233. OC.Settings.Apps.updateApp(appid, element);
  234. }
  235. });
  236. if(appid) {
  237. var item = $('#leftcontent li[data-id="'+appid+'"]');
  238. if(item) {
  239. item.trigger('click');
  240. item.addClass('active');
  241. $('#leftcontent').animate({scrollTop: $(item).offset().top-70}, 'slow','swing');
  242. }
  243. }
  244. });