/portal-web/docroot/html/js/editor/ckeditor.jsp

http://github.com/liferay/liferay-portal · JavaServer Pages · 327 lines · 237 code · 77 blank · 13 comment · 24 complexity · 440fe65b6c26346365aeee9c50cacd5e MD5 · raw file

  1. <%--
  2. /**
  3. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU Lesser General Public License as published by the Free
  7. * Software Foundation; either version 2.1 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. --%>
  16. <%@ include file="/html/taglib/init.jsp" %>
  17. <%
  18. String portletId = portletDisplay.getRootPortletId();
  19. String mainPath = themeDisplay.getPathMain();
  20. String doAsUserId = themeDisplay.getDoAsUserId();
  21. if (Validator.isNull(doAsUserId)) {
  22. doAsUserId = Encryptor.encrypt(company.getKeyObj(), String.valueOf(themeDisplay.getUserId()));
  23. }
  24. long doAsGroupId = themeDisplay.getDoAsGroupId();
  25. String ckEditorConfigFileName = ParamUtil.getString(request, "ckEditorConfigFileName");
  26. if (!_ckEditorConfigFileNames.contains(ckEditorConfigFileName)) {
  27. ckEditorConfigFileName = "ckconfig.jsp";
  28. }
  29. boolean useCustomDataProcessor = false;
  30. if (!ckEditorConfigFileName.equals("ckconfig.jsp")) {
  31. useCustomDataProcessor = true;
  32. }
  33. boolean hideImageResizing = ParamUtil.getBoolean(request, "hideImageResizing");
  34. Map<String, String> configParamsMap = (Map<String, String>)request.getAttribute("liferay-ui:input-editor:configParams");
  35. Map<String, String> fileBrowserParamsMap = (Map<String, String>)request.getAttribute("liferay-ui:input-editor:fileBrowserParams");
  36. String configParams = marshallParams(configParamsMap);
  37. String fileBrowserParams = marshallParams(fileBrowserParamsMap);
  38. String cssClass = GetterUtil.getString((String)request.getAttribute("liferay-ui:input-editor:cssClass"));
  39. String cssClasses = GetterUtil.getString((String)request.getAttribute("liferay-ui:input-editor:cssClasses"));
  40. String editorImpl = (String)request.getAttribute("liferay-ui:input-editor:editorImpl");
  41. String name = namespace + GetterUtil.getString((String)request.getAttribute("liferay-ui:input-editor:name"));
  42. String initMethod = (String)request.getAttribute("liferay-ui:input-editor:initMethod");
  43. String onChangeMethod = (String)request.getAttribute("liferay-ui:input-editor:onChangeMethod");
  44. if (Validator.isNotNull(onChangeMethod)) {
  45. onChangeMethod = namespace + onChangeMethod;
  46. }
  47. boolean resizable = GetterUtil.getBoolean((String)request.getAttribute("liferay-ui:input-editor:resizable"));
  48. boolean skipEditorLoading = GetterUtil.getBoolean((String)request.getAttribute("liferay-ui:input-editor:skipEditorLoading"));
  49. String toolbarSet = (String)request.getAttribute("liferay-ui:input-editor:toolbarSet");
  50. %>
  51. <c:if test="<%= hideImageResizing %>">
  52. <liferay-util:html-top outputKey="js_editor_ckeditor_hide_image_resizing">
  53. <style type="text/css">
  54. a.cke_dialog_tab {
  55. display: none !important;
  56. }
  57. a.cke_dialog_tab_selected {
  58. display:block !important;
  59. }
  60. </style>
  61. </liferay-util:html-top>
  62. </c:if>
  63. <c:if test="<%= !skipEditorLoading %>">
  64. <liferay-util:html-top outputKey="js_editor_ckeditor_skip_editor_loading">
  65. <style type="text/css">
  66. table.cke_dialog {
  67. position: absolute !important;
  68. }
  69. </style>
  70. <%
  71. long javaScriptLastModified = ServletContextUtil.getLastModified(application, "/html/js/", true);
  72. %>
  73. <script src="<%= HtmlUtil.escape(PortalUtil.getStaticResourceURL(request, themeDisplay.getCDNHost() + themeDisplay.getPathJavaScript() + "/editor/ckeditor/ckeditor.js", javaScriptLastModified)) %>" type="text/javascript"></script>
  74. <script type="text/javascript">
  75. Liferay.namespace('EDITORS')['<%= editorImpl %>'] = true;
  76. </script>
  77. </liferay-util:html-top>
  78. </c:if>
  79. <aui:script>
  80. window['<%= name %>'] = {
  81. destroy: function() {
  82. CKEDITOR.instances['<%= name %>'].destroy();
  83. delete window['<%= name %>'];
  84. },
  85. focus: function() {
  86. CKEDITOR.instances['<%= name %>'].focus();
  87. },
  88. getCkData: function() {
  89. var data = CKEDITOR.instances['<%= name %>'].getData();
  90. if (CKEDITOR.env.gecko && (CKEDITOR.tools.trim(data) == '<br />')) {
  91. data = '';
  92. }
  93. return data;
  94. },
  95. getHTML: function() {
  96. return window['<%= name %>'].getCkData();
  97. },
  98. getText: function() {
  99. return window['<%= name %>'].getCkData();
  100. },
  101. <%
  102. if (Validator.isNotNull(onChangeMethod)) {
  103. %>
  104. onChangeCallback: function () {
  105. var ckEditor = CKEDITOR.instances['<%= name %>'];
  106. var dirty = ckEditor.checkDirty();
  107. if (dirty) {
  108. <%= HtmlUtil.escapeJS(onChangeMethod) %>(window['<%= name %>'].getText());
  109. ckEditor.resetDirty();
  110. }
  111. },
  112. <%
  113. }
  114. %>
  115. setHTML: function(value) {
  116. CKEDITOR.instances['<%= name %>'].setData(value);
  117. }
  118. };
  119. </aui:script>
  120. <div class="<%= cssClass %>">
  121. <textarea id="<%= name %>" name="<%= name %>" style="display: none;"></textarea>
  122. </div>
  123. <aui:script use="aui-base">
  124. (function() {
  125. function setData() {
  126. <c:if test="<%= Validator.isNotNull(initMethod) %>">
  127. ckEditor.setData(<%= HtmlUtil.escapeJS(namespace + initMethod) %>());
  128. </c:if>
  129. }
  130. <%
  131. StringBundler sb = new StringBundler(10);
  132. sb.append(mainPath);
  133. sb.append("/portal/fckeditor?p_l_id=");
  134. sb.append(plid);
  135. sb.append("&p_p_id=");
  136. sb.append(HttpUtil.encodeURL(portletId));
  137. sb.append("&doAsUserId=");
  138. sb.append(HttpUtil.encodeURL(doAsUserId));
  139. sb.append("&doAsGroupId=");
  140. sb.append(HttpUtil.encodeURL(String.valueOf(doAsGroupId)));
  141. sb.append(fileBrowserParams);
  142. String connectorURL = HttpUtil.encodeURL(sb.toString());
  143. %>
  144. CKEDITOR.replace(
  145. '<%= name %>',
  146. {
  147. customConfig: '<%= PortalUtil.getPathContext() %>/html/js/editor/ckeditor/<%= HtmlUtil.escapeJS(ckEditorConfigFileName) %>?p_l_id=<%= plid %>&p_p_id=<%= HttpUtil.encodeURL(portletId) %>&p_main_path=<%= HttpUtil.encodeURL(mainPath) %>&doAsUserId=<%= HttpUtil.encodeURL(doAsUserId) %>&doAsGroupId=<%= HttpUtil.encodeURL(String.valueOf(doAsGroupId)) %>&cssPath=<%= HttpUtil.encodeURL(themeDisplay.getPathThemeCss()) %>&cssClasses=<%= HttpUtil.encodeURL(cssClasses) %>&imagesPath=<%= HttpUtil.encodeURL(themeDisplay.getPathThemeImages()) %>&languageId=<%= HttpUtil.encodeURL(LocaleUtil.toLanguageId(locale)) %>&resizable=<%= resizable %><%= configParams %>',
  148. filebrowserBrowseUrl: '<%= PortalUtil.getPathContext() %>/html/js/editor/ckeditor/editor/filemanager/browser/liferay/browser.html?Connector=<%= connectorURL %><%= fileBrowserParams %>',
  149. filebrowserUploadUrl: null,
  150. toolbar: '<%= TextFormatter.format(HtmlUtil.escapeJS(toolbarSet), TextFormatter.M) %>'
  151. }
  152. );
  153. var ckEditor = CKEDITOR.instances['<%= name %>'];
  154. var customDataProcessorLoaded = false;
  155. <%
  156. if (useCustomDataProcessor) {
  157. %>
  158. ckEditor.on(
  159. 'customDataProcessorLoaded',
  160. function() {
  161. customDataProcessorLoaded = true;
  162. if (instanceReady) {
  163. setData();
  164. }
  165. }
  166. );
  167. <%
  168. }
  169. %>
  170. var instanceReady = false;
  171. ckEditor.on(
  172. 'instanceReady',
  173. function() {
  174. <%
  175. if (useCustomDataProcessor) {
  176. %>
  177. instanceReady = true;
  178. if (customDataProcessorLoaded) {
  179. setData();
  180. }
  181. <%
  182. }
  183. else {
  184. %>
  185. setData();
  186. <%
  187. }
  188. if (Validator.isNotNull(onChangeMethod)) {
  189. %>
  190. setInterval(
  191. function() {
  192. try {
  193. window['<%= name %>'].onChangeCallback();
  194. }
  195. catch (e) {
  196. }
  197. },
  198. 300
  199. );
  200. <%
  201. }
  202. %>
  203. }
  204. );
  205. <%
  206. if (toolbarSet.equals("creole")) {
  207. %>
  208. Liferay.provide(
  209. window,
  210. '<%= name %>creoleImageHandler',
  211. function(event) {
  212. var A = AUI();
  213. var dialog = event.data.definition.dialog;
  214. if (dialog.getName() == 'image') {
  215. var lockButton = A.one('.cke_btn_locked');
  216. if (lockButton) {
  217. var imageProperties = lockButton.ancestor('.cke_dialog_ui_hbox_first');
  218. if (imageProperties) {
  219. imageProperties.hide();
  220. }
  221. }
  222. var imagePreviewBox = A.one('.ImagePreviewBox');
  223. if (imagePreviewBox) {
  224. imagePreviewBox.setStyle('width', 410);
  225. }
  226. }
  227. },
  228. ['aui-base']
  229. );
  230. ckEditor.on('dialogShow', window['<%= name %>creoleImageHandler']);
  231. <%
  232. }
  233. %>
  234. })();
  235. </aui:script>
  236. <%!
  237. public String marshallParams(Map<String, String> params) {
  238. StringBundler sb = new StringBundler();
  239. if (params != null) {
  240. for (Map.Entry<String, String> configParam : params.entrySet()) {
  241. sb.append(StringPool.AMPERSAND);
  242. sb.append(configParam.getKey());
  243. sb.append(StringPool.EQUAL);
  244. sb.append(HttpUtil.encodeURL(configParam.getValue()));
  245. }
  246. }
  247. return sb.toString();
  248. }
  249. private static Set<String> _ckEditorConfigFileNames = SetUtil.fromArray(new String[] {"ckconfig.jsp", "ckconfig_bbcode.jsp", "ckconfig_creole.jsp"});
  250. %>