PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/apps/collaboration/blogs/blogs-web/src/main/resources/META-INF/resources/blogs/js/blogs.js

http://github.com/liferay/liferay-portal
JavaScript | 462 lines | 340 code | 122 blank | 0 comment | 44 complexity | 781aa44604bbde823455638c7b489ea7 MD5 | raw file
Possible License(s): LGPL-2.0
  1. AUI.add(
  2. 'liferay-blogs',
  3. function(A) {
  4. var Lang = A.Lang;
  5. var CSS_INVISIBLE = 'invisible';
  6. var STR_BLANK = '';
  7. var STR_CHANGE = 'change';
  8. var STR_CLICK = 'click';
  9. var STR_SUFFIX = '...';
  10. var Blogs = A.Component.create(
  11. {
  12. ATTRS: {
  13. constants: {
  14. validator: Lang.isObject
  15. },
  16. descriptionLength: {
  17. validator: Lang.isNumber,
  18. value: 400
  19. },
  20. editEntryURL: {
  21. validator: Lang.isString
  22. },
  23. entry: {
  24. validator: Lang.isObject
  25. },
  26. saveInterval: {
  27. value: 30000
  28. },
  29. strings: {
  30. validator: Lang.isObject,
  31. value: {
  32. confirmDiscardImages: Liferay.Language.get('uploads-are-in-progress-confirmation'),
  33. savedAtMessage: Liferay.Language.get('entry-saved-at-x'),
  34. savedDraftAtMessage: Liferay.Language.get('draft-saved-at-x'),
  35. saveDraftError: Liferay.Language.get('could-not-save-draft-to-the-server'),
  36. saveDraftMessage: Liferay.Language.get('saving-draft')
  37. }
  38. }
  39. },
  40. AUGMENTS: [Liferay.PortletBase],
  41. EXTENDS: A.Base,
  42. NAME: 'liferay-blogs',
  43. NS: 'liferay-blogs',
  44. prototype: {
  45. initializer: function(config) {
  46. var instance = this;
  47. instance._bindUI();
  48. var entry = instance.get('entry');
  49. var draftEntry = entry && entry.status === instance.get('constants').STATUS_DRAFT;
  50. var userEntry = entry && entry.userId === themeDisplay.getUserId();
  51. if (!entry || userEntry && draftEntry) {
  52. instance._initDraftSaveInterval();
  53. }
  54. var customDescriptionEnabled = entry && entry.customDescription;
  55. instance._customDescription = customDescriptionEnabled ? entry.description : STR_BLANK;
  56. instance._shortenDescription = !customDescriptionEnabled;
  57. instance.setDescription(window[instance.ns('contentEditor')].getText());
  58. },
  59. destructor: function() {
  60. var instance = this;
  61. if (instance._saveDraftTimer) {
  62. instance._saveDraftTimer.cancel();
  63. }
  64. (new A.EventHandle(instance._eventHandles)).detach();
  65. },
  66. setDescription: function(text) {
  67. var instance = this;
  68. var description = instance._customDescription;
  69. if (instance._shortenDescription) {
  70. description = instance._shorten(text);
  71. }
  72. window[instance.ns('descriptionEditor')].setHTML(description);
  73. instance._syncDescriptionEditorUI();
  74. },
  75. _bindUI: function() {
  76. var instance = this;
  77. instance._captionNode = instance.one('.cover-image-caption');
  78. var eventHandles = [
  79. Liferay.on('coverImageDeleted', instance._removeCaption, instance),
  80. Liferay.on(['coverImageUploaded', 'coverImageSelected'], instance._showCaption, instance)
  81. ];
  82. var publishButton = instance.one('#publishButton');
  83. if (publishButton) {
  84. eventHandles.push(
  85. publishButton.on(STR_CLICK, A.bind('_checkImagesBeforeSave', instance, false, false))
  86. );
  87. }
  88. var saveButton = instance.one('#saveButton');
  89. if (saveButton) {
  90. eventHandles.push(
  91. saveButton.on(STR_CLICK, A.bind('_checkImagesBeforeSave', instance, true, false))
  92. );
  93. }
  94. var customAbstractOptions = instance.one('#entryAbstractOptions');
  95. if (customAbstractOptions) {
  96. eventHandles.push(
  97. customAbstractOptions.delegate(STR_CHANGE, instance._configureAbstract, 'input[type="radio"]', instance)
  98. );
  99. }
  100. instance._eventHandles = eventHandles;
  101. },
  102. _checkImagesBeforeSave: function(draft, ajax) {
  103. var instance = this;
  104. if (instance._hasTempImages()) {
  105. if (confirm(instance.get('strings').confirmDiscardImages)) {
  106. instance._getTempImages().each(
  107. function(node) {
  108. node.ancestor().remove();
  109. }
  110. );
  111. instance._saveEntry(draft, ajax);
  112. }
  113. }
  114. else {
  115. instance._saveEntry(draft, ajax);
  116. }
  117. },
  118. _configureAbstract: function(event) {
  119. var instance = this;
  120. var target = event.target;
  121. var description = instance._customDescription;
  122. instance._shortenDescription = target.val() === 'false';
  123. if (instance._shortenDescription) {
  124. instance._customDescription = window[instance.ns('descriptionEditor')].getHTML();
  125. description = window[instance.ns('contentEditor')].getText();
  126. }
  127. instance._setDescriptionReadOnly(instance._shortenDescription);
  128. instance.setDescription(description);
  129. },
  130. _getPrincipalForm: function(formName) {
  131. var instance = this;
  132. return instance.one('form[name=' + instance.ns(formName || 'fm') + ']');
  133. },
  134. _getTempImages: function() {
  135. var instance = this;
  136. return instance.all('img[data-random-id]');
  137. },
  138. _hasTempImages: function() {
  139. var instance = this;
  140. return instance._getTempImages().size() > 0;
  141. },
  142. _initDraftSaveInterval: function() {
  143. var instance = this;
  144. instance._saveDraftTimer = A.later(
  145. instance.get('saveInterval'),
  146. instance,
  147. function() {
  148. if (!instance._hasTempImages()) {
  149. instance._saveEntry(true, true);
  150. }
  151. },
  152. null,
  153. true
  154. );
  155. var entry = instance.get('entry');
  156. instance._oldContent = entry ? entry.content : STR_BLANK;
  157. instance._oldSubtitle = entry ? entry.subtitle : STR_BLANK;
  158. instance._oldTitle = entry ? entry.title : STR_BLANK;
  159. },
  160. _removeCaption: function() {
  161. var instance = this;
  162. var captionNode = instance._captionNode;
  163. if (captionNode) {
  164. captionNode.addClass(CSS_INVISIBLE);
  165. }
  166. window[instance.ns('coverImageCaptionEditor')].setHTML(STR_BLANK);
  167. },
  168. _saveEntry: function(draft, ajax) {
  169. var instance = this;
  170. var constants = instance.get('constants');
  171. var content = window[instance.ns('contentEditor')].getHTML();
  172. var coverImageCaption = window[instance.ns('coverImageCaptionEditor')].getHTML();
  173. var description = window[instance.ns('descriptionEditor')].getHTML();
  174. var subtitle = window[instance.ns('subtitleEditor')].getHTML();
  175. var title = window[instance.ns('titleEditor')].getText();
  176. var form = instance._getPrincipalForm();
  177. if (draft && ajax) {
  178. var hasData = content !== STR_BLANK && title !== STR_BLANK;
  179. var hasChanged = instance._oldContent !== content || instance._oldSubtitle !== subtitle || instance._oldTitle !== title;
  180. if (hasData && hasChanged) {
  181. var strings = instance.get('strings');
  182. var saveStatus = instance.one('#saveStatus');
  183. var allowPingbacks = instance.one('#allowPingbacks');
  184. var allowTrackbacks = instance.one('#allowTrackbacks');
  185. var data = instance.ns(
  186. {
  187. 'allowPingbacks': allowPingbacks && allowPingbacks.val(),
  188. 'allowTrackbacks': allowTrackbacks && allowTrackbacks.val(),
  189. 'assetTagNames': instance.one('#assetTagNames').val(),
  190. 'cmd': constants.ADD,
  191. 'content': content,
  192. 'coverImageCaption': coverImageCaption,
  193. 'coverImageFileEntryCropRegion': instance.one('#coverImageFileEntryCropRegion').val(),
  194. 'coverImageFileEntryId': instance.one('#coverImageFileEntryId').val(),
  195. 'displayDateAmPm': instance.one('#displayDateAmPm').val(),
  196. 'displayDateDay': instance.one('#displayDateDay').val(),
  197. 'displayDateHour': instance.one('#displayDateHour').val(),
  198. 'displayDateMinute': instance.one('#displayDateMinute').val(),
  199. 'displayDateMonth': instance.one('#displayDateMonth').val(),
  200. 'displayDateYear': instance.one('#displayDateYear').val(),
  201. 'entryId': instance.one('#entryId').val(),
  202. 'referringPortletResource': instance.one('#referringPortletResource').val(),
  203. 'subtitle': subtitle,
  204. 'title': title,
  205. 'workflowAction': constants.ACTION_SAVE_DRAFT
  206. }
  207. );
  208. var customAttributes = form.all('[name^=' + instance.NS + 'ExpandoAttribute]');
  209. customAttributes.each(
  210. function(item, index, collection) {
  211. data[item.attr('name')] = item.val();
  212. }
  213. );
  214. A.io.request(
  215. instance.get('editEntryURL'),
  216. {
  217. data: data,
  218. dataType: 'JSON',
  219. on: {
  220. failure: function() {
  221. instance._updateStatus(strings.saveDraftError);
  222. },
  223. start: function() {
  224. Liferay.Util.toggleDisabled(instance.one('#publishButton'), true);
  225. instance._updateStatus(strings.saveDraftMessage);
  226. },
  227. success: function(event, id, obj) {
  228. instance._oldContent = content;
  229. instance._oldSubtitle = subtitle;
  230. instance._oldTitle = title;
  231. var message = this.get('responseData');
  232. if (message) {
  233. instance.one('#coverImageFileEntryId').val(message.coverImageFileEntryId);
  234. instance.one('#entryId').val(message.entryId);
  235. if (message.blogsEntryAttachmentReferences) {
  236. instance._updateImages(message.blogsEntryAttachmentReferences);
  237. }
  238. var tabs1BackButton = instance.one('#tabs1TabsBack');
  239. if (tabs1BackButton) {
  240. tabs1BackButton.attr('href', message.redirect);
  241. }
  242. var cancelButton = instance.one('#cancelButton');
  243. if (cancelButton) {
  244. cancelButton.attr('href', message.redirect);
  245. }
  246. if (saveStatus) {
  247. var entry = instance.get('entry');
  248. var saveText = entry && entry.pending ? strings.savedAtMessage : strings.savedDraftAtMessage;
  249. var now = saveText.replace(/\{0\}/gim, (new Date()).toString());
  250. instance._updateStatus(now);
  251. }
  252. }
  253. else {
  254. saveStatus.hide();
  255. }
  256. Liferay.Util.toggleDisabled(instance.one('#publishButton'), false);
  257. }
  258. }
  259. }
  260. );
  261. }
  262. }
  263. else {
  264. instance.one('#' + constants.CMD).val(instance.get('entry') ? constants.UPDATE : constants.ADD);
  265. instance.one('#content').val(content);
  266. instance.one('#coverImageCaption').val(coverImageCaption);
  267. instance.one('#description').val(description);
  268. instance.one('#subtitle').val(subtitle);
  269. instance.one('#title').val(title);
  270. instance.one('#workflowAction').val(draft ? constants.ACTION_SAVE_DRAFT : constants.ACTION_PUBLISH);
  271. submitForm(form);
  272. }
  273. },
  274. _setDescriptionReadOnly: function(readOnly) {
  275. var instance = this;
  276. var descriptionEditorNode = instance.one('#descriptionEditor');
  277. descriptionEditorNode.attr('contenteditable', !readOnly);
  278. descriptionEditorNode.toggleClass('readonly', readOnly);
  279. },
  280. _shorten: function(text) {
  281. var instance = this;
  282. var descriptionLength = instance.get('descriptionLength');
  283. if (text.length > descriptionLength) {
  284. text = text.substring(0, descriptionLength);
  285. if (STR_SUFFIX.length < descriptionLength) {
  286. var spaceIndex = text.lastIndexOf(' ', descriptionLength - STR_SUFFIX.length);
  287. text = text.substring(0, spaceIndex).concat(STR_SUFFIX);
  288. }
  289. }
  290. return text;
  291. },
  292. _showCaption: function() {
  293. var instance = this;
  294. var captionNode = instance._captionNode;
  295. if (captionNode) {
  296. captionNode.removeClass(CSS_INVISIBLE);
  297. }
  298. },
  299. _syncDescriptionEditorUI: function() {
  300. var instance = this;
  301. var liferayDescriptionEditor = window[instance.ns('descriptionEditor')];
  302. if (liferayDescriptionEditor.instanceReady) {
  303. var nativeDescriptionEditor = liferayDescriptionEditor.getNativeEditor().get('nativeEditor');
  304. if (nativeDescriptionEditor && nativeDescriptionEditor.plugins && nativeDescriptionEditor.plugins.ae_placeholder) {
  305. var editorEvent = {
  306. editor: nativeDescriptionEditor
  307. };
  308. nativeDescriptionEditor.plugins.ae_placeholder._checkEmptyData(editorEvent);
  309. }
  310. }
  311. },
  312. _updateImages: function(persistentImages) {
  313. var instance = this;
  314. persistentImages.forEach(
  315. function(item, index) {
  316. var el = instance.one('img[' + item.attributeDataImageId + '="' + item.fileEntryId + '"]');
  317. if (el) {
  318. el.attr('src', item.fileEntryUrl);
  319. el.removeAttribute(item.attributeDataImageId);
  320. }
  321. }
  322. );
  323. },
  324. _updateStatus: function(text) {
  325. var instance = this;
  326. var saveStatus = instance.one('#saveStatus');
  327. if (saveStatus) {
  328. saveStatus.html(text);
  329. }
  330. }
  331. }
  332. }
  333. );
  334. Liferay.Blogs = Blogs;
  335. },
  336. '',
  337. {
  338. requires: ['aui-base', 'aui-io-request', 'liferay-portlet-base']
  339. }
  340. );