Debian Codegraph @

dojox/editor/plugins/PageBreak.js

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