| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
useDefaultCommand: false, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
iconClassPrefix: "dijitAdditionalEditorIcon", |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
_unbreakableNodes: ["li", "ul", "ol"], |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
_pbContent: "<hr style='page-break-after: always;' class='dijitEditorPageBreak'>", |
| 35 |
|
| 36 |
_initButton: function(){ |
| 37 |
|
| 38 |
|
| 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 |
|
| 51 |
ed.addKeyHandler(dojo.keys.ENTER, true, true, dojo.hitch(this, this._insertPageBreak)); |
| 52 |
if(dojo.isWebKit || dojo.isOpera){ |
| 53 |
|
| 54 |
|
| 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 |
|
| 67 |
|
| 68 |
this.button.set("disabled", this.get("disabled")); |
| 69 |
}, |
| 70 |
|
| 71 |
setEditor: function(editor){ |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
this.editor = editor; |
| 77 |
this._initButton(); |
| 78 |
}, |
| 79 |
|
| 80 |
_style: function(){ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
try{ |
| 133 |
if(!this._styled){ this._style(); } |
| 134 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 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 |
}); |