PageBreak.js 5.2 KB

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