You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tags.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. OC.Tags= {
  2. edit:function(type, cb) {
  3. if(!type && !this.type) {
  4. throw {
  5. name: 'MissingParameter',
  6. message: t(
  7. 'core',
  8. 'The object type is not specified.'
  9. )
  10. };
  11. }
  12. type = type ? type : this.type;
  13. var self = this;
  14. $.when(this._getTemplate()).then(function($tmpl) {
  15. if(self.$dialog) {
  16. self.$dialog.ocdialog('close');
  17. }
  18. self.$dialog = $tmpl.octemplate({
  19. addText: t('core', 'Enter new')
  20. });
  21. $('body').append(self.$dialog);
  22. self.$dialog.ready(function() {
  23. self.$taglist = self.$dialog.find('.taglist');
  24. self.$taginput = self.$dialog.find('.addinput');
  25. self.$taglist.on('change', 'input:checkbox', function(event) {
  26. self._handleChanges(self.$taglist, self.$taginput);
  27. });
  28. self.$taginput.on('input', function(event) {
  29. self._handleChanges(self.$taglist, self.$taginput);
  30. });
  31. self.deleteButton = {
  32. text: t('core', 'Delete'),
  33. click: function() {
  34. self._deleteTags(
  35. self,
  36. type,
  37. self._selectedIds()
  38. );
  39. }
  40. };
  41. self.addButton = {
  42. text: t('core', 'Add'),
  43. click: function() {
  44. self._addTag(
  45. self,
  46. type,
  47. self.$taginput.val()
  48. );
  49. }
  50. };
  51. self._fillTagList(type, self.$taglist);
  52. });
  53. self.$dialog.ocdialog({
  54. title: t('core', 'Edit tags'),
  55. closeOnEscape: true,
  56. width: 250,
  57. height: 'auto',
  58. modal: true,
  59. //buttons: buttonlist,
  60. close: function(event, ui) {
  61. try {
  62. $(this).ocdialog('destroy').remove();
  63. } catch(e) {console.warn(e);}
  64. self.$dialog = null;
  65. }
  66. });
  67. })
  68. .fail(function(status, error) {
  69. // If the method is called while navigating away
  70. // from the page, it is probably not needed ;)
  71. if(status !== 0) {
  72. alert(t('core', 'Error loading dialog template: {error}', {error: error}));
  73. }
  74. });
  75. },
  76. /**
  77. * @param {string} type
  78. * @param {string} tag
  79. * @return jQuery.Promise which resolves with an array of ids
  80. */
  81. getIdsForTag:function(type, tag) {
  82. if(!type && !this.type) {
  83. throw new Error('The object type is not specified.');
  84. }
  85. type = type ? type : this.type;
  86. var defer = $.Deferred(),
  87. url = OC.generateUrl('/tags/{type}/ids', {type: type});
  88. $.getJSON(url, {tag: tag}, function(response) {
  89. if(response.status === 'success') {
  90. defer.resolve(response.ids);
  91. } else {
  92. defer.reject(response);
  93. }
  94. });
  95. return defer.promise();
  96. },
  97. /**
  98. * @param {string} type
  99. * @return {*} jQuery.Promise which resolves with an array of ids
  100. */
  101. getFavorites:function(type) {
  102. if(!type && !this.type) {
  103. throw new Error('The object type is not specified.');
  104. }
  105. type = type ? type : this.type;
  106. var defer = $.Deferred(),
  107. url = OC.generateUrl('/tags/{type}/favorites', {type: type});
  108. $.getJSON(url, function(response) {
  109. if(response.status === 'success') {
  110. defer.resolve(response.ids);
  111. } else {
  112. defer.reject(response);
  113. }
  114. });
  115. return defer.promise();
  116. },
  117. /**
  118. * @param {string} type
  119. * @return {*} jQuery.Promise which resolves with an array of id/name objects
  120. */
  121. getTags:function(type) {
  122. if(!type && !this.type) {
  123. throw new Error('The object type is not specified.');
  124. }
  125. type = type ? type : this.type;
  126. var defer = $.Deferred(),
  127. url = OC.generateUrl('/tags/{type}', {type: type});
  128. $.getJSON(url, function(response) {
  129. if(response.status === 'success') {
  130. defer.resolve(response.tags);
  131. } else {
  132. defer.reject(response);
  133. }
  134. });
  135. return defer.promise();
  136. },
  137. /**
  138. * @param {number} id
  139. * @param {string} tag
  140. * @param {string} type
  141. * @return {*} jQuery.Promise
  142. */
  143. tagAs:function(id, tag, type) {
  144. if(!type && !this.type) {
  145. throw new Error('The object type is not specified.');
  146. }
  147. type = type ? type : this.type;
  148. var defer = $.Deferred(),
  149. url = OC.generateUrl('/tags/{type}/tag/{id}/', {type: type, id: id});
  150. $.post(url, {tag: tag}, function(response) {
  151. if(response.status === 'success') {
  152. defer.resolve(response);
  153. } else {
  154. defer.reject(response);
  155. }
  156. }).fail(function(jqXHR, textStatus, errorThrown) {
  157. defer.reject(jqXHR.status, errorThrown);
  158. });
  159. return defer.promise();
  160. },
  161. /**
  162. * @param {number} id
  163. * @param {string} tag
  164. * @param {string} type
  165. * @return {*} jQuery.Promise
  166. */
  167. unTag:function(id, tag, type) {
  168. if(!type && !this.type) {
  169. throw new Error('The object type is not specified.');
  170. }
  171. type = type ? type : this.type;
  172. var defer = $.Deferred(),
  173. self = this,
  174. url = OC.generateUrl('/tags/{type}/untag/{id}/', {type: type, id: id});
  175. $.post(url, {tag: tag}, function(response) {
  176. if(response.status === 'success') {
  177. defer.resolve(response);
  178. } else {
  179. defer.reject(response);
  180. }
  181. }).fail(function(jqXHR, textStatus, errorThrown) {
  182. defer.reject(jqXHR.status, errorThrown);
  183. });
  184. return defer.promise();
  185. },
  186. /**
  187. * @param {number} id
  188. * @param {string} type
  189. * @return {*} jQuery.Promise
  190. */
  191. addToFavorites:function(id, type) {
  192. if(!type && !this.type) {
  193. throw new Error('The object type is not specified.');
  194. }
  195. type = type ? type : this.type;
  196. var defer = $.Deferred(),
  197. url = OC.generateUrl(
  198. '/tags/{type}/favorite/{id}/',
  199. {type: type, id: id}
  200. );
  201. $.post(url, function(response) {
  202. if(response.status === 'success') {
  203. defer.resolve(response);
  204. } else {
  205. defer.reject(response);
  206. }
  207. }).fail(function(jqXHR, textStatus, errorThrown) {
  208. defer.reject(jqXHR.status, errorThrown);
  209. });
  210. return defer.promise();
  211. },
  212. /**
  213. * @param {number} id
  214. * @param {string} type
  215. * @return {*} jQuery.Promise
  216. */
  217. removeFromFavorites:function(id, type) {
  218. if(!type && !this.type) {
  219. throw new Error('The object type is not specified.');
  220. }
  221. type = type ? type : this.type;
  222. var defer = $.Deferred(),
  223. url = OC.generateUrl(
  224. '/tags/{type}/unfavorite/{id}/',
  225. {type: type, id: id}
  226. );
  227. $.post(url, function(response) {
  228. if(response.status === 'success') {
  229. defer.resolve();
  230. } else {
  231. defer.reject(response);
  232. }
  233. }).fail(function(jqXHR, textStatus, errorThrown) {
  234. defer.reject(jqXHR.status, errorThrown);
  235. });
  236. return defer.promise();
  237. },
  238. /**
  239. * @param {string} tag
  240. * @param {string} type
  241. * @return {*} jQuery.Promise which resolves with an object with the name and the new id
  242. */
  243. addTag:function(tag, type) {
  244. if(!type && !this.type) {
  245. throw new Error('The object type is not specified.');
  246. }
  247. type = type ? type : this.type;
  248. var defer = $.Deferred(),
  249. url = OC.generateUrl('/tags/{type}/add', {type: type});
  250. $.post(url,{tag:tag}, function(response) {
  251. if(typeof cb == 'function') {
  252. cb(response);
  253. }
  254. if(response.status === 'success') {
  255. defer.resolve({id:response.id, name: tag});
  256. } else {
  257. defer.reject(response);
  258. }
  259. }).fail(function(jqXHR, textStatus, errorThrown) {
  260. defer.reject(jqXHR.status, errorThrown);
  261. });
  262. return defer.promise();
  263. },
  264. /**
  265. * @param {array} tags
  266. * @param {string} type
  267. * @return {*} jQuery.Promise
  268. */
  269. deleteTags:function(tags, type) {
  270. if(!type && !this.type) {
  271. throw new Error('The object type is not specified.');
  272. }
  273. type = type ? type : this.type;
  274. var defer = $.Deferred(),
  275. url = OC.generateUrl('/tags/{type}/delete', {type: type});
  276. if(!tags || !tags.length) {
  277. throw new Error(t('core', 'No tags selected for deletion.'));
  278. }
  279. $.post(url, {tags:tags}, function(response) {
  280. if(response.status === 'success') {
  281. defer.resolve(response.tags);
  282. } else {
  283. defer.reject(response);
  284. }
  285. }).fail(function(jqXHR, textStatus, errorThrown) {
  286. defer.reject(jqXHR.status, errorThrown);
  287. });
  288. return defer.promise();
  289. },
  290. _update:function(tags, type) {
  291. if(!this.$dialog) {
  292. return;
  293. }
  294. var $taglist = this.$dialog.find('.taglist'),
  295. self = this;
  296. $taglist.empty();
  297. $.each(tags, function(idx, tag) {
  298. var $item = self.$listTmpl.octemplate({id: tag.id, name: tag.name});
  299. $item.appendTo($taglist);
  300. });
  301. $(this).trigger('change', {type: type, tags: tags});
  302. if(typeof this.changed === 'function') {
  303. this.changed(tags);
  304. }
  305. },
  306. _getTemplate: function() {
  307. var defer = $.Deferred();
  308. if(!this.$template) {
  309. var self = this;
  310. $.get(OC.filePath('core', 'templates', 'tags.html'), function(tmpl) {
  311. self.$template = $(tmpl);
  312. self.$listTmpl = self.$template.find('.taglist li:first-child').detach();
  313. defer.resolve(self.$template);
  314. })
  315. .fail(function(jqXHR, textStatus, errorThrown) {
  316. defer.reject(jqXHR.status, errorThrown);
  317. });
  318. } else {
  319. defer.resolve(this.$template);
  320. }
  321. return defer.promise();
  322. },
  323. _fillTagList: function(type) {
  324. var self = this;
  325. $.when(this.getTags(type))
  326. .then(function(tags) {
  327. self._update(tags, type);
  328. })
  329. .fail(function(response) {
  330. console.warn(response);
  331. });
  332. },
  333. _selectedIds: function() {
  334. return $.map(this.$taglist.find('input:checked'), function(b) {return $(b).val();});
  335. },
  336. _handleChanges: function($list, $input) {
  337. var ids = this._selectedIds();
  338. var buttons = [];
  339. if($input.val().length) {
  340. buttons.push(this.addButton);
  341. }
  342. if(ids.length) {
  343. buttons.push(this.deleteButton);
  344. }
  345. this.$dialog.ocdialog('option', 'buttons', buttons);
  346. },
  347. _deleteTags: function(self, type, ids) {
  348. $.when(self.deleteTags(ids, type))
  349. .then(function() {
  350. self._fillTagList(type);
  351. self.$dialog.ocdialog('option', 'buttons', []);
  352. })
  353. .fail(function(response) {
  354. console.warn(response);
  355. });
  356. },
  357. _addTag: function(self, type, tag) {
  358. $.when(self.addTag(tag, type))
  359. .then(function(tag) {
  360. self._fillTagList(type);
  361. self.$taginput.val('').trigger('input');
  362. })
  363. .fail(function(response) {
  364. console.warn(response);
  365. });
  366. }
  367. };