| 1 |
define([ |
| 2 |
"dojo", |
| 3 |
"dijit", |
| 4 |
"dojox", |
| 5 |
"dijit/_editor/_Plugin", |
| 6 |
"dijit/_base/manager", |
| 7 |
"dijit/_editor/range", |
| 8 |
"dijit/_Templated", |
| 9 |
"dijit/TooltipDialog", |
| 10 |
"dijit/form/ValidationTextBox", |
| 11 |
"dijit/form/Select", |
| 12 |
"dijit/form/Button", |
| 13 |
"dijit/form/DropDownButton", |
| 14 |
"dojo/_base/declare", |
| 15 |
"dojo/i18n", |
| 16 |
"dojo/string", |
| 17 |
"dojo/NodeList-dom", |
| 18 |
"dojox/editor/plugins/ToolbarLineBreak", |
| 19 |
"dojo/i18n!dojox/editor/plugins/nls/InsertAnchor", |
| 20 |
"dojo/i18n!dijit/nls/common" |
| 21 |
], function(dojo, dijit, dojox, _Plugin ) { |
| 22 |
|
| 23 |
var InsertAnchor = dojo.declare("dojox.editor.plugins.InsertAnchor", _Plugin, { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
htmlTemplate: "<a name=\"${anchorInput}\" class=\"dijitEditorPluginInsertAnchorStyle\">${textInput}</a>", |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
iconClassPrefix: "dijitAdditionalEditorIcon", |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
_template: [ |
| 44 |
"<table role='presentation'><tr><td>", |
| 45 |
"<label for='${id}_anchorInput'>${anchor}</label>", |
| 46 |
"</td><td>", |
| 47 |
"<input dojoType='dijit.form.ValidationTextBox' required='true' " + |
| 48 |
"id='${id}_anchorInput' name='anchorInput' intermediateChanges='true'>", |
| 49 |
"</td></tr><tr><td>", |
| 50 |
"<label for='${id}_textInput'>${text}</label>", |
| 51 |
"</td><td>", |
| 52 |
"<input dojoType='dijit.form.ValidationTextBox' required='true' id='${id}_textInput' " + |
| 53 |
"name='textInput' intermediateChanges='true'>", |
| 54 |
"</td></tr>", |
| 55 |
"<tr><td colspan='2'>", |
| 56 |
"<button dojoType='dijit.form.Button' type='submit' id='${id}_setButton'>${set}</button>", |
| 57 |
"<button dojoType='dijit.form.Button' type='button' id='${id}_cancelButton'>${cancel}</button>", |
| 58 |
"</td></tr></table>" |
| 59 |
].join(""), |
| 60 |
|
| 61 |
_initButton: function(){ |
| 62 |
|
| 63 |
|
| 64 |
var _this = this; |
| 65 |
var messages = dojo.i18n.getLocalization("dojox.editor.plugins", "InsertAnchor", this.lang); |
| 66 |
|
| 67 |
|
| 68 |
var dropDown = (this.dropDown = new dijit.TooltipDialog({ |
| 69 |
title: messages["title"], |
| 70 |
execute: dojo.hitch(this, "setValue"), |
| 71 |
onOpen: function(){ |
| 72 |
_this._onOpenDialog(); |
| 73 |
dijit.TooltipDialog.prototype.onOpen.apply(this, arguments); |
| 74 |
}, |
| 75 |
onCancel: function(){ |
| 76 |
setTimeout(dojo.hitch(_this, "_onCloseDialog"),0); |
| 77 |
} |
| 78 |
})); |
| 79 |
|
| 80 |
this.button = new dijit.form.DropDownButton({ |
| 81 |
label: messages["insertAnchor"], |
| 82 |
showLabel: false, |
| 83 |
iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "InsertAnchor", |
| 84 |
tabIndex: "-1", |
| 85 |
dropDown: this.dropDown |
| 86 |
}); |
| 87 |
|
| 88 |
messages.id = dijit.getUniqueId(this.editor.id); |
| 89 |
this._uniqueId = messages.id; |
| 90 |
|
| 91 |
this.dropDown.set('content', dropDown.title + |
| 92 |
"<div style='border-bottom: 1px black solid;padding-bottom:2pt;margin-bottom:4pt'></div>" + |
| 93 |
dojo.string.substitute(this._template, messages)); |
| 94 |
|
| 95 |
dropDown.startup(); |
| 96 |
this._anchorInput = dijit.byId(this._uniqueId + "_anchorInput"); |
| 97 |
this._textInput = dijit.byId(this._uniqueId + "_textInput"); |
| 98 |
this._setButton = dijit.byId(this._uniqueId + "_setButton"); |
| 99 |
this.connect(dijit.byId(this._uniqueId + "_cancelButton"), "onClick", function(){ |
| 100 |
this.dropDown.onCancel(); |
| 101 |
}); |
| 102 |
|
| 103 |
if(this._anchorInput){ |
| 104 |
this.connect(this._anchorInput, "onChange", "_checkInput"); |
| 105 |
} |
| 106 |
if(this._textInput){ |
| 107 |
this.connect(this._anchorInput, "onChange", "_checkInput"); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
this.editor.contentDomPreFilters.push(dojo.hitch(this, this._preDomFilter)); |
| 112 |
this.editor.contentDomPostFilters.push(dojo.hitch(this, this._postDomFilter)); |
| 113 |
this._setup(); |
| 114 |
}, |
| 115 |
|
| 116 |
updateState: function(){ |
| 117 |
|
| 118 |
|
| 119 |
this.button.set("disabled", this.get("disabled")); |
| 120 |
}, |
| 121 |
|
| 122 |
setEditor: function(editor){ |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
this.editor = editor; |
| 128 |
this._initButton(); |
| 129 |
}, |
| 130 |
|
| 131 |
_checkInput: function(){ |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
var disable = true; |
| 138 |
if(this._anchorInput.isValid()){ |
| 139 |
disable = false; |
| 140 |
} |
| 141 |
this._setButton.set("disabled", disable); |
| 142 |
}, |
| 143 |
|
| 144 |
_setup: function(){ |
| 145 |
|
| 146 |
|
| 147 |
this.editor.onLoadDeferred.addCallback(dojo.hitch(this, function(){ |
| 148 |
this.connect(this.editor.editNode, "ondblclick", this._onDblClick); |
| 149 |
setTimeout(dojo.hitch(this, function() { |
| 150 |
this._applyStyles(); |
| 151 |
}), 100); |
| 152 |
})); |
| 153 |
}, |
| 154 |
|
| 155 |
getAnchorStyle: function(){ |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
var style = "@media screen {\n" + |
| 162 |
"\t.dijitEditorPluginInsertAnchorStyle {\n" + |
| 163 |
"\t\tbackground-image: url({MODURL}/images/anchor.gif);\n" + |
| 164 |
"\t\tbackground-repeat: no-repeat;\n" + |
| 165 |
"\t\tbackground-position: top left;\n" + |
| 166 |
"\t\tborder-width: 1px;\n" + |
| 167 |
"\t\tborder-style: dashed;\n" + |
| 168 |
"\t\tborder-color: #D0D0D0;\n" + |
| 169 |
"\t\tpadding-left: 20px;\n" + |
| 170 |
"\t}\n" + |
| 171 |
"}\n"; |
| 172 |
|
| 173 |
|
| 174 |
var modurl = dojo.moduleUrl(dojox._scopeName, "editor/plugins/resources").toString(); |
| 175 |
if(!(modurl.match(/^https?:\/\//i)) && |
| 176 |
!(modurl.match(/^file:\/\//i))){ |
| 177 |
|
| 178 |
|
| 179 |
var bUrl; |
| 180 |
if(modurl.charAt(0) === "/"){ |
| 181 |
|
| 182 |
var proto = dojo.doc.location.protocol; |
| 183 |
var hostn = dojo.doc.location.host; |
| 184 |
bUrl = proto + "//" + hostn; |
| 185 |
}else{ |
| 186 |
bUrl = this._calcBaseUrl(dojo.global.location.href); |
| 187 |
} |
| 188 |
if(bUrl[bUrl.length - 1] !== "/" && modurl.charAt(0) !== "/"){ |
| 189 |
bUrl += "/"; |
| 190 |
} |
| 191 |
modurl = bUrl + modurl; |
| 192 |
} |
| 193 |
return style.replace(/\{MODURL\}/gi, modurl); |
| 194 |
}, |
| 195 |
|
| 196 |
_applyStyles: function(){ |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if(!this._styled){ |
| 201 |
try{ |
| 202 |
|
| 203 |
this._styled = true; |
| 204 |
var doc = this.editor.document; |
| 205 |
var style = this.getAnchorStyle(); |
| 206 |
if(!dojo.isIE){ |
| 207 |
var sNode = doc.createElement("style"); |
| 208 |
sNode.appendChild(doc.createTextNode(style)); |
| 209 |
doc.getElementsByTagName("head")[0].appendChild(sNode); |
| 210 |
}else{ |
| 211 |
var ss = doc.createStyleSheet(""); |
| 212 |
ss.cssText = style; |
| 213 |
} |
| 214 |
}catch(e){ } |
| 215 |
} |
| 216 |
}, |
| 217 |
|
| 218 |
_calcBaseUrl: function(fullUrl) { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
var baseUrl = null; |
| 227 |
if (fullUrl !== null) { |
| 228 |
|
| 229 |
var index = fullUrl.indexOf("?"); |
| 230 |
if (index != -1) { |
| 231 |
fullUrl = fullUrl.substring(0,index); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
index = fullUrl.lastIndexOf("/"); |
| 237 |
if (index > 0 && index < fullUrl.length) { |
| 238 |
baseUrl = fullUrl.substring(0,index); |
| 239 |
}else{ |
| 240 |
baseUrl = fullUrl; |
| 241 |
} |
| 242 |
} |
| 243 |
return baseUrl; |
| 244 |
}, |
| 245 |
|
| 246 |
_checkValues: function(args){ |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if(args){ |
| 254 |
if(args.anchorInput){ |
| 255 |
args.anchorInput = args.anchorInput.replace(/"/g, """); |
| 256 |
} |
| 257 |
if(!args.textInput){ |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
args.textInput = " "; |
| 262 |
} |
| 263 |
} |
| 264 |
return args; |
| 265 |
}, |
| 266 |
|
| 267 |
setValue: function(args){ |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
this._onCloseDialog(); |
| 273 |
if(!this.editor.window.getSelection){ |
| 274 |
|
| 275 |
var sel = dijit.range.getSelection(this.editor.window); |
| 276 |
var range = sel.getRangeAt(0); |
| 277 |
var a = range.endContainer; |
| 278 |
if(a.nodeType === 3){ |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
a = a.parentNode; |
| 283 |
} |
| 284 |
if(a && (a.nodeName && a.nodeName.toLowerCase() !== "a")){ |
| 285 |
|
| 286 |
|
| 287 |
a = this.editor._sCall("getSelectedElement", ["a"]); |
| 288 |
} |
| 289 |
if(a && (a.nodeName && a.nodeName.toLowerCase() === "a")){ |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if(this.editor.queryCommandEnabled("unlink")){ |
| 295 |
|
| 296 |
|
| 297 |
this.editor._sCall("selectElementChildren", [a]); |
| 298 |
this.editor.execCommand("unlink"); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
args = this._checkValues(args); |
| 304 |
this.editor.execCommand('inserthtml', |
| 305 |
dojo.string.substitute(this.htmlTemplate, args)); |
| 306 |
}, |
| 307 |
|
| 308 |
_onCloseDialog: function(){ |
| 309 |
|
| 310 |
|
| 311 |
this.editor.focus(); |
| 312 |
}, |
| 313 |
|
| 314 |
_getCurrentValues: function(a){ |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
var anchor, text; |
| 322 |
if(a && a.tagName.toLowerCase() === "a" && dojo.attr(a, "name")){ |
| 323 |
anchor = dojo.attr(a, "name"); |
| 324 |
text = a.textContent || a.innerText; |
| 325 |
this.editor._sCall("selectElement", [a, true]); |
| 326 |
}else{ |
| 327 |
text = this.editor._sCall("getSelectedText"); |
| 328 |
} |
| 329 |
return {anchorInput: anchor || '', textInput: text || ''}; |
| 330 |
}, |
| 331 |
|
| 332 |
_onOpenDialog: function(){ |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
var a; |
| 337 |
if(!this.editor.window.getSelection){ |
| 338 |
|
| 339 |
|
| 340 |
var sel = dijit.range.getSelection(this.editor.window); |
| 341 |
var range = sel.getRangeAt(0); |
| 342 |
a = range.endContainer; |
| 343 |
if(a.nodeType === 3){ |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
a = a.parentNode; |
| 348 |
} |
| 349 |
if(a && (a.nodeName && a.nodeName.toLowerCase() !== "a")){ |
| 350 |
|
| 351 |
|
| 352 |
a = this.editor._sCall("getSelectedElement", ["a"]); |
| 353 |
} |
| 354 |
}else{ |
| 355 |
a = this.editor._sCall("getAncestorElement", ["a"]); |
| 356 |
} |
| 357 |
this.dropDown.reset(); |
| 358 |
this._setButton.set("disabled", true); |
| 359 |
this.dropDown.set("value", this._getCurrentValues(a)); |
| 360 |
}, |
| 361 |
|
| 362 |
_onDblClick: function(e){ |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
if(e && e.target){ |
| 372 |
var t = e.target; |
| 373 |
var tg = t.tagName? t.tagName.toLowerCase() : ""; |
| 374 |
if(tg === "a" && dojo.attr(t, "name")){ |
| 375 |
this.editor.onDisplayChanged(); |
| 376 |
this.editor._sCall("selectElement", [t]); |
| 377 |
setTimeout(dojo.hitch(this, function(){ |
| 378 |
|
| 379 |
|
| 380 |
this.button.set("disabled", false); |
| 381 |
this.button.openDropDown(); |
| 382 |
if(this.button.dropDown.focus){ |
| 383 |
this.button.dropDown.focus(); |
| 384 |
} |
| 385 |
}), 10); |
| 386 |
} |
| 387 |
} |
| 388 |
}, |
| 389 |
|
| 390 |
_preDomFilter: function(node){ |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
dojo.query("a[name]:not([href])", this.editor.editNode).addClass("dijitEditorPluginInsertAnchorStyle"); |
| 400 |
}, |
| 401 |
|
| 402 |
_postDomFilter: function(node){ |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if(node){ |
| 412 |
dojo.query("a[name]:not([href])", node).removeClass("dijitEditorPluginInsertAnchorStyle"); |
| 413 |
} |
| 414 |
return node; |
| 415 |
} |
| 416 |
}); |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){ |
| 421 |
if(o.plugin){ return; } |
| 422 |
var name = o.args.name; |
| 423 |
if(name) { name = name.toLowerCase(); } |
| 424 |
if(name === "insertanchor"){ |
| 425 |
o.plugin = new InsertAnchor(); |
| 426 |
} |
| 427 |
}); |
| 428 |
|
| 429 |
return InsertAnchor; |
| 430 |
|
| 431 |
}); |