apps.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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').html(app.description);
  21. page.find('img.preview').attr('src', app.preview);
  22. page.find('small.externalapp').attr('style', 'visibility:visible');
  23. page.find('span.author').text(app.author);
  24. page.find('span.licence').text(app.licence);
  25. page.find('input.enable').show();
  26. page.find('input.enable').val((app.active) ? t('settings', 'Disable') : t('settings', 'Enable'));
  27. page.find('input.enable').data('appid', app.id);
  28. page.find('input.enable').data('active', app.active);
  29. if (app.internal == false) {
  30. page.find('span.score').show();
  31. page.find('p.appslink').show();
  32. page.find('a').attr('href', 'http://apps.owncloud.com/content/show.php?content=' + app.id);
  33. page.find('small.externalapp').hide();
  34. } else {
  35. page.find('p.appslink').hide();
  36. page.find('span.score').hide();
  37. }
  38. },
  39. enableApp:function(appid, active, element) {
  40. console.log('enableApp:', appid, active, element);
  41. var appitem=$('#leftcontent li[data-id="'+appid+'"]');
  42. appData = appitem.data('app');
  43. appData.active = !active;
  44. appitem.data('app', appData);
  45. if(active) {
  46. $.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appid},function(result) {
  47. if(!result || result.status!='success') {
  48. OC.dialogs.alert('Error while disabling app','Error');
  49. }
  50. else {
  51. element.data('active',false);
  52. OC.Settings.Apps.removeNavigation(appid);
  53. element.val(t('settings','Enable'));
  54. }
  55. },'json');
  56. $('#leftcontent li[data-id="'+appid+'"]').removeClass('active');
  57. } else {
  58. $.post(OC.filePath('settings','ajax','enableapp.php'),{appid:appid},function(result) {
  59. if(!result || result.status!='success') {
  60. OC.dialogs.alert('Error while enabling app','Error');
  61. }
  62. else {
  63. OC.Settings.Apps.addNavigation(appid);
  64. element.data('active',true);
  65. element.val(t('settings','Disable'));
  66. }
  67. },'json');
  68. $('#leftcontent li[data-id="'+appid+'"]').addClass('active');
  69. }
  70. },
  71. insertApp:function(appdata) {
  72. var applist = $('#leftcontent li');
  73. var app =
  74. $('<li data-id="' + appdata.id + '" data-type="external" data-installed="0">'
  75. + '<a class="app externalapp" href="' + OC.filePath('settings', 'apps', 'index.php') + '&appid=' + appdata.id+'">'
  76. + appdata.name+'</a><small class="externalapp list">3rd party</small></li>');
  77. app.data('app', appdata);
  78. var added = false;
  79. applist.each(function() {
  80. if(!parseInt($(this).data('installed')) && $(this).find('a').text().toLowerCase() > appdata.name.toLowerCase()) {
  81. $(this).before(app);
  82. added = true;
  83. return false; // dang, remember this to get out of loop
  84. }
  85. });
  86. if(!added) {
  87. applist.last().after(app);
  88. }
  89. return app;
  90. },
  91. removeNavigation: function(appid){
  92. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  93. if(response.status === 'success'){
  94. var navIds=response.nav_ids;
  95. for(var i=0; i< navIds.length; i++){
  96. $('#apps').children('li[data-id="'+navIds[i]+'"]').remove();
  97. }
  98. }
  99. });
  100. },
  101. addNavigation: function(appid){
  102. $.getJSON(OC.filePath('settings', 'ajax', 'navigationdetect.php'), {app: appid}).done(function(response){
  103. if(response.status === 'success'){
  104. var navEntries=response.nav_entries;
  105. for(var i=0; i< navEntries.length; i++){
  106. var entry = navEntries[i];
  107. var container = $('#apps');
  108. if(container.children('li[data-id="'+entry.id+'"]').length === 0){
  109. var li=$('<li></li>');
  110. li.attr('data-id', entry.id);
  111. var a=$('<a></a>');
  112. a.attr('style', 'background-image: url('+entry.icon+')');
  113. a.text(entry.name);
  114. a.attr('href', entry.href);
  115. li.append(a);
  116. container.append(li);
  117. }
  118. }
  119. }
  120. });
  121. }
  122. };
  123. $(document).ready(function(){
  124. $('#leftcontent li').each(function(index,li){
  125. var app = OC.get('appData_'+$(li).data('id'));
  126. $(li).data('app',app);
  127. $(this).find('span.hidden').remove();
  128. });
  129. $('#leftcontent li').keydown(function(event) {
  130. if (event.which == 13 || event.which == 32) {
  131. $(event.target).click();
  132. }
  133. return false;
  134. });
  135. $(document).on('click', '#leftcontent', function(event){
  136. var tgt = $(event.target);
  137. if (tgt.is('li') || tgt.is('a')) {
  138. var item = tgt.is('li') ? $(tgt) : $(tgt).parent();
  139. var app = item.data('app');
  140. OC.Settings.Apps.loadApp(app);
  141. }
  142. return false;
  143. });
  144. $('#rightcontent input.enable').click(function(){
  145. var element = $(this);
  146. var appid=$(this).data('appid');
  147. var active=$(this).data('active');
  148. if(appid) {
  149. OC.Settings.Apps.enableApp(appid, active, element);
  150. }
  151. });
  152. if(appid) {
  153. var item = $('#leftcontent li[data-id="'+appid+'"]');
  154. if(item) {
  155. item.trigger('click');
  156. item.addClass('active');
  157. $('#leftcontent').animate({scrollTop: $(item).offset().top-70}, 'slow','swing');
  158. }
  159. }
  160. });