/pimcore/static/js/pimcore/document/edit.js

https://github.com/benawv/bogor-city-guide · JavaScript · 321 lines · 237 code · 57 blank · 27 comment · 37 complexity · c265a88a5a02b3dbcff462c6d2460f98 MD5 · raw file

  1. /**
  2. * Pimcore
  3. *
  4. * LICENSE
  5. *
  6. * This source file is subject to the new BSD license that is bundled
  7. * with this package in the file LICENSE.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://www.pimcore.org/license
  10. *
  11. * @copyright Copyright (c) 2009-2013 pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license New BSD License
  13. */
  14. pimcore.registerNS("pimcore.document.edit");
  15. pimcore.document.edit = Class.create({
  16. initialize: function(document) {
  17. this.document = document;
  18. },
  19. getEditLink: function () {
  20. var date = new Date();
  21. var link = this.document.data.path + this.document.data.key + '?pimcore_editmode=true&systemLocale='
  22. + pimcore.settings.language+'&_dc=' + date.getTime();
  23. if(this.persona && this.persona.getValue()) {
  24. link += "&_ptp=" + this.persona.getValue();
  25. }
  26. return link;
  27. },
  28. getLayout: function (additionalConfig) {
  29. if (this.layout == null) {
  30. this.reloadInProgress = true;
  31. this.iframeName = 'document_iframe_' + this.document.id;
  32. var html = '<iframe id="' + this.iframeName + '" width="100%" name="' + this.iframeName
  33. + '" src="' + this.getEditLink() + '" frameborder="0"></iframe>';
  34. var cleanupFunction = function () {
  35. Ext.Ajax.request({
  36. url: "/admin/page/clear-editable-data",
  37. params: {
  38. persona: this["persona"] ? this.persona.getValue() : "",
  39. id: this.document.id
  40. },
  41. success: function () {
  42. this.reload(true);
  43. }.bind(this)
  44. });
  45. };
  46. var tbar = [{
  47. text: t("refresh"),
  48. iconCls: "pimcore_icon_reload",
  49. handler: this.reload.bind(this)
  50. },"-",{
  51. tooltip: t("highlight_editable_elements"),
  52. iconCls: "pimcore_icon_highlight",
  53. enableToggle: true,
  54. handler: function (el) {
  55. var editables = this.frame.Ext.getBody().query(".pimcore_editable");
  56. var ed;
  57. for(var i=0; i<editables.length; i++) {
  58. ed = Ext.get(editables[i]);
  59. if(!ed.hasClass("pimcore_tag_inc") && !ed.hasClass("pimcore_tag_areablock")
  60. && !ed.hasClass("pimcore_tag_block") && !ed.hasClass("pimcore_tag_area")) {
  61. if(el.pressed) {
  62. var mask = ed.mask();
  63. mask.setStyle("background-color","#f5d833");
  64. } else {
  65. ed.unmask();
  66. }
  67. }
  68. }
  69. }.bind(this)
  70. }, "-", {
  71. tooltip: t("clear_content_of_current_view"),
  72. iconCls: "pimcore_icon_cleanup",
  73. handler: cleanupFunction.bind(this)
  74. }];
  75. // add persona selection to toolbar
  76. if(this.document.getType() == "page" && pimcore.globalmanager.get("personas").getCount() > 0) {
  77. this.persona = new Ext.form.ComboBox({
  78. displayField:'text',
  79. valueField: "id",
  80. store: {
  81. xtype: "jsonstore",
  82. url: "/admin/reports/targeting/persona-list/?add-default=true",
  83. fields: ["id", "text"]
  84. },
  85. editable: false,
  86. triggerAction: 'all',
  87. width: 240,
  88. cls: "pimcore_icon_persona_select",
  89. emptyText: t("edit_content_for_persona"),
  90. listeners: {
  91. select: function (el) {
  92. if(this.document.isDirty()) {
  93. Ext.Msg.confirm(t('warning'), t('you_have_unsaved_changes')
  94. + "<br />" + t("continue") + "?",
  95. function(btn){
  96. if (btn == 'yes'){
  97. this.reload(true);
  98. }
  99. }.bind(this)
  100. );
  101. } else {
  102. this.reload(true);
  103. }
  104. }.bind(this)
  105. }
  106. });
  107. tbar.push("->", this.persona, {
  108. tooltip: t("clear_content_of_selected_persona"),
  109. iconCls: "pimcore_icon_cleanup",
  110. handler: cleanupFunction.bind(this)
  111. });
  112. }
  113. // edit panel configuration
  114. var config = {
  115. id: "document_content_" + this.document.id,
  116. html: html,
  117. title: t('edit'),
  118. autoScroll: true,
  119. bodyStyle: "-webkit-overflow-scrolling:touch;",
  120. forceLayout: true,
  121. hideMode: "offsets",
  122. iconCls: "pimcore_icon_tab_edit",
  123. tbar: tbar
  124. };
  125. if(typeof additionalConfig == "object") {
  126. config = Ext.apply(config, additionalConfig);
  127. }
  128. this.layout = new Ext.Panel(config);
  129. this.layout.on("resize", this.onLayoutResize.bind(this));
  130. this.layout.on("afterrender", function () {
  131. // unfortunately we have to do this in jQuery, because Ext doesn'T offer this functionality
  132. $("#" + this.iframeName).load(function () {
  133. // this is to hide the mask if edit/startup.js isn't executed (eg. in case an error is shown)
  134. // otherwise edit/startup.js will disable the loading mask
  135. if(!this["frame"]) {
  136. this.loadMask.hide();
  137. }
  138. }.bind(this));
  139. this.loadMask = new Ext.LoadMask(this.layout.body, {msg: t("please_wait")});
  140. this.loadMask.enable();
  141. this.loadMask.show();
  142. }.bind(this));
  143. }
  144. return this.layout;
  145. },
  146. /*
  147. iFrameLoaded: function () {
  148. console.log("finished");
  149. },
  150. */
  151. onLayoutResize: function (el, width, height, rWidth, rHeight) {
  152. this.setLayoutFrameDimensions(width, height);
  153. },
  154. setLayoutFrameDimensions: function (width, height) {
  155. Ext.get(this.iframeName).setStyle({
  156. height: (height-32) + "px"
  157. });
  158. },
  159. onClose: function () {
  160. try {
  161. this.reloadInProgress = true;
  162. window[this.iframeName].location.href = "about:blank";
  163. Ext.get(this.iframeName).remove();
  164. delete window[this.iframeName];
  165. } catch (e) {
  166. }
  167. },
  168. protectLocation: function () {
  169. if (this.reloadInProgress != true) {
  170. window.setTimeout(this.reload.bind(this), 200);
  171. }
  172. },
  173. iframeOnbeforeunload: function () {
  174. if(!this.reloadInProgress && !pimcore.globalmanager.get("pimcore_reload_in_progress")) {
  175. return t("do_you_really_want_to_leave_the_editmode");
  176. }
  177. },
  178. reload: function (disableSaveToSession) {
  179. if (this.reloadInProgress) {
  180. disableSaveToSession = true;
  181. }
  182. this.reloadInProgress = true;
  183. try {
  184. if(this["frame"]) {
  185. this.lastScrollposition = this.frame.Ext.getBody().getScroll();
  186. }
  187. }
  188. catch (e) {
  189. console.log(e);
  190. }
  191. this.loadMask.show();
  192. if (disableSaveToSession === true) {
  193. this.frame = null;
  194. Ext.get(this.iframeName).dom.src = this.getEditLink();
  195. }
  196. else {
  197. this.document.saveToSession(function () {
  198. this.frame = null;
  199. Ext.get(this.iframeName).dom.src = this.getEditLink();
  200. }.bind(this));
  201. }
  202. },
  203. maskFrames: function () {
  204. // this is for dnd over iframes, with this method it's not nessercery to register the dnd manager in each
  205. // iframe (wysiwyg)
  206. var width;
  207. var height;
  208. var offset;
  209. var element;
  210. var i;
  211. // mask frames (iframes)
  212. try {
  213. // mask iframes
  214. if (typeof this.frame.Ext != "object") {
  215. return;
  216. }
  217. var iFrames = this.frame.document.getElementsByTagName("iframe");
  218. for (i = 0; i < iFrames.length; i++) {
  219. width = Ext.get(iFrames[i]).getWidth();
  220. height = Ext.get(iFrames[i]).getHeight();
  221. offset = Ext.get(iFrames[i]).getOffsetsTo(this.frame.Ext.getBody());
  222. element = this.frame.Ext.getBody().createChild({
  223. tag: "div",
  224. id: Ext.id()
  225. });
  226. element.setStyle({
  227. width: width + "px",
  228. height: height + "px",
  229. left: offset[0] + "px",
  230. top: offset[1] + "px"
  231. });
  232. element.addClass("pimcore_iframe_mask");
  233. }
  234. }
  235. catch (e) {
  236. console.log(e);
  237. console.log("there is no frame to mask");
  238. }
  239. },
  240. getValues: function () {
  241. var values = {};
  242. if (!this.frame || !this.frame.editables) {
  243. throw "edit not available";
  244. }
  245. try {
  246. var editables = this.frame.editables;
  247. var editableName = "";
  248. for (var i = 0; i < editables.length; i++) {
  249. try {
  250. if (editables[i].getName() && !editables[i].getInherited()) {
  251. editableName = editables[i].getName();
  252. values[editableName] = {};
  253. values[editableName].data = editables[i].getValue();
  254. values[editableName].type = editables[i].getType();
  255. }
  256. } catch (e) {
  257. }
  258. }
  259. }
  260. catch (e2) {
  261. }
  262. return values;
  263. }
  264. });