apps.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 = $('#app-content');
  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. var userDocumentation = false;
  38. var adminDocumentation = false;
  39. if (typeof(app.documentation) !== 'undefined') {
  40. if (typeof(app.documentation.user) !== 'undefined') {
  41. userDocumentation = true;
  42. page.find('span.userDocumentation').html("<a id='userDocumentation' href='" + app.documentation.user + "'>" + t('settings', 'User Documentation') + "</a>");
  43. page.find('p.documentation').show();
  44. }
  45. if (typeof(app.documentation.admin) !== 'undefined') {
  46. adminDocumentation = true;
  47. page.find('span.adminDocumentation').html("<a id='adminDocumentation' href='" + app.documentation.admin + "'>" + t('settings', 'Admin Documentation') + "</a>");
  48. page.find('p.documentation').show();
  49. }
  50. if(userDocumentation && adminDocumentation) {
  51. page.find('span.userDocumentation').after(', ');
  52. }
  53. }
  54. if (typeof(app.website) !== 'undefined') {
  55. page.find('p.website').show();
  56. page.find('a#websitelink').attr('href', app.website);
  57. }
  58. if (app.update !== false) {
  59. page.find('input.update').show();
  60. page.find('input.update').data('appid', app.id);
  61. page.find('input.update').attr('value',t('settings', 'Update to {appversion}', {appversion:app.update}));
  62. } else {
  63. page.find('input.update').hide();
  64. }
  65. page.find('input.enable').show();
  66. page.find('input.enable').val((app.active) ? t('settings', 'Disable') : t('settings', 'Enable'));
  67. page.find('input.enable').data('appid', app.id);
  68. page.find('input.enable').data('active', app.active);
  69. if (app.internal === false) {
  70. page.find('span.score').show();
  71. page.find('p.appstore').show();
  72. page.find('a#appstorelink').attr('href', 'http://apps.owncloud.com/content/show.php?content=' + app.id);
  73. page.find('small.externalapp').hide();
  74. } else {
  75. page.find('p.appslink').hide();
  76. page.find('span.score').hide();
  77. }
  78. if (typeof($('#app-navigation ul li[data-id="'+app.id+'"]').data('errormsg')) !== "undefined") {
  79. page.find(".warning").show();
  80. page.find(".warning").text($('#app-navigation ul li[data-id="'+app.id+'"]').data('errormsg'));
  81. } else {
  82. page.find(".warning").hide();
  83. }
  84. },
  85. enableApp:function(appid, active, element) {
  86. console.log('enableApp:', appid, active, element);
  87. var appitem=$('#app-navigation ul li[data-id="'+appid+'"]');
  88. element.val(t('settings','Please wait....'));
  89. if(active) {
  90. $.post(OC.filePath('settings','ajax','disableapp.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 disabling app'));
  97. appitem.data('errormsg', t('settings', 'Error while disabling app'));
  98. }
  99. element.val(t('settings','Disable'));
  100. appitem.addClass('appwarning');
  101. }
  102. else {
  103. appitem.data('active',false);
  104. element.data('active',false);
  105. OC.Settings.Apps.removeNavigation(appid);
  106. appitem.removeClass('active');
  107. element.val(t('settings','Enable'));
  108. }
  109. },'json');
  110. } else {
  111. $.post(OC.filePath('settings','ajax','enableapp.php'),{appid:appid},function(result) {
  112. if(!result || result.status !== 'success') {
  113. if (result.data && result.data.message) {
  114. OC.Settings.Apps.showErrorMessage(result.data.message);
  115. appitem.data('errormsg', result.data.message);
  116. } else {
  117. OC.Settings.Apps.showErrorMessage(t('settings', 'Error while enabling app'));
  118. appitem.data('errormsg', t('settings', 'Error while disabling app'));
  119. }
  120. element.val(t('settings','Enable'));
  121. appitem.addClass('appwarning');
  122. } else {
  123. OC.Settings.Apps.addNavigation(appid);
  124. appitem.data('active',true);
  125. element.data('active',true);
  126. appitem.addClass('active');
  127. element.val(t('settings','Disable'));
  128. }
  129. },'json')
  130. .fail(function() {
  131. OC.Settings.Apps.showErrorMessage(t('settings', 'Error while enabling app'));
  132. appitem.data('errormsg', t('settings', 'Error while enabling app'));
  133. appitem.data('active',false);
  134. appitem.addClass('appwarning');
  135. OC.Settings.Apps.removeNavigation(appid);
  136. element.val(t('settings','Enable'));
  137. });
  138. }
  139. },
  140. updateApp:function(appid, element) {
  141. console.log('updateApp:', appid, element);
  142. element.val(t('settings','Updating....'));
  143. $.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appid},function(result) {
  144. if(!result || result.status !== 'success') {
  145. OC.Settings.Apps.showErrorMessage(t('settings','Error while updating app'),t('settings','Error'));
  146. element.val(t('settings','Update'));
  147. }
  148. else {
  149. element.val(t('settings','Updated'));
  150. element.hide();
  151. }
  152. },'json');
  153. },
  154. insertApp:function(appdata) {
  155. var applist = $('#app-navigation ul li');
  156. var app =
  157. $('<li data-id="' + appdata.id + '" data-type="external" data-installed="0">'
  158. + '<a class="app externalapp" href="' + OC.filePath('settings', 'apps', 'index.php') + '&appid=' + appdata.id+'">'
  159. + appdata.name+'</a><small class="externalapp list">3rd party</small></li>');
  160. app.data('app', appdata);
  161. var added = false;
  162. applist.each(function() {
  163. if(!parseInt($(this).data('installed')) && $(this).find('a').text().toLowerCase() > appdata.name.toLowerCase()) {
  164. $(this).before(app);
  165. added = true;
  166. return false; // dang, remember this to get out of loop
  167. }
  168. });
  169. if(!added) {
  170. applist.last().after(app);
  171. }
  172. return app;
  173. },
  174. removeNavigation: function(appid){
  175. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  176. if(response.status === 'success'){
  177. var navIds=response.nav_ids;
  178. for(var i=0; i< navIds.length; i++){
  179. $('#apps .wrapper').children('li[data-id="'+navIds[i]+'"]').remove();
  180. }
  181. }
  182. });
  183. },
  184. addNavigation: function(appid){
  185. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  186. if(response.status === 'success'){
  187. var navEntries=response.nav_entries;
  188. for(var i=0; i< navEntries.length; i++){
  189. var entry = navEntries[i];
  190. var container = $('#apps .wrapper');
  191. if(container.children('li[data-id="'+entry.id+'"]').length === 0){
  192. var li=$('<li></li>');
  193. li.attr('data-id', entry.id);
  194. var img= $('<img class="icon"/>').attr({ src: entry.icon});
  195. var a=$('<a></a>').attr('href', entry.href);
  196. var filename=$('<span></span>');
  197. filename.text(entry.name);
  198. a.prepend(filename);
  199. a.prepend(img);
  200. li.append(a);
  201. // append the new app as last item in the list
  202. // (.push is from sticky footer)
  203. $('#apps .wrapper .push').before(li);
  204. // scroll the app navigation down
  205. // so the newly added app is seen
  206. $('#navigation').animate({
  207. scrollTop: $('#navigation').height()
  208. }, 'slow');
  209. // draw attention to the newly added app entry
  210. // by flashing it twice
  211. container.children('li[data-id="' + entry.id + '"]')
  212. .animate({opacity: 0.3})
  213. .animate({opacity: 1})
  214. .animate({opacity: 0.3})
  215. .animate({opacity: 1});
  216. if (!SVGSupport() && entry.icon.match(/\.svg$/i)) {
  217. $(img).addClass('svg');
  218. replaceSVG();
  219. }
  220. }
  221. }
  222. }
  223. });
  224. },
  225. showErrorMessage: function(message) {
  226. $('.appinfo .warning').show();
  227. $('.appinfo .warning').text(message);
  228. }
  229. };
  230. $(document).ready(function(){
  231. $('#app-navigation ul li').each(function(index,li){
  232. var app = OC.get('appData_'+$(li).data('id'));
  233. $(li).data('app',app);
  234. $(this).find('span.hidden').remove();
  235. });
  236. $('#app-navigation ul li').keydown(function(event) {
  237. if (event.which === 13 || event.which === 32) {
  238. $(event.target).click();
  239. }
  240. return false;
  241. });
  242. $(document).on('click', '#app-navigation', function(event){
  243. var tgt = $(event.target);
  244. if (tgt.is('li') || tgt.is('a')) {
  245. var item = tgt.is('li') ? $(tgt) : $(tgt).parent();
  246. var app = item.data('app');
  247. OC.Settings.Apps.loadApp(app);
  248. $('#app-navigation .selected').removeClass('selected');
  249. item.addClass('selected');
  250. }
  251. return false;
  252. });
  253. $('#app-content input.enable').click(function(){
  254. var element = $(this);
  255. var appid=$(this).data('appid');
  256. var active=$(this).data('active');
  257. if(appid) {
  258. OC.Settings.Apps.enableApp(appid, active, element);
  259. }
  260. });
  261. $('#app-content input.update').click(function(){
  262. var element = $(this);
  263. var appid=$(this).data('appid');
  264. if(appid) {
  265. OC.Settings.Apps.updateApp(appid, element);
  266. }
  267. });
  268. if(appid) {
  269. var item = $('#app-navigation ul li[data-id="'+appid+'"]');
  270. if(item) {
  271. item.trigger('click');
  272. item.addClass('active');
  273. $('#app-navigation').animate({scrollTop: $(item).offset().top-70}, 'slow','swing');
  274. }
  275. }
  276. });