ShowBlockNodes.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.editor.plugins.ShowBlockNodes"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.ShowBlockNodes"] = true;
  8. dojo.provide("dojox.editor.plugins.ShowBlockNodes");
  9. dojo.require("dijit._editor._Plugin");
  10. dojo.require("dijit.form.Button");
  11. dojo.require("dojo.i18n");
  12. dojo.requireLocalization("dojox.editor.plugins", "ShowBlockNodes", null, "ROOT,ar,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  13. dojo.declare("dojox.editor.plugins.ShowBlockNodes",dijit._editor._Plugin,{
  14. // summary:
  15. // This plugin provides ShowBlockNodes cabability to the editor. When
  16. // clicked, the document in the editor will apply a class to specific
  17. // block nodes to make them visible in the layout. This info is not
  18. // exposed/extracted when the editor value is obtained, it is purely for help
  19. // while working on the page.
  20. // useDefaultCommand [protected] boolean
  21. // Over-ride indicating that the command processing is done all by this plugin.
  22. useDefaultCommand: false,
  23. // iconClassPrefix: [const] String
  24. // The CSS class name for the button node is formed from `iconClassPrefix` and `command`
  25. iconClassPrefix: "dijitAdditionalEditorIcon",
  26. // _styled [private] boolean
  27. // Flag indicating the document has had the style updates applied.
  28. _styled: false,
  29. _initButton: function(){
  30. // summary:
  31. // Over-ride for creation of the preview button.
  32. var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "ShowBlockNodes");
  33. this.button = new dijit.form.ToggleButton({
  34. label: strings["showBlockNodes"],
  35. showLabel: false,
  36. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "ShowBlockNodes",
  37. tabIndex: "-1",
  38. onChange: dojo.hitch(this, "_showBlocks")
  39. });
  40. this.editor.addKeyHandler(dojo.keys.F9, true, true, dojo.hitch(this, this.toggle));
  41. },
  42. updateState: function(){
  43. // summary:
  44. // Over-ride for button state control for disabled to work.
  45. this.button.set("disabled", this.get("disabled"));
  46. },
  47. setEditor: function(editor){
  48. // summary:
  49. // Over-ride for the setting of the editor.
  50. // editor: Object
  51. // The editor to configure for this plugin to use.
  52. this.editor = editor;
  53. this._initButton();
  54. },
  55. toggle: function(){
  56. // summary:
  57. // Function to allow programmatic toggling of the view.
  58. this.button.set("checked", !this.button.get("checked"));
  59. },
  60. _showBlocks: function(show){
  61. // summary:
  62. // Function to trigger printing of the editor document
  63. // tags:
  64. // private
  65. var doc = this.editor.document;
  66. if(!this._styled){
  67. try{
  68. //Attempt to inject our specialized style rules for doing this.
  69. this._styled = true;
  70. var style = "";
  71. var blocks = ["div", "p", "ul", "ol", "table", "h1",
  72. "h2", "h3", "h4", "h5", "h6", "pre", "dir", "center",
  73. "blockquote", "form", "fieldset", "address", "object",
  74. "pre", "hr", "ins", "noscript", "li", "map", "button",
  75. "dd", "dt"];
  76. var template = "@media screen {\n" +
  77. "\t.editorShowBlocks {TAG} {\n" +
  78. "\t\tbackground-image: url({MODURL}/images/blockelems/{TAG}.gif);\n" +
  79. "\t\tbackground-repeat: no-repeat;\n" +
  80. "\t\tbackground-position: top left;\n" +
  81. "\t\tborder-width: 1px;\n" +
  82. "\t\tborder-style: dashed;\n" +
  83. "\t\tborder-color: #D0D0D0;\n" +
  84. "\t\tpadding-top: 15px;\n" +
  85. "\t\tpadding-left: 15px;\n" +
  86. "\t}\n" +
  87. "}\n";
  88. dojo.forEach(blocks, function(tag){
  89. style += template.replace(/\{TAG\}/gi, tag);
  90. });
  91. //Finally associate in the image locations based off the module url.
  92. var modurl = dojo.moduleUrl(dojox._scopeName, "editor/plugins/resources").toString();
  93. if(!(modurl.match(/^https?:\/\//i)) &&
  94. !(modurl.match(/^file:\/\//i))){
  95. // We have to root it to the page location on webkit for some nutball reason.
  96. // Probably has to do with how iframe was loaded.
  97. var bUrl;
  98. if(modurl.charAt(0) === "/"){
  99. //Absolute path on the server, so lets handle...
  100. var proto = dojo.doc.location.protocol;
  101. var hostn = dojo.doc.location.host;
  102. bUrl = proto + "//" + hostn;
  103. }else{
  104. bUrl = this._calcBaseUrl(dojo.global.location.href);
  105. }
  106. if(bUrl[bUrl.length - 1] !== "/" && modurl.charAt(0) !== "/"){
  107. bUrl += "/";
  108. }
  109. modurl = bUrl + modurl;
  110. }
  111. // Update all the urls.
  112. style = style.replace(/\{MODURL\}/gi, modurl);
  113. if(!dojo.isIE){
  114. var sNode = doc.createElement("style");
  115. sNode.appendChild(doc.createTextNode(style));
  116. doc.getElementsByTagName("head")[0].appendChild(sNode);
  117. }else{
  118. var ss = doc.createStyleSheet("");
  119. ss.cssText = style;
  120. }
  121. }catch(e){
  122. console.warn(e);
  123. }
  124. }
  125. // Apply/remove the classes based on state.
  126. if(show){
  127. dojo.addClass(this.editor.editNode, "editorShowBlocks");
  128. }else{
  129. dojo.removeClass(this.editor.editNode, "editorShowBlocks");
  130. }
  131. },
  132. _calcBaseUrl: function(fullUrl) {
  133. // summary:
  134. // Internal function used to figure out the full root url (no relatives)
  135. // for loading images in the styles in the iframe.
  136. // fullUrl: String
  137. // The full url to tear down to the base.
  138. // tags:
  139. // private
  140. var baseUrl = null;
  141. if (fullUrl !== null) {
  142. // Check to see if we need to strip off any query parameters from the Url.
  143. var index = fullUrl.indexOf("?");
  144. if (index != -1) {
  145. fullUrl = fullUrl.substring(0,index);
  146. }
  147. // Now we need to trim if necessary. If it ends in /, then we don't
  148. // have a filename to trim off so we can return.
  149. index = fullUrl.lastIndexOf("/");
  150. if (index > 0 && index < fullUrl.length) {
  151. baseUrl = fullUrl.substring(0,index);
  152. }else{
  153. baseUrl = fullUrl;
  154. }
  155. }
  156. return baseUrl; //String
  157. }
  158. });
  159. // Register this plugin.
  160. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  161. if(o.plugin){ return; }
  162. var name = o.args.name.toLowerCase();
  163. if(name === "showblocknodes"){
  164. o.plugin = new dojox.editor.plugins.ShowBlockNodes();
  165. }
  166. });
  167. }