oc-vcategories.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. var OCCategories= {
  2. category_favorites:'_$!<Favorite>!$_',
  3. edit:function(type, cb) {
  4. if(!type && !this.type) {
  5. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  6. }
  7. type = type ? type : this.type;
  8. $('body').append('<div id="category_dialog"></div>');
  9. $('#category_dialog').load(
  10. OC.filePath('core', 'ajax', 'vcategories/edit.php') + '?type=' + type, function(response) {
  11. try {
  12. var jsondata = jQuery.parseJSON(response);
  13. if(response.status == 'error') {
  14. OC.dialogs.alert(response.data.message, 'Error');
  15. return;
  16. }
  17. } catch(e) {
  18. var setEnabled = function(d, enable) {
  19. if(enable) {
  20. d.css('cursor', 'default').find('input,button:not(#category_addbutton)')
  21. .prop('disabled', false).css('cursor', 'default');
  22. } else {
  23. d.css('cursor', 'wait').find('input,button:not(#category_addbutton)')
  24. .prop('disabled', true).css('cursor', 'wait');
  25. }
  26. }
  27. var dlg = $('#edit_categories_dialog').dialog({
  28. modal: true,
  29. height: 350, minHeight:200, width: 250, minWidth: 200,
  30. buttons: {
  31. 'Close': function() {
  32. $(this).dialog('close');
  33. },
  34. 'Delete':function() {
  35. var categories = $('#categorylist').find('input:checkbox').serialize();
  36. setEnabled(dlg, false);
  37. OCCategories.doDelete(categories, function() {
  38. setEnabled(dlg, true);
  39. });
  40. },
  41. 'Rescan':function() {
  42. setEnabled(dlg, false);
  43. OCCategories.rescan(function() {
  44. setEnabled(dlg, true);
  45. });
  46. }
  47. },
  48. close : function(event, ui) {
  49. $(this).dialog('destroy').remove();
  50. $('#category_dialog').remove();
  51. },
  52. open : function(event, ui) {
  53. $('#category_addinput').on('input',function() {
  54. if($(this).val().length > 0) {
  55. $('#category_addbutton').removeAttr('disabled');
  56. }
  57. });
  58. $('#categoryform').submit(function() {
  59. OCCategories.add($('#category_addinput').val());
  60. $('#category_addinput').val('');
  61. $('#category_addbutton').attr('disabled', 'disabled');
  62. return false;
  63. });
  64. $('#category_addbutton').on('click',function(e) {
  65. e.preventDefault();
  66. if($('#category_addinput').val().length > 0) {
  67. OCCategories.add($('#category_addinput').val());
  68. $('#category_addinput').val('');
  69. }
  70. });
  71. }
  72. });
  73. }
  74. });
  75. },
  76. _processDeleteResult:function(jsondata) {
  77. if(jsondata.status == 'success') {
  78. OCCategories._update(jsondata.data.categories);
  79. } else {
  80. OC.dialogs.alert(jsondata.data.message, 'Error');
  81. }
  82. },
  83. favorites:function(type, cb) {
  84. if(!type && !this.type) {
  85. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  86. }
  87. type = type ? type : this.type;
  88. $.getJSON(OC.filePath('core', 'ajax', 'categories/favorites.php'), {type: type},function(jsondata) {
  89. if(typeof cb == 'function') {
  90. cb(jsondata);
  91. } else {
  92. if(jsondata.status === 'success') {
  93. OCCategories._update(jsondata.data.categories);
  94. } else {
  95. OC.dialogs.alert(jsondata.data.message, t('core', 'Error'));
  96. }
  97. }
  98. });
  99. },
  100. addToFavorites:function(id, type, cb) {
  101. if(!type && !this.type) {
  102. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  103. }
  104. type = type ? type : this.type;
  105. $.post(OC.filePath('core', 'ajax', 'vcategories/addToFavorites.php'), {id:id, type:type}, function(jsondata) {
  106. if(typeof cb == 'function') {
  107. cb(jsondata);
  108. } else {
  109. if(jsondata.status !== 'success') {
  110. OC.dialogs.alert(jsondata.data.message, 'Error');
  111. }
  112. }
  113. });
  114. },
  115. removeFromFavorites:function(id, type, cb) {
  116. if(!type && !this.type) {
  117. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  118. }
  119. type = type ? type : this.type;
  120. $.post(OC.filePath('core', 'ajax', 'vcategories/removeFromFavorites.php'), {id:id, type:type}, function(jsondata) {
  121. if(typeof cb == 'function') {
  122. cb(jsondata);
  123. } else {
  124. if(jsondata.status !== 'success') {
  125. OC.dialogs.alert(jsondata.data.message, t('core', 'Error'));
  126. }
  127. }
  128. });
  129. },
  130. doDelete:function(categories, type, cb) {
  131. if(!type && !this.type) {
  132. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  133. }
  134. type = type ? type : this.type;
  135. if(categories == '' || categories == undefined) {
  136. OC.dialogs.alert(t('core', 'No categories selected for deletion.'), t('core', 'Error'));
  137. return false;
  138. }
  139. var self = this;
  140. var q = categories + '&type=' + type;
  141. if(this.app) {
  142. q += '&app=' + this.app;
  143. $.post(OC.filePath(this.app, 'ajax', 'categories/delete.php'), q, function(jsondata) {
  144. if(typeof cb == 'function') {
  145. cb(jsondata);
  146. } else {
  147. self._processDeleteResult(jsondata);
  148. }
  149. });
  150. } else {
  151. $.post(OC.filePath('core', 'ajax', 'vcategories/delete.php'), q, function(jsondata) {
  152. if(typeof cb == 'function') {
  153. cb(jsondata);
  154. } else {
  155. self._processDeleteResult(jsondata);
  156. }
  157. });
  158. }
  159. },
  160. add:function(category, type, cb) {
  161. if(!type && !this.type) {
  162. throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
  163. }
  164. type = type ? type : this.type;
  165. $.post(OC.filePath('core', 'ajax', 'vcategories/add.php'),{'category':category, 'type':type},function(jsondata) {
  166. if(typeof cb == 'function') {
  167. cb(jsondata);
  168. } else {
  169. if(jsondata.status === 'success') {
  170. OCCategories._update(jsondata.data.categories);
  171. } else {
  172. OC.dialogs.alert(jsondata.data.message, 'Error');
  173. }
  174. }
  175. });
  176. },
  177. rescan:function(app, cb) {
  178. if(!app && !this.app) {
  179. throw { name: 'MissingParameter', message: t('core', 'The app name is not specified.') };
  180. }
  181. app = app ? app : this.app;
  182. $.getJSON(OC.filePath(app, 'ajax', 'categories/rescan.php'),function(jsondata, status, xhr) {
  183. if(typeof cb == 'function') {
  184. cb(jsondata);
  185. } else {
  186. if(jsondata.status === 'success') {
  187. OCCategories._update(jsondata.data.categories);
  188. } else {
  189. OC.dialogs.alert(jsondata.data.message, 'Error');
  190. }
  191. }
  192. }).error(function(xhr){
  193. if (xhr.status == 404) {
  194. var errormessage = t('core', 'The required file {file} is not installed!',
  195. {file: OC.filePath(app, 'ajax', 'categories/rescan.php')}, t('core', 'Error'));
  196. if(typeof cb == 'function') {
  197. cb({status:'error', data:{message:errormessage}});
  198. } else {
  199. OC.dialogs.alert(errormessage);
  200. }
  201. }
  202. });
  203. },
  204. _update:function(categories) {
  205. var categorylist = $('#categorylist');
  206. categorylist.find('li').remove();
  207. for(var category in categories) {
  208. var item = '<li><input type="checkbox" name="categories" value="' + categories[category] + '" />' + categories[category] + '</li>';
  209. $(item).appendTo(categorylist);
  210. }
  211. if(typeof OCCategories.changed === 'function') {
  212. OCCategories.changed(categories);
  213. }
  214. }
  215. }