PageBreak.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.PageBreak"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.PageBreak"] = true;
  8. dojo.provide("dojox.editor.plugins.PageBreak");
  9. dojo.require("dijit._editor.html");
  10. dojo.require("dijit._editor._Plugin");
  11. dojo.require("dojo.i18n");
  12. dojo.requireLocalization("dojox.editor.plugins", "PageBreak", 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.PageBreak",dijit._editor._Plugin,{
  14. // summary:
  15. // This plugin provides a simple CSS page break plugin that
  16. // lets you insert browser pring recognizable page breaks in
  17. // the document.
  18. // This plugin registers the hotkey command: CTRL-SHIFT-ENTER
  19. // useDefaultCommand [protected]
  20. // Over-ride indicating that the command processing is done all by this plugin.
  21. useDefaultCommand: false,
  22. // iconClassPrefix: [const] String
  23. // The CSS class name for the button node is formed from
  24. // `iconClassPrefix` and `command`
  25. iconClassPrefix: "dijitAdditionalEditorIcon",
  26. // _unbreakableNodes: [private] Array
  27. // The nodes that should not allow page breaks to be inserted into them.
  28. _unbreakableNodes: ["li", "ul", "ol"],
  29. // _pbContent: [private] String
  30. // The markup used for the pagebreak insert.
  31. _pbContent: "<hr style='page-break-after: always;' class='dijitEditorPageBreak'>",
  32. _initButton: function(){
  33. // summary:
  34. // Over-ride for creation of the resize button.
  35. var ed = this.editor;
  36. var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "PageBreak");
  37. this.button = new dijit.form.Button({
  38. label: strings["pageBreak"],
  39. showLabel: false,
  40. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "PageBreak",
  41. tabIndex: "-1",
  42. onClick: dojo.hitch(this, "_insertPageBreak")
  43. });
  44. ed.onLoadDeferred.addCallback(
  45. dojo.hitch(this, function(){
  46. //Register our hotkey to CTRL-SHIFT-ENTER.
  47. ed.addKeyHandler(dojo.keys.ENTER, true, true, dojo.hitch(this, this._insertPageBreak));
  48. if(dojo.isWebKit || dojo.isOpera){
  49. // Webkit and Opera based browsers don't generate keypress events when ctrl and shift are
  50. // held then enter is pressed. Odd, that.
  51. this.connect(this.editor, "onKeyDown", dojo.hitch(this, function(e){
  52. if((e.keyCode === dojo.keys.ENTER) && e.ctrlKey && e.shiftKey){
  53. this._insertPageBreak();
  54. }
  55. }));
  56. }
  57. })
  58. );
  59. },
  60. updateState: function(){
  61. // summary:
  62. // Over-ride for button state control for disabled to work.
  63. this.button.set("disabled", this.get("disabled"));
  64. },
  65. setEditor: function(editor){
  66. // summary:
  67. // Over-ride for the setting of the editor.
  68. // editor: Object
  69. // The editor to configure for this plugin to use.
  70. this.editor = editor;
  71. this._initButton();
  72. },
  73. _style: function(){
  74. // summary:
  75. // Internal function for inserting dynamic css. This was originally
  76. // in an editor.onLoadDeferred, but I ran into issues in Chrome with
  77. // the tag being ignored. Having it done at insert worked better.
  78. // tags:
  79. // private
  80. if(!this._styled){
  81. this._styled = true;
  82. var doc = this.editor.document;
  83. var style = ".dijitEditorPageBreak {\n" +
  84. "\tborder-top-style: solid;\n" +
  85. "\tborder-top-width: 3px;\n" +
  86. "\tborder-top-color: #585858;\n" +
  87. "\tborder-bottom-style: solid;\n" +
  88. "\tborder-bottom-width: 1px;\n" +
  89. "\tborder-bottom-color: #585858;\n" +
  90. "\tborder-left-style: solid;\n" +
  91. "\tborder-left-width: 1px;\n" +
  92. "\tborder-left-color: #585858;\n" +
  93. "\tborder-right-style: solid;\n" +
  94. "\tborder-right-width: 1px;\n" +
  95. "\tborder-right-color: #585858;\n" +
  96. "\tcolor: #A4A4A4;\n" +
  97. "\tbackground-color: #A4A4A4;\n" +
  98. "\theight: 10px;\n"+
  99. "\tpage-break-after: always;\n" +
  100. "\tpadding: 0px 0px 0px 0px;\n" +
  101. "}\n\n" +
  102. "@media print {\n" +
  103. "\t.dijitEditorPageBreak { page-break-after: always; " +
  104. "background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); " +
  105. "border: 0px none rgba(0,0,0,0); display: hidden; " +
  106. "width: 0px; height: 0px;}\n" +
  107. "}";
  108. if(!dojo.isIE){
  109. var sNode = doc.createElement("style");
  110. sNode.appendChild(doc.createTextNode(style));
  111. doc.getElementsByTagName("head")[0].appendChild(sNode);
  112. }else{
  113. var ss = doc.createStyleSheet("");
  114. ss.cssText = style;
  115. }
  116. }
  117. },
  118. _insertPageBreak: function(){
  119. // summary:
  120. // Function to insert a CSS page break at the current point in the document
  121. // tags:
  122. // private
  123. try{
  124. if(!this._styled){ this._style(); }
  125. //this.editor.focus();
  126. if(this._allowBreak()){
  127. this.editor.execCommand("inserthtml", this._pbContent);
  128. }
  129. }catch(e){
  130. console.warn(e);
  131. }
  132. },
  133. _allowBreak: function(){
  134. // summary:
  135. // Internal function to see if we should allow a page break at the document
  136. // location.
  137. // tags:
  138. // private
  139. var ed = this.editor;
  140. var doc = ed.document;
  141. var node = ed._sCall("getSelectedElement", null) || ed._sCall("getParentElement", null);
  142. while(node && node !== doc.body && node !== doc.html){
  143. if(ed._sCall("isTag", [node, this._unbreakableNodes])){
  144. return false;
  145. }
  146. node = node.parentNode;
  147. }
  148. return true;
  149. }
  150. });
  151. // Register this plugin.
  152. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  153. if(o.plugin){ return; }
  154. var name = o.args.name.toLowerCase();
  155. if(name === "pagebreak"){
  156. o.plugin = new dojox.editor.plugins.PageBreak({});
  157. }
  158. });
  159. }