| 1 |
define([ |
| 2 |
"require", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/declare", |
| 5 |
"dojo/dom-construct", |
| 6 |
"dojo/i18n", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/string", |
| 9 |
"dojo/store/Memory", |
| 10 |
"../../registry", |
| 11 |
"../../_Widget", |
| 12 |
"../../_TemplatedMixin", |
| 13 |
"../../_WidgetsInTemplateMixin", |
| 14 |
"../../form/FilteringSelect", |
| 15 |
"../_Plugin", |
| 16 |
"../range", |
| 17 |
"dojo/i18n!../nls/FontChoice" |
| 18 |
], function(require, array, declare, domConstruct, i18n, lang, stringUtil, MemoryStore, |
| 19 |
registry, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, FilteringSelect, _Plugin, rangeapi){ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
var _FontDropDown = declare("dijit._editor.plugins._FontDropDown", |
| 25 |
[_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
label: "", |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
plainText: false, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
templateString: "<span style='white-space: nowrap' class='dijit dijitReset dijitInline'>" + |
| 43 |
"<label class='dijitLeft dijitInline' for='${selectId}'>${label}</label>" + |
| 44 |
"<input data-dojo-type='../../form/FilteringSelect' required='false' " + |
| 45 |
"data-dojo-props='labelType:\"html\", labelAttr:\"label\", searchAttr:\"name\"' " + |
| 46 |
"class='${comboClass}' " + |
| 47 |
"tabIndex='-1' id='${selectId}' data-dojo-attach-point='select' value=''/>" + |
| 48 |
"</span>", |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
contextRequire: require, |
| 53 |
|
| 54 |
postMixInProperties: function(){ |
| 55 |
|
| 56 |
|
| 57 |
this.inherited(arguments); |
| 58 |
|
| 59 |
this.strings = i18n.getLocalization("dijit._editor", "FontChoice"); |
| 60 |
|
| 61 |
|
| 62 |
this.label = this.strings[this.command]; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
this.id = registry.getUniqueId(this.declaredClass.replace(/\./g, "_")); |
| 68 |
|
| 69 |
this.selectId = this.id + "_select"; |
| 70 |
|
| 71 |
this.inherited(arguments); |
| 72 |
}, |
| 73 |
|
| 74 |
postCreate: function(){ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
this.select.set("store", new MemoryStore({ |
| 82 |
idProperty: "value", |
| 83 |
data: array.map(this.values, function(value){ |
| 84 |
var name = this.strings[value] || value; |
| 85 |
return { |
| 86 |
label: this.getLabel(value, name), |
| 87 |
name: name, |
| 88 |
value: value |
| 89 |
}; |
| 90 |
}, this) |
| 91 |
})); |
| 92 |
|
| 93 |
this.select.set("value", "", false); |
| 94 |
this.disabled = this.select.get("disabled"); |
| 95 |
}, |
| 96 |
|
| 97 |
_setValueAttr: function(value, priorityChange){ |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
priorityChange = priorityChange !== false; |
| 109 |
this.select.set('value', array.indexOf(this.values, value) < 0 ? "" : value, priorityChange); |
| 110 |
if(!priorityChange){ |
| 111 |
|
| 112 |
this.select._lastValueReported = null; |
| 113 |
} |
| 114 |
}, |
| 115 |
|
| 116 |
_getValueAttr: function(){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
return this.select.get('value'); |
| 121 |
}, |
| 122 |
|
| 123 |
focus: function(){ |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
this.select.focus(); |
| 128 |
}, |
| 129 |
|
| 130 |
_setDisabledAttr: function(value){ |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
this._set("disabled", value); |
| 138 |
this.select.set("disabled", value); |
| 139 |
} |
| 140 |
}); |
| 141 |
|
| 142 |
|
| 143 |
var _FontNameDropDown = declare("dijit._editor.plugins._FontNameDropDown", _FontDropDown, { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
generic: false, |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
command: "fontName", |
| 154 |
|
| 155 |
comboClass: "dijitFontNameCombo", |
| 156 |
|
| 157 |
postMixInProperties: function(){ |
| 158 |
|
| 159 |
|
| 160 |
if(!this.values){ |
| 161 |
this.values = this.generic ? |
| 162 |
["serif", "sans-serif", "monospace", "cursive", "fantasy"] : |
| 163 |
["Arial", "Times New Roman", "Comic Sans MS", "Courier New"]; |
| 164 |
} |
| 165 |
this.inherited(arguments); |
| 166 |
}, |
| 167 |
|
| 168 |
getLabel: function(value, name){ |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if(this.plainText){ |
| 178 |
return name; |
| 179 |
}else{ |
| 180 |
return "<div style='font-family: " + value + "'>" + name + "</div>"; |
| 181 |
} |
| 182 |
}, |
| 183 |
|
| 184 |
_normalizeFontName: function (value) { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
var allowedValues = this.values; |
| 189 |
if (!value || !allowedValues) { |
| 190 |
return value; |
| 191 |
} |
| 192 |
var fontNames = value.split(','); |
| 193 |
if (fontNames.length > 1) { |
| 194 |
for (var i = 0, l = fontNames.length; i < l; i++) { |
| 195 |
var fontName = stringUtil.trim(fontNames[i]); |
| 196 |
var pos = array.indexOf(allowedValues, fontName); |
| 197 |
if (pos > -1) { |
| 198 |
return fontName; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
return value; |
| 203 |
}, |
| 204 |
|
| 205 |
_setValueAttr: function(value, priorityChange){ |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
priorityChange = priorityChange !== false; |
| 211 |
value = this._normalizeFontName(value); |
| 212 |
if(this.generic){ |
| 213 |
var map = { |
| 214 |
"Arial": "sans-serif", |
| 215 |
"Helvetica": "sans-serif", |
| 216 |
"Myriad": "sans-serif", |
| 217 |
"Times": "serif", |
| 218 |
"Times New Roman": "serif", |
| 219 |
"Comic Sans MS": "cursive", |
| 220 |
"Apple Chancery": "cursive", |
| 221 |
"Courier": "monospace", |
| 222 |
"Courier New": "monospace", |
| 223 |
"Papyrus": "fantasy", |
| 224 |
"Estrangelo Edessa": "cursive", |
| 225 |
"Gabriola": "fantasy" |
| 226 |
}; |
| 227 |
value = map[value] || value; |
| 228 |
} |
| 229 |
this.inherited(arguments, [value, priorityChange]); |
| 230 |
} |
| 231 |
}); |
| 232 |
|
| 233 |
var _FontSizeDropDown = declare("dijit._editor.plugins._FontSizeDropDown", _FontDropDown, { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
command: "fontSize", |
| 240 |
|
| 241 |
comboClass: "dijitFontSizeCombo", |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
values: [1, 2, 3, 4, 5, 6, 7], |
| 246 |
|
| 247 |
getLabel: function(value, name){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if(this.plainText){ |
| 259 |
return name; |
| 260 |
}else{ |
| 261 |
return "<font size=" + value + "'>" + name + "</font>"; |
| 262 |
} |
| 263 |
}, |
| 264 |
|
| 265 |
_setValueAttr: function(value, priorityChange){ |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
priorityChange = priorityChange !== false; |
| 270 |
if(value.indexOf && value.indexOf("px") != -1){ |
| 271 |
var pixels = parseInt(value, 10); |
| 272 |
value = {10: 1, 13: 2, 16: 3, 18: 4, 24: 5, 32: 6, 48: 7}[pixels] || value; |
| 273 |
} |
| 274 |
|
| 275 |
this.inherited(arguments, [value, priorityChange]); |
| 276 |
} |
| 277 |
}); |
| 278 |
|
| 279 |
|
| 280 |
var _FormatBlockDropDown = declare("dijit._editor.plugins._FormatBlockDropDown", _FontDropDown, { |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
command: "formatBlock", |
| 287 |
|
| 288 |
comboClass: "dijitFormatBlockCombo", |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
values: ["noFormat", "p", "h1", "h2", "h3", "pre"], |
| 293 |
|
| 294 |
postCreate: function(){ |
| 295 |
|
| 296 |
|
| 297 |
this.inherited(arguments); |
| 298 |
this.set("value", "noFormat", false); |
| 299 |
}, |
| 300 |
|
| 301 |
getLabel: function(value, name){ |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if(this.plainText || value == "noFormat"){ |
| 311 |
return name; |
| 312 |
}else{ |
| 313 |
return "<" + value + ">" + name + "</" + value + ">"; |
| 314 |
} |
| 315 |
}, |
| 316 |
|
| 317 |
_execCommand: function(editor, command, choice){ |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
if(choice === "noFormat"){ |
| 322 |
var start; |
| 323 |
var end; |
| 324 |
var sel = rangeapi.getSelection(editor.window); |
| 325 |
if(sel && sel.rangeCount > 0){ |
| 326 |
var range = sel.getRangeAt(0); |
| 327 |
var node, tag; |
| 328 |
if(range){ |
| 329 |
start = range.startContainer; |
| 330 |
end = range.endContainer; |
| 331 |
|
| 332 |
|
| 333 |
while(start && start !== editor.editNode && |
| 334 |
start !== editor.document.body && |
| 335 |
start.nodeType !== 1){ |
| 336 |
start = start.parentNode; |
| 337 |
} |
| 338 |
|
| 339 |
while(end && end !== editor.editNode && |
| 340 |
end !== editor.document.body && |
| 341 |
end.nodeType !== 1){ |
| 342 |
end = end.parentNode; |
| 343 |
} |
| 344 |
|
| 345 |
var processChildren = lang.hitch(this, function(node, ary){ |
| 346 |
if(node.childNodes && node.childNodes.length){ |
| 347 |
var i; |
| 348 |
for(i = 0; i < node.childNodes.length; i++){ |
| 349 |
var c = node.childNodes[i]; |
| 350 |
if(c.nodeType == 1){ |
| 351 |
if(editor.selection.inSelection(c)){ |
| 352 |
var tag = c.tagName ? c.tagName.toLowerCase() : ""; |
| 353 |
if(array.indexOf(this.values, tag) !== -1){ |
| 354 |
ary.push(c); |
| 355 |
} |
| 356 |
processChildren(c, ary); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
}); |
| 362 |
|
| 363 |
var unformatNodes = lang.hitch(this, function(nodes){ |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if(nodes && nodes.length){ |
| 369 |
editor.beginEditing(); |
| 370 |
while(nodes.length){ |
| 371 |
this._removeFormat(editor, nodes.pop()); |
| 372 |
} |
| 373 |
editor.endEditing(); |
| 374 |
} |
| 375 |
}); |
| 376 |
|
| 377 |
var clearNodes = []; |
| 378 |
if(start == end){ |
| 379 |
|
| 380 |
|
| 381 |
var block; |
| 382 |
node = start; |
| 383 |
while(node && node !== editor.editNode && node !== editor.document.body){ |
| 384 |
if(node.nodeType == 1){ |
| 385 |
tag = node.tagName ? node.tagName.toLowerCase() : ""; |
| 386 |
if(array.indexOf(this.values, tag) !== -1){ |
| 387 |
block = node; |
| 388 |
break; |
| 389 |
} |
| 390 |
} |
| 391 |
node = node.parentNode; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
processChildren(start, clearNodes); |
| 397 |
if(block){ |
| 398 |
clearNodes = [block].concat(clearNodes); |
| 399 |
} |
| 400 |
unformatNodes(clearNodes); |
| 401 |
}else{ |
| 402 |
|
| 403 |
node = start; |
| 404 |
while(editor.selection.inSelection(node)){ |
| 405 |
if(node.nodeType == 1){ |
| 406 |
tag = node.tagName ? node.tagName.toLowerCase() : ""; |
| 407 |
if(array.indexOf(this.values, tag) !== -1){ |
| 408 |
clearNodes.push(node); |
| 409 |
} |
| 410 |
processChildren(node, clearNodes); |
| 411 |
} |
| 412 |
node = node.nextSibling; |
| 413 |
} |
| 414 |
unformatNodes(clearNodes); |
| 415 |
} |
| 416 |
editor.onDisplayChanged(); |
| 417 |
} |
| 418 |
} |
| 419 |
}else{ |
| 420 |
editor.execCommand(command, choice); |
| 421 |
} |
| 422 |
}, |
| 423 |
|
| 424 |
_removeFormat: function(editor, node){ |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if(editor.customUndo){ |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
while(node.firstChild){ |
| 435 |
domConstruct.place(node.firstChild, node, "before"); |
| 436 |
} |
| 437 |
node.parentNode.removeChild(node); |
| 438 |
}else{ |
| 439 |
|
| 440 |
|
| 441 |
editor.selection.selectElementChildren(node); |
| 442 |
var html = editor.selection.getSelectedHtml(); |
| 443 |
editor.selection.selectElement(node); |
| 444 |
editor.execCommand("inserthtml", html || ""); |
| 445 |
} |
| 446 |
} |
| 447 |
}); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
var FontChoice = declare("dijit._editor.plugins.FontChoice", _Plugin, { |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
useDefaultCommand: false, |
| 482 |
|
| 483 |
_initButton: function(){ |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
var clazz = { |
| 492 |
fontName: _FontNameDropDown, |
| 493 |
fontSize: _FontSizeDropDown, |
| 494 |
formatBlock: _FormatBlockDropDown |
| 495 |
}[this.command], |
| 496 |
params = this.params; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if(this.params.custom){ |
| 501 |
params.values = this.params.custom; |
| 502 |
} |
| 503 |
|
| 504 |
var editor = this.editor; |
| 505 |
this.button = new clazz(lang.delegate({dir: editor.dir, lang: editor.lang}, params)); |
| 506 |
|
| 507 |
|
| 508 |
this.own(this.button.select.on("change", lang.hitch(this, function(choice){ |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
if(this.editor.focused){ |
| 513 |
|
| 514 |
this.editor.focus(); |
| 515 |
} |
| 516 |
|
| 517 |
if(this.command == "fontName" && choice.indexOf(" ") != -1){ |
| 518 |
choice = "'" + choice + "'"; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if(this.button._execCommand){ |
| 524 |
this.button._execCommand(this.editor, this.command, choice); |
| 525 |
}else{ |
| 526 |
this.editor.execCommand(this.command, choice); |
| 527 |
} |
| 528 |
}))); |
| 529 |
}, |
| 530 |
|
| 531 |
updateState: function(){ |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
var _e = this.editor; |
| 541 |
var _c = this.command; |
| 542 |
if(!_e || !_e.isLoaded || !_c.length){ |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
if(this.button){ |
| 547 |
var disabled = this.get("disabled"); |
| 548 |
this.button.set("disabled", disabled); |
| 549 |
if(disabled){ |
| 550 |
return; |
| 551 |
} |
| 552 |
var value; |
| 553 |
try{ |
| 554 |
value = _e.queryCommandValue(_c) || ""; |
| 555 |
}catch(e){ |
| 556 |
|
| 557 |
value = ""; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
var quoted = lang.isString(value) && (value.match(/'([^']*)'/) || value.match(/"([^"]*)"/)); |
| 562 |
if(quoted){ |
| 563 |
value = quoted[1]; |
| 564 |
} |
| 565 |
if (_c === "fontSize" && !value) { |
| 566 |
value = 3; |
| 567 |
} |
| 568 |
if(_c === "formatBlock"){ |
| 569 |
if(!value || value == "p"){ |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
value = null; |
| 574 |
var elem; |
| 575 |
|
| 576 |
var sel = rangeapi.getSelection(this.editor.window); |
| 577 |
if(sel && sel.rangeCount > 0){ |
| 578 |
var range = sel.getRangeAt(0); |
| 579 |
if(range){ |
| 580 |
elem = range.endContainer; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
while(elem && elem !== _e.editNode && elem !== _e.document){ |
| 586 |
var tg = elem.tagName ? elem.tagName.toLowerCase() : ""; |
| 587 |
if(tg && array.indexOf(this.button.values, tg) > -1){ |
| 588 |
value = tg; |
| 589 |
break; |
| 590 |
} |
| 591 |
elem = elem.parentNode; |
| 592 |
} |
| 593 |
if(!value){ |
| 594 |
|
| 595 |
value = "noFormat"; |
| 596 |
} |
| 597 |
}else{ |
| 598 |
|
| 599 |
|
| 600 |
if(array.indexOf(this.button.values, value) < 0){ |
| 601 |
value = "noFormat"; |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
if(value !== this.button.get("value")){ |
| 606 |
|
| 607 |
|
| 608 |
this.button.set('value', value, false); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
}); |
| 613 |
|
| 614 |
|
| 615 |
array.forEach(["fontName", "fontSize", "formatBlock"], function(name){ |
| 616 |
_Plugin.registry[name] = function(args){ |
| 617 |
return new FontChoice({ |
| 618 |
command: name, |
| 619 |
plainText: args.plainText |
| 620 |
}); |
| 621 |
}; |
| 622 |
}); |
| 623 |
|
| 624 |
|
| 625 |
FontChoice._FontDropDown = _FontDropDown; |
| 626 |
FontChoice._FontNameDropDown = _FontNameDropDown; |
| 627 |
FontChoice._FontSizeDropDown = _FontSizeDropDown; |
| 628 |
FontChoice._FormatBlockDropDown = _FormatBlockDropDown; |
| 629 |
return FontChoice; |
| 630 |
|
| 631 |
}); |