PageRenderTime 71ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/files/js/NewsEntryListEdit.class.js

https://github.com/WCFSolutions/com.wcfsolutions.wsip
JavaScript | 444 lines | 298 code | 49 blank | 97 comment | 82 complexity | 27a7683eff3f235feae6e9203293ec5c MD5 | raw file
Possible License(s): LGPL-3.0
  1. /**
  2. * @author Sebastian Oettl
  3. * @copyright 2009-2012 WCF Solutions <http://www.wcfsolutions.com/>
  4. * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  5. */
  6. var NewsEntryListEdit = Class.create({
  7. /**
  8. * Inits NewsEntryListEdit.
  9. */
  10. initialize: function(data, count) {
  11. this.data = data;
  12. this.count = count;
  13. this.options = Object.extend({
  14. page: '',
  15. url: '',
  16. categoryID: 0,
  17. entryID: 0,
  18. enableRecycleBin: true
  19. }, arguments[2] || { });
  20. // get parent object
  21. this.parentObject = new InlineListEdit('newsEntry', this);
  22. },
  23. /**
  24. * Initialises special news entry options.
  25. */
  26. initItem: function(id) {
  27. // init subject edit
  28. if (permissions['canEditNewsEntry']) {
  29. var entrySubjectDiv = $('newsEntryTitle'+id);
  30. if (entrySubjectDiv) {
  31. entrySubjectDiv.observe('dblclick', function(id) { this.startTitleEdit(id); }.bind(this, id));
  32. }
  33. }
  34. },
  35. /**
  36. * Show the status of a news entry.
  37. */
  38. showStatus: function(id) {
  39. var entry = this.data.get(id);
  40. // get row
  41. var row = $('newsEntryRow'+id);
  42. // update css class
  43. if (row) {
  44. // remove all classes
  45. row.removeClassName('marked');
  46. row.removeClassName('disabled');
  47. row.removeClassName('deleted');
  48. // disabled
  49. if (entry.isDisabled) {
  50. row.addClassName('disabled');
  51. }
  52. // deleted
  53. if (entry.isDeleted) {
  54. row.addClassName('deleted');
  55. }
  56. // marked
  57. if (entry.isMarked) {
  58. row.addClassName('marked');
  59. }
  60. }
  61. // update icon
  62. var icon = $('newsEntryEdit'+id);
  63. if (icon && icon.src != undefined) {
  64. // deleted
  65. if (entry.isDeleted) {
  66. icon.src = icon.src.replace(/[a-z0-9-_]*?(?=(?:Options)?(?:S|M|L|XL)\.png$)/i, 'newsEntryTrash');
  67. }
  68. else {
  69. icon.src = icon.src.replace(/newsEntryTrash/i, 'newsEntry');
  70. }
  71. }
  72. },
  73. /**
  74. * Saves the marked status.
  75. */
  76. saveMarkedStatus: function(data) {
  77. new Ajax.Request('index.php?action=NewsEntryMark&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  78. method: 'post',
  79. parameters: data
  80. });
  81. },
  82. /**
  83. * Returns a list of the edit options for the edit menu.
  84. */
  85. getEditOptions: function(id) {
  86. var options = new Array();
  87. var i = 0;
  88. var entry = this.data.get(id);
  89. // edit title
  90. if (permissions['canEditNewsEntry']) {
  91. options[i] = new Object();
  92. options[i]['function'] = 'newsEntryListEdit.startTitleEdit('+id+');';
  93. options[i]['text'] = language['wsip.news.entries.button.editTitle'];
  94. i++;
  95. }
  96. // enable / disable
  97. if (permissions['canEnableNewsEntry']) {
  98. if (entry.isDisabled == 1) {
  99. options[i] = new Object();
  100. options[i]['function'] = 'newsEntryListEdit.enable('+id+');';
  101. options[i]['text'] = language['wsip.news.entries.button.enable'];
  102. i++;
  103. }
  104. else if (entry.isDeleted == 0) {
  105. options[i] = new Object();
  106. options[i]['function'] = 'newsEntryListEdit.disable('+id+');';
  107. options[i]['text'] = language['wsip.news.entries.button.disable'];
  108. i++;
  109. }
  110. }
  111. // delete
  112. if (permissions['canDeleteNewsEntry'] && (permissions['canDeleteNewsEntryCompletely'] || (entry.isDeleted == 0 && this.options.enableRecycleBin))) {
  113. options[i] = new Object();
  114. options[i]['function'] = 'newsEntryListEdit.remove('+id+');';
  115. options[i]['text'] = (entry.isDeleted == 0 ? language['wcf.global.button.delete'] : language['wcf.global.button.deleteCompletely']);
  116. i++;
  117. }
  118. // recover
  119. if (entry.isDeleted == 1 && permissions['canDeleteNewsEntryCompletely']) {
  120. options[i] = new Object();
  121. options[i]['function'] = 'newsEntryListEdit.recover('+id+');';
  122. options[i]['text'] = language['wsip.news.entries.button.recover'];
  123. i++;
  124. }
  125. // marked status
  126. if (permissions['canMarkNewsEntry']) {
  127. var markedStatus = entry ? entry.isMarked : false;
  128. options[i] = new Object();
  129. options[i]['function'] = 'newsEntryListEdit.parentObject.markItem(' + (markedStatus ? 'false' : 'true') + ', '+id+');';
  130. options[i]['text'] = markedStatus ? language['wcf.global.button.unmark'] : language['wcf.global.button.mark'];
  131. i++;
  132. }
  133. return options;
  134. },
  135. /**
  136. * Returns a list of the edit options for the edit marked menu.
  137. */
  138. getEditMarkedOptions: function() {
  139. var options = new Array();
  140. var i = 0;
  141. if (this.options.page == 'category') {
  142. // move
  143. if (permissions['canMoveNewsEntry']) {
  144. options[i] = new Object();
  145. options[i]['function'] = "newsEntryListEdit.move('move');";
  146. options[i]['text'] = language['wsip.news.entries.button.move'];
  147. i++;
  148. }
  149. }
  150. // delete
  151. if (permissions['canDeleteNewsEntry'] && (permissions['canDeleteNewsEntryCompletely'] || this.options.enableRecycleBin)) {
  152. options[i] = new Object();
  153. options[i]['function'] = 'newsEntryListEdit.removeAll();';
  154. options[i]['text'] = language['wcf.global.button.delete'];
  155. i++;
  156. }
  157. // recover
  158. if (this.options.enableRecycleBin && permissions['canDeleteNewsEntryCompletely']) {
  159. options[i] = new Object();
  160. options[i]['function'] = 'newsEntryListEdit.recoverAll();';
  161. options[i]['text'] = language['wsip.news.entries.button.recover'];
  162. i++;
  163. }
  164. // unmark all
  165. options[i] = new Object();
  166. options[i]['function'] = 'newsEntryListEdit.unmarkAll();';
  167. options[i]['text'] = language['wcf.global.button.unmark'];
  168. i++;
  169. // show marked
  170. options[i] = new Object();
  171. options[i]['function'] = 'document.location.href = fixURL("index.php?page=ModerationMarkedEntries'+SID_ARG_2ND+'")';
  172. options[i]['text'] = language['wsip.news.entries.button.showMarked'];
  173. i++;
  174. return options;
  175. },
  176. /**
  177. * Returns the title of the edit marked menu.
  178. */
  179. getMarkedTitle: function() {
  180. return eval(language['wsip.news.entries.markedEntries']);
  181. },
  182. /**
  183. * Moves this entry.
  184. */
  185. move: function(action) {
  186. document.location.href = fixURL('index.php?action=NewsEntryMoveMarked&categoryID='+this.options.categoryID+'&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  187. },
  188. /**
  189. * Deletes this entry.
  190. */
  191. remove: function(id) {
  192. var entry = this.data.get(id);
  193. if (entry.isDeleted == 0 && this.options.enableRecycleBin) {
  194. var promptResult = prompt(language['wsip.news.entries.delete.reason']);
  195. if (typeof(promptResult) != 'object' && typeof(promptResult) != 'undefined') {
  196. if (permissions['canReadDeletedNewsEntry']) {
  197. new Ajax.Request('index.php?action=NewsEntryTrash&entryID='+id+'&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  198. method: 'post',
  199. parameters: {
  200. reason: promptResult
  201. },
  202. onSuccess: function() {
  203. entry.isDeleted = 1;
  204. this.showStatus(id);
  205. var entryRow = $('newsEntryRow'+id);
  206. if (entryRow) {
  207. entryRow.down('.editNote').insert('<p class="deleteNote smallFont">'+promptResult.escapeHTML()+'</p>');
  208. }
  209. }.bind(this)
  210. });
  211. }
  212. else {
  213. document.location.href = fixURL('index.php?action=NewsEntryTrash&entryID='+id+'&reason='+encodeURIComponent(promptResult)+'&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  214. }
  215. }
  216. }
  217. else {
  218. if (confirm((entry.isDeleted == 0 ? language['wsip.news.entries.delete.sure'] : language['wsip.news.entries.deleteCompletely.sure']))) {
  219. document.location.href = fixURL('index.php?action=NewsEntryDelete&entryID='+id+'&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  220. }
  221. }
  222. },
  223. /**
  224. * Deletes all marked entries.
  225. */
  226. removeAll: function() {
  227. if (this.options.enableRecycleBin) {
  228. var promptResult = prompt(language['wsip.news.entries.deleteMarked.reason']);
  229. if (typeof(promptResult) != 'object' && typeof(promptResult) != 'undefined') {
  230. document.location.href = fixURL('index.php?action=NewsEntryDeleteMarked&reason='+encodeURIComponent(promptResult)+'&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  231. }
  232. }
  233. else if (confirm(language['wsip.news.entries.deleteMarked.sure'])) {
  234. document.location.href = fixURL('index.php?action=NewsEntryDeleteMarked&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  235. }
  236. },
  237. /**
  238. * Recovers all marked entries.
  239. */
  240. recoverAll: function(id) {
  241. document.location.href = fixURL('index.php?action=NewsEntryRecoverMarked&categoryID='+this.options.categoryID+'&url='+encodeURIComponent(this.options.url)+'&t='+SECURITY_TOKEN+SID_ARG_2ND);
  242. },
  243. /**
  244. * Unmarkes all marked entries.
  245. */
  246. unmarkAll: function() {
  247. new Ajax.Request('index.php?action=NewsEntryUnmarkAll&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  248. method: 'get'
  249. });
  250. // checkboxes
  251. this.count = 0;
  252. var entryIDArray = this.data.keys();
  253. for (var i = 0; i < entryIDArray.length; i++) {
  254. var id = entryIDArray[i];
  255. var entry = this.data.get(id);
  256. entry.isMarked = 0;
  257. var checkbox = $('newsEntryMark'+id);
  258. if (checkbox) {
  259. checkbox.checked = false;
  260. }
  261. this.showStatus(id);
  262. }
  263. // mark all checkboxes
  264. this.parentObject.checkMarkAll(false);
  265. // edit marked menu
  266. this.parentObject.showMarked();
  267. },
  268. /**
  269. * Recovers an entry.
  270. */
  271. recover: function(id) {
  272. var entry = this.data.get(id);
  273. if (entry.isDeleted == 1) {
  274. new Ajax.Request('index.php?action=NewsEntryRecover&entryID='+id+'&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  275. method: 'get',
  276. onSuccess: function() {
  277. entry.isDeleted = 0;
  278. this.showStatus(id);
  279. var entryRow = $('newsEntryRow'+id);
  280. if (entryRow) {
  281. entryRow.down('.deleteNote').remove();
  282. }
  283. }.bind(this)
  284. });
  285. }
  286. },
  287. /**
  288. * Enables an entry.
  289. */
  290. enable: function(id) {
  291. var entry = this.data.get(id);
  292. if (entry.isDisabled == 1) {
  293. new Ajax.Request('index.php?action=NewsEntryEnable&entryID='+id+'&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  294. method: 'get',
  295. onSuccess: function() {
  296. entry.isDisabled = 0;
  297. this.showStatus(id);
  298. }.bind(this)
  299. });
  300. }
  301. },
  302. /**
  303. * Disables an entry.
  304. */
  305. disable: function(id) {
  306. var entry = this.data.get(id);
  307. if (entry.isDisabled == 0 && entry.isDeleted == 0) {
  308. new Ajax.Request('index.php?action=NewsEntryDisable&entryID='+id+'&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  309. method: 'get',
  310. onSuccess: function() {
  311. entry.isDisabled = 1;
  312. this.showStatus(id);
  313. }.bind(this)
  314. });
  315. }
  316. },
  317. /**
  318. * Starts the editing of an entry title.
  319. */
  320. startTitleEdit: function(id) {
  321. if ($('newsEntryTitleInput'+id)) return;
  322. var entrySubjectDiv = $('newsEntryTitle'+id);
  323. if (entrySubjectDiv) {
  324. // get value and hide title
  325. var value = '';
  326. var title = entrySubjectDiv.select('a')[0];
  327. if (title) {
  328. title.addClassName('hidden');
  329. value = title.innerHTML.unescapeHTML();
  330. }
  331. // show input field
  332. var inputField = new Element('input', { 'id': 'newsEntryTitleInput'+id, 'type': 'text', 'className': 'inputText', 'value': value });
  333. entrySubjectDiv.insert(inputField);
  334. // add event listeners
  335. inputField.observe('keydown', function(id, e) { this.doTitleEdit(id, e); }.bind(this, id));
  336. inputField.observe('blur', function(id) { this.abortTitleEdit(id); }.bind(this, id));
  337. // set focus
  338. inputField.focus();
  339. }
  340. },
  341. /**
  342. * Aborts the editing of an entry title.
  343. */
  344. abortTitleEdit: function(id) {
  345. // remove input field
  346. var entrySubjectInputDiv = $('newsEntryTitleInput'+id);
  347. if (entrySubjectInputDiv) {
  348. entrySubjectInputDiv.remove();
  349. }
  350. // show title
  351. var entrySubjectDiv = $('newsEntryTitle'+id);
  352. if (entrySubjectDiv) {
  353. // show first child
  354. var title = entrySubjectDiv.select('a')[0];
  355. if (title) {
  356. title.removeClassName('hidden');
  357. }
  358. }
  359. },
  360. /**
  361. * Takes the value of the input-field and creates an ajax-request to save the new title.
  362. * enter = save
  363. * esc = abort
  364. */
  365. doTitleEdit: function(id, event) {
  366. var keyCode = event.keyCode;
  367. // get input field
  368. var inputField = $('newsEntryTitleInput'+id);
  369. // enter
  370. if (keyCode == Event.KEY_RETURN && inputField.value != '') {
  371. // set new value
  372. inputField.value = inputField.value.strip();
  373. var entrySubjectDiv = $('newsEntryTitle'+id);
  374. var title = entrySubjectDiv.select('a')[0];
  375. if (title) {
  376. title.update(inputField.getValue().escapeHTML());
  377. }
  378. // save new value
  379. new Ajax.Request('index.php?action=NewsEntrySubjectEdit&entryID='+id+'&t='+SECURITY_TOKEN+SID_ARG_2ND, {
  380. method: 'get',
  381. parameters: {
  382. subject: inputField.getValue()
  383. }
  384. });
  385. // abort editing
  386. inputField.blur();
  387. return false;
  388. }
  389. // esc
  390. else if (keyCode == '27') {
  391. inputField.blur();
  392. return false;
  393. }
  394. }
  395. });