| 1 |
define([ |
| 2 |
"dojo", |
| 3 |
"dijit", |
| 4 |
"dojox", |
| 5 |
"dijit/_editor/_Plugin", |
| 6 |
"dijit/form/Button", |
| 7 |
"dijit/form/ToggleButton", |
| 8 |
"dojo/_base/connect", |
| 9 |
"dojo/_base/declare", |
| 10 |
"dojo/i18n", |
| 11 |
"dojo/i18n!dojox/editor/plugins/nls/ShowBlockNodes" |
| 12 |
], function(dojo, dijit, dojox, _Plugin) { |
| 13 |
|
| 14 |
var ShowBlockNodes = dojo.declare("dojox.editor.plugins.ShowBlockNodes", _Plugin, { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
useDefaultCommand: false, |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
iconClassPrefix: "dijitAdditionalEditorIcon", |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
_styled: false, |
| 32 |
|
| 33 |
_initButton: function(){ |
| 34 |
|
| 35 |
|
| 36 |
var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "ShowBlockNodes"); |
| 37 |
this.button = new dijit.form.ToggleButton({ |
| 38 |
label: strings["showBlockNodes"], |
| 39 |
showLabel: false, |
| 40 |
iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "ShowBlockNodes", |
| 41 |
tabIndex: "-1", |
| 42 |
onChange: dojo.hitch(this, "_showBlocks") |
| 43 |
}); |
| 44 |
this.editor.addKeyHandler(dojo.keys.F9, true, true, dojo.hitch(this, this.toggle)); |
| 45 |
}, |
| 46 |
|
| 47 |
updateState: function(){ |
| 48 |
|
| 49 |
|
| 50 |
this.button.set("disabled", this.get("disabled")); |
| 51 |
}, |
| 52 |
|
| 53 |
setEditor: function(editor){ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
this.editor = editor; |
| 59 |
this._initButton(); |
| 60 |
}, |
| 61 |
|
| 62 |
toggle: function(){ |
| 63 |
|
| 64 |
|
| 65 |
this.button.set("checked", !this.button.get("checked")); |
| 66 |
}, |
| 67 |
|
| 68 |
_showBlocks: function(show){ |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
var doc = this.editor.document; |
| 74 |
if(!this._styled){ |
| 75 |
try{ |
| 76 |
|
| 77 |
this._styled = true; |
| 78 |
|
| 79 |
var style = ""; |
| 80 |
var blocks = ["div", "p", "ul", "ol", "table", "h1", |
| 81 |
"h2", "h3", "h4", "h5", "h6", "pre", "dir", "center", |
| 82 |
"blockquote", "form", "fieldset", "address", "object", |
| 83 |
"pre", "hr", "ins", "noscript", "li", "map", "button", |
| 84 |
"dd", "dt"]; |
| 85 |
|
| 86 |
var template = "@media screen {\n" + |
| 87 |
"\t.editorShowBlocks {TAG} {\n" + |
| 88 |
"\t\tbackground-image: url({MODURL}/images/blockelems/{TAG}.gif);\n" + |
| 89 |
"\t\tbackground-repeat: no-repeat;\n" + |
| 90 |
"\t\tbackground-position: top left;\n" + |
| 91 |
"\t\tborder-width: 1px;\n" + |
| 92 |
"\t\tborder-style: dashed;\n" + |
| 93 |
"\t\tborder-color: #D0D0D0;\n" + |
| 94 |
"\t\tpadding-top: 15px;\n" + |
| 95 |
"\t\tpadding-left: 15px;\n" + |
| 96 |
"\t}\n" + |
| 97 |
"}\n"; |
| 98 |
|
| 99 |
dojo.forEach(blocks, function(tag){ |
| 100 |
style += template.replace(/\{TAG\}/gi, tag); |
| 101 |
}); |
| 102 |
|
| 103 |
|
| 104 |
var modurl = dojo.moduleUrl(dojox._scopeName, "editor/plugins/resources").toString(); |
| 105 |
if(!(modurl.match(/^https?:\/\//i)) && |
| 106 |
!(modurl.match(/^file:\/\//i))){ |
| 107 |
|
| 108 |
|
| 109 |
var bUrl; |
| 110 |
if(modurl.charAt(0) === "/"){ |
| 111 |
|
| 112 |
var proto = dojo.doc.location.protocol; |
| 113 |
var hostn = dojo.doc.location.host; |
| 114 |
bUrl = proto + "//" + hostn; |
| 115 |
}else{ |
| 116 |
bUrl = this._calcBaseUrl(dojo.global.location.href); |
| 117 |
} |
| 118 |
if(bUrl[bUrl.length - 1] !== "/" && modurl.charAt(0) !== "/"){ |
| 119 |
bUrl += "/"; |
| 120 |
} |
| 121 |
modurl = bUrl + modurl; |
| 122 |
} |
| 123 |
|
| 124 |
style = style.replace(/\{MODURL\}/gi, modurl); |
| 125 |
if(!dojo.isIE){ |
| 126 |
var sNode = doc.createElement("style"); |
| 127 |
sNode.appendChild(doc.createTextNode(style)); |
| 128 |
doc.getElementsByTagName("head")[0].appendChild(sNode); |
| 129 |
}else{ |
| 130 |
var ss = doc.createStyleSheet(""); |
| 131 |
ss.cssText = style; |
| 132 |
} |
| 133 |
}catch(e){ |
| 134 |
console.warn(e); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if(show){ |
| 140 |
dojo.addClass(this.editor.editNode, "editorShowBlocks"); |
| 141 |
}else{ |
| 142 |
dojo.removeClass(this.editor.editNode, "editorShowBlocks"); |
| 143 |
} |
| 144 |
}, |
| 145 |
|
| 146 |
_calcBaseUrl: function(fullUrl) { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
var baseUrl = null; |
| 155 |
if (fullUrl !== null) { |
| 156 |
|
| 157 |
var index = fullUrl.indexOf("?"); |
| 158 |
if (index != -1) { |
| 159 |
fullUrl = fullUrl.substring(0,index); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
index = fullUrl.lastIndexOf("/"); |
| 165 |
if (index > 0 && index < fullUrl.length) { |
| 166 |
baseUrl = fullUrl.substring(0,index); |
| 167 |
}else{ |
| 168 |
baseUrl = fullUrl; |
| 169 |
} |
| 170 |
} |
| 171 |
return baseUrl; |
| 172 |
} |
| 173 |
}); |
| 174 |
|
| 175 |
|
| 176 |
dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){ |
| 177 |
if(o.plugin){ return; } |
| 178 |
var name = o.args.name.toLowerCase(); |
| 179 |
if(name === "showblocknodes"){ |
| 180 |
o.plugin = new ShowBlockNodes(); |
| 181 |
} |
| 182 |
}); |
| 183 |
|
| 184 |
return ShowBlockNodes; |
| 185 |
|
| 186 |
}); |