/
editor.js
executable file
·2104 lines (1850 loc) · 63.2 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(window.gloader || glow).module({
name: 'glow.widgets.Editor',
library: ['glow', '@VERSION@'],
depends: [[
'glow', '@VERSION@',
'glow.dom',
'glow.events',
'glow.widgets',
'glow.i18n',
'glow.widgets.Overlay'
]],
builder: function(glow) {
var $ = glow.dom.get, // shortcuts
events = glow.events,
$i18n = glow.i18n;
$i18n.addLocaleModule("GLOW_WIDGETS_EDITOR", "en", {
ENTER_MESSAGE : "You are about to enter a Rich Text Editor",
SKIP_LINK_TEXT : "Skip past",
LEAVE_MESSAGE : "You have left the Rich Text Editor",
BOLD_TITLE : "Bold",
BOLD_LABEL : "B",
ITALICS_TITLE : "Italics",
ITALICS_LABEL : "I",
STRIKE_TITLE : "Strikethrough",
STRIKE_LABEL : "Strike",
UNORDERED_TITLE : "Unordered list",
UNORDERED_LABEL : "unordered list",
ORDERED_TITLE : "Ordered list",
ORDERED_LABEL : "ordered list",
FORMATBLOCK_TITLE : "Text style",
FORMATBLOCK_LABEL : "text style",
HEADINGLEVELONE_TITLE : "Heading 1",
HEADINGLEVELTWO_TITLE : "Heading 2",
HEADINGLEVELTHREE_TITLE : "Heading 3",
NORMAL_TITLE : "Normal"
/*
BLOCK_TITLE : "Blockquote",
BLOCK_LABEL : "blockquote"
HEADING1_TITLE : "Heading1",
HEADING1_LABEL : "Heading1",
TOGGLE_TITLE : "toggle",
TOGGLE_LABEL : "toggle",
** DONT FORGET THE OTHER LOCAL PACKS (eg CY)
*/
});
/**
@name glow.widgets.Editor
@class
@description A Rich Text Editor to allow text formatting with form inputs.
<div class="info">Widgets must be called in a <code>glow.ready()</code> call.</div>
@constructor
@param {String|glow.dom.NodeList} textarea Textarea HTML element that the Editor replaces on your page
@param {Object} opts
@_param {String} [opts.toolset="basic"]
@param {String} [opts.theme="light"] Visual Theme
Currently supported themes are "dark" and "light".
@param {Function} [opts.onCommit] Event shortcut.
See documentation below
@_property {glow.dom.NodeList} element
@_property {glow.dom.NodeList} textarea
@_property {glow.widgets.Editor.Toolbar} toolbar
@_property {glow.widgets.Editor.EditArea} editArea
@example
var editor = new glow.widgets.Editor("#commentEntry");
@see <a href="../furtherinfo/widgets/editor/">Editor user guide</a>
*/
/**
@name glow.widgets.Editor#event:commit
@event
@description Fired whenever the associated textarea is updated by the editor.
@param {glow.events.Event} event Event Object
*/
glow.widgets.Editor = function(textarea, opts) {
textarea = $(textarea);
var editorLocaleModule = $i18n.getLocaleModule("GLOW_WIDGETS_EDITOR");
this._tools = createTools(editorLocaleModule);
opts = this._opts = glow.lang.apply(
{
toolset: "basic",
onCommit: null
},
opts
);
// interpolate context
this.element = glow.dom.create('<div class="glowCSSVERSION-editor"><p class="glowCSSVERSION-hidden">{ENTER_MESSAGE}, <a href="#endOfEditor' + endOfEditorCounter() + '" tabindex="0">{SKIP_LINK_TEXT}</a></p><div class="editor-' + (opts.theme || "light") + '"><div class="editor-state"></div></div><p id="endOfEditor' + endOfEditorCounter() + '" class="glowCSSVERSION-hidden endOfEditorCounter" tabindex="0">{LEAVE_MESSAGE}</p></div>', {interpolate : editorLocaleModule});
this.textarea = textarea;
this.toolbar = new glow.widgets.Editor.Toolbar(this);
if (this._opts.toolset == "basic") {
this.toolbar._addToolset("italics", "bold", "strike", "formatBlock", /*"blockquote",*/ "unorderedlist", "orderedlist");
}
else throw new Exception("Unknown toolset name.");
this.editArea = new glow.widgets.Editor.EditArea(this);
this.cleaner = new TagCleaner();
// Safari 2 is not enhanced
if (!isSafariTwo()) {
place.apply(this);
bindEditor.apply(this, []);
}
if (opts.onCommit){
events.addListener(this, "commit", opts.onCommit);
}
}
////
/**
@ignore
@private
@name endOfEditorCounter
@description We need to give each hidden accessibility hidden message a unique id, so to do this add a number on the end. Work out the number by counting how many of these links are already on the page.
*/
var endOfEditorCounter = function() {
return glow.dom.get('p.endOfEditorCounter').length+1;
}
/**
@ignore
@private
@name Idler
@constructor
@param {String|Object} attachTo The object to attach the idler to.
@param {String|String[]} name The event name, or names for multiple events.
@param {Number} wait The number of seconds to wait after no event occurs before the callback should run.
@param {Object} [opts] Options object
@param {Function} [opts.onFire] Function to run when this Idler fires.
@param {Number} [opts.rate] Repeat the fire event every x seconds
*/
var Idler = function(attachTo, name, wait, opts) { /*debug*///console.info("new Idler()");
opts = this._opts = glow.lang.apply(
{
onFire: function() {}
},
opts
);
var that = this;
this.attachTo = attachTo;
this.name = name;
this.wait = wait;
this.callback = opts.onFire;
this.rate = opts.rate;
this.running = false;
this.initiated = false;
if (typeof this.name.pop == "undefined") { // is it an array?
this.name = [this.name];
}
for (var i = 0, l = this.name.length; i < l; i++) {
var name = this.name[i];
glow.events.addListener(
this.attachTo,
name,
function() {
clearInterval(that.intervalId);
clearTimeout(that.timeoutId);
that._tick();
}
);
}
this._start();
}
/**
@ignore
@private
@name Idler#disabled
@function
@param {Boolean} [disabledState]
@description Sets/gets the disabled state of the Idler. When disabled, any running
timers are cancelled and no new timers can be started.
*/
Idler.prototype.disabled = function(disabledState) {
if (typeof disabledState == "undefined") {
return !this.running;
}
else {
if (disabledState) this._stop();
else this._start();
}
}
Idler.prototype._tick = function() {/*debug*///console.info("Idler._tick()");
var that = this;
this.timeoutId = setTimeout(
function() {
if (typeof that.rate != "undefined") {
that.intervalId = setInterval(that.callback, that.rate);
}
else {
that.callback();
}
},
that.wait
);
}
/**
@ignore
@private
@name Idler#_start
@function
@description Start the idler, if it is not already running.
*/
Idler.prototype._start = function() {
if (this.running) return;
this._tick();
this.running = true;
}
/**
@ignore
@private
@name Idler#_stop
@function
@description Stop the idler, if it is running.
*/
Idler.prototype._stop = function() {
if (!this.running) return;
clearInterval(this.intervalId);
clearTimeout(this.timeoutId);
this.running = false;
}
////
/**
@ignore
@private
@name place
@function
@description Positions the toolbar over the textarea.
@this glow.widgets.Editor.Toolbar
*/
function place() {
var inputOffset = this.textarea.offset();
var height = (this.textarea[0].offsetHeight > 142) ? this.textarea[0].offsetHeight : 142;
this.element.css('width', (this.textarea[0].offsetWidth-2) + 'px');
this.element.css('height', (height-2) + 'px');
}
var bindEditor = function() {
// Add the widget into the page
this.textarea.before(this.element);
// Redo height of the iframe. We need to do it here because we can only get the correct height when the element is in the page.
this.element.get('iframe').css( 'height', (parseInt(this.element.css('height'))-42) );
// Set the textarea so the user can't see it (but a screen reader can)
this.textarea.css("display", "block");
this.textarea.css("position", "absolute");
this.textarea.css("left", "-9999px");
this.textarea.css("top", "-9999px");
this.bound = true;
}
////////
glow.widgets.Editor.prototype.inject = function(html) {
this.editArea._setContent(this.cleaner.dirty(this.cleaner.clean(html)));
}
glow.widgets.Editor.prototype.commit = function() {
if (this.bound) {
$(this.textarea).val(this.cleaner.clean(this.editArea._getContent()));
}
glow.events.fire(this, "commit", {});
}
function TagCleaner(opts) {
// assumes nesting of dirty tags is always valid
// assumes no '>' in any dirty tag attributes
// assumes attributes are quoted with double-quotes, and attribute values contain no escaped double-quotes
this.opts = opts || {};
this.whitelist = ["em", "strong", "strike", "p", "br", /*"blockquote",*/ "ul", "ol", "li", "h1", "h2", "h3"]; // TODO: support tags.attributes
}
// runs before clean
TagCleaner.prototype.pretreat = function(input) {
// remove html comments
input = input.replace(/<!--[\s\S]*?-->/g, "");
// remove style tags and their contents
input = input.replace(/<style\b[\s\S]*?<\/style>/gi, "");
// remove script tags and their contents
input = input.replace(/<script\b[\s\S]*?<\/script>/gi, "");
return input
}
TagCleaner.prototype.clean = function(input) { /*debug*///console.log("TagCleaner#clean("+input+")")
var output = "",
stack = [];
input = this.pretreat(input);
//var sanity = 99;
while (input) {
//if (sanity-- == 0) throw new Error("stoopid loops");
var skip = 1; // characters
if (/^(<[^>]+>)/.test(input)) { // tag encountered
var foundTag = new TagCleaner.Tag(RegExp.$1);
this.tagClean(foundTag);
if (foundTag.clean && foundTag.opening) { // there's a clean version
output += foundTag.clean.start;
if (!foundTag.unary) stack.unshift(foundTag);
skip = foundTag.text.length;
}
else if (stack[0] && input.toLowerCase().indexOf(stack[0].end) === 0) { // found tag was closed
output += stack[0].clean.end;
skip = stack[0].end.length;
stack.shift();
}
else { // unknown tag
output += foundTag;
skip = foundTag.text.length;
}
}
else { // non-tag content
output += input.charAt(0);
}
// move ahead
input = input.substring(skip);
}
output = this.spin(output);
return output;
}
TagCleaner.prototype.dirty = function(clean) {
var dirty;
if (glow.env.gecko) { // mozilla?
dirty = clean
.replace(/<strong>/g, '<b _moz_dirty="">').replace(/<\/strong>/g, '</b>')
.replace(/<em>/g, '<i _moz_dirty="">').replace(/<\/em>/g, '</i>')
.replace(/<strike>/g, '<strike _moz_dirty="">')
;
}
else if (glow.env.ie || glow.env.opera) {
dirty = clean
.replace(/<strong>/g, '<STRONG>').replace(/<\/strong>/g, '</STRONG>')
.replace(/<em>/g, '<EM>').replace(/<\/em>/g, '</EM>')
.replace(/<strike>/g, '<STRIKE>').replace(/<\/strike>/g, '</STRIKE>')
;
}
else if (glow.env.webkit > 528) { // safari 4? TODO: same as safari 2?
dirty = clean
.replace(/<strong>/g, '<b>').replace(/<\/strong>/g, '</b>')
.replace(/<em>/g, '<i>').replace(/<\/em>/g, '</i>')
.replace(/<strike>/g, '<span class="Apple-style-span" style="text-decoration: line-through;">').replace(/<\/strike>/g, '</span>')
;
}
else if (glow.env.webkit) { // safari 3?
dirty = clean
.replace(/<strong>/g, '<span class="Apple-style-span" style="font-weight: bold;">').replace(/<\/strong>/g, '</span>')
.replace(/<em>/g, '<span class="Apple-style-span" style="font-style: italic;">').replace(/<\/em>/g, '</span>')
.replace(/<strike>/g, '<span class="Apple-style-span" style="text-decoration: line-through;">').replace(/<\/strike>/g, '</span>')
;
}
else { throw new Error("Can't be dirty: Unknown browser."); }
return dirty;
}
/**
@ignore
@private
@description A single span can become a combination of several semantic tags.
@param {TagCleaner.Tag} tag A span Tag object.
@returns {String[]} An array of [0] opening tag or tags as a string and [1] closing tag or tags as a string.
*/
TagCleaner.prototype.spanClean = function(tag) {
var clean = {start:"", end:""};
if (/\bstyle\s*=\s*"(.+)"/.test(tag.attrText.toLowerCase())) {
if (RegExp.$1.indexOf("bold") > -1) {
clean.start += "<strong>";
clean.end = "<\/strong>"+clean.end;
}
// safari needs this
if (RegExp.$1.indexOf("font-weight: normal") > -1) {
clean.start += "<\/strong>";
clean.end = "<strong>"+clean.end;
}
if (RegExp.$1.indexOf("italic") > -1) {
clean.start += "<em>";
clean.end = "<\/em>"+clean.end;
}
// safari needs this
if (RegExp.$1.indexOf("font-style: normal") > -1) {
clean.start += "<\/em>";
clean.end = "<em>"+clean.end;
}
if (RegExp.$1.indexOf("line-through") > -1) {
clean.start += "<strike>";
clean.end = "<\/strike>"+clean.end;
}
}
return clean;
}
/**
@ignore
@private
@description Given a dirty tag, add a clean tag property, if one can be found.
@param {TagCleaner.Tag} tag A dirty Tag object.
@returns {String[]} An array of [0] opening tag or tags as a string and [1] closing tag or tags as a string.
*/
TagCleaner.prototype.tagClean = function(tag) {
var clean = ["", ""];
if (tag.name == "span") clean = this.spanClean(tag);
else if (tag.name == "b") clean = {start:'<strong>', end:'<\/strong>'};
else if (tag.name == "i") clean = {start:'<em>', end:'<\/em>'};
if (clean.start) tag.clean = clean;
}
TagCleaner.Tag = function(tagText) { /*debug*///console.log("new TagCleaner.Tag("+tagText+")");
/^<(\/?)([a-zA-Z1-6]+)\b(.*)( ?\/)?>$/.exec(tagText);
this.closing = !!RegExp.$1;
this.opening = !this.closing;
this.unary = !!RegExp.$4;
this.name = RegExp.$2.toLowerCase();
this.attrText = RegExp.$3;
this.text = tagText;
// normalise case of tag names
this.start = tagText.replace(/^<(\/?)([a-zA-Z]+)\b/, "<$1"+this.name);
if (this.opening && !this.unary) {
this.end = "<\/"+this.name+">";
}
}
TagCleaner.Tag.prototype.toString = function() {
return "<" + RegExp.$1 + this.name/* + this.attrText */+ RegExp.$4 +">";
}
TagCleaner.prototype.spin = function(input) {
var whitetags = this.whitelist.join("\|");
// note: Safari 2.0.4 doesn't support unicode in regex, but can use unprintable ASCII characters, like the "Group Separator"
var allowedTags = new RegExp("<(\\/?("+whitetags+")\\b[^>]*)>", "g");
input = input.replace(allowedTags, "\x1D$1\x1D"); // hide allowed tags
input = input.replace(/<[^>]+>/g, ""); // kill all visible tags
input = input.replace(/\x1D([^\x1D]+)\x1D/g, "<$1>"); // restore allowed tags
// general final clean up...
input = input.replace(/<>/g, ""); // remove Safari droppings
return input;
}
////////
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar
@description
@constructor
@_param {glow.widgets.Editor} editor
@_param {Object} opts
*/
glow.widgets.Editor.Toolbar = function(editor, opts) {
opts = opts || {};
this.editor = editor;
this.element = glow.dom.create('<fieldset class="editor-toolbar"><ul class="editor-toolbar-tools"></ul></fieldset>');
this._tools = [];
this.editor.element.get(".editor-state").prepend(this.element);
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#_addToolset
@description Quickly add several tools at once to the toolbar.
@function
@param {String} ... Either the name of a built-in set of tools,
like "fontFormatting" [TBD] and/or the predefined names of
built-in tools.
@returns {glow.widgets.Editor.Toolbar} this
@example
myToolbar._addToolset("Bold", "Italics")
.addButton("MyCustomButton", opts); // will be chainable
*/
glow.widgets.Editor.Toolbar.prototype._addToolset = function(/*arguments*/) {
var toolToAdd;
for (var i = 0, l = arguments.length; i < l; i++) {
if ( (toolToAdd = this.editor._tools[arguments[i]]) ) {
toolToAdd.opts.theme = this.editor._opts.theme; // tools inherit theme from associated editor theme
addTool.call(this, glow.widgets.Editor.Toolbar.prototype._toolFactory(toolToAdd));
}
}
// Give the toolbar one focusable button
// this.element.get('a').item(0).tabIndex = 0;
addToolbarIntoTabIndex.apply(this);
return this;
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#_toolFactory
@description Factory for building different kinds of toolbar tools.
@function
@param {this.editor._tools}
@returns {glow.widgets.Editor.Toolbar.tool} this
*/
glow.widgets.Editor.Toolbar.prototype._toolFactory = function(toolToAdd, editorRef) {
var newTool;
switch(toolToAdd.type) {
case "button":
newTool = new glow.widgets.Editor.Toolbar.Button(toolToAdd.name, toolToAdd.opts);
break;
case "dropDown":
newTool = new glow.widgets.Editor.Toolbar.DropDown(toolToAdd.name, toolToAdd.opts);
break;
}
return newTool;
}
// modifies HTML in place, while preserving the cursor position
glow.widgets.Editor.blackList = {
FORM: true,
TABLE: true,
TBODY: true,
CAPTION: true,
TH: true,
TR: true,
TD: true,
SCRIPT: true,
STYLE: true,
INPUT: true,
BUTTON: true,
OBJECT: true,
EMBED: true,
SELECT: true,
// H1: true,
// H2: true,
// H3: true,
H4: true,
H5: true,
H6: true,
DIV: true,
ADDRESS: true,
// BLOCKQUOTE: true,
CENTER: true,
PRE: true,
CODE: true,
A: true,
// UL: true,
// OL: true,
// LI: true,
DL: true,
DT: true,
DD: true,
ABBR: true,
ACRONYM: true,
DFN: true,
INS: true,
DEL: true,
SAMP: true,
VAR: true,
BIG: true,
SMALL: true,
BLINK: true,
MARQUEE: true,
FONT: true,
Q: true,
U: true,
KBD: true,
SUB: true,
SUP: true,
CITE: true,
HTML: true,
BODY: true,
FIELDSET: true,
LEGEND: true,
LABEL: true,
TEXTAREA: true,
HR: true,
IMG: true,
IFRAME: true,
ILAYER: true,
LAYER: true
};
glow.widgets.Editor.prototype._rinse = function() { /*debug*///console.log("glow.widgets.Editor#_rinse()");
if (this._lastRinse == this.editArea._getContent()) return; /*debug*///console.log("rinsing");
var doc = this.editArea.contentWindow.document;
var node = doc.body;
var that = this; // keep track of me, even when recursing
function walkNode(node) {
if (node.childNodes) {
for (var i = 0; i < node.childNodes.length; i++) {
var keepStatus = glow.widgets.Editor.blackList[node.childNodes[i].nodeName];
if (node.nodeType == 1) { // an element node
if (keepStatus) {
var replacementNode = doc.createElement("SPAN");
//replacementNode.setAttribute('class', 'glow-rinsed');
replacementNode.innerHTML = that.cleaner.clean(node.childNodes[i].innerHTML+" ");
node.replaceChild(replacementNode, node.childNodes[i]);
}
else {
// it's an allowed node but we should limit external styles as much as possible
if (node.childNodes[i].nodeName == "P") node.childNodes[i].removeAttribute("style");
if (node.childNodes[i].nodeName == "SPAN") { // webkit may use font-size spans to show headings
if (/font-size/.test(node.childNodes[i].getAttribute("style"))) node.childNodes[i].removeAttribute("style");
}
walkNode(node.childNodes[i]);
}
}
}
}
else {
if (glow.widgets.Editor.blackList[node.nodeName]) {
node.parentNode.removeChild(node);
}
}
}
walkNode(node);
this._lastRinse = this.editArea._getContent();
}
/**
@ignore
@name addTool
@private
@function
@param {glow.widgets.Editor.Toolbar.Tool} toolToAdd
*/
function addTool(toolToAdd) { /*debug*///console.log("addTool("+toolToAdd+")")
toolToAdd.editor = this.editor;
this._tools.push(toolToAdd);
this.element.get(".editor-toolbar-tools").append(toolToAdd.element);
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#addButton
@description
@function
@param {String} name
@param {Object} opts
*/
glow.widgets.Editor.Toolbar.prototype.addButton = function(name, opts) {
var newTool = new glow.widgets.Editor.Toolbar.Button(name, opts, this);
addTool.call(this, newTool);
return this;
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#getTool
@description
@function
@param {String} name
@returns {glow.widgets.Editor.Toolbar.Tool}
*/
glow.widgets.Editor.Toolbar.prototype.getTool = function(name) {
var i = this._tools.length;
while (--i >= 0) {
if (this._tools[i].name == name) return this._tools[i];
}
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#_update
@description
@function
@param {String} name
@returns {glow.widgets.Editor.Toolbar.Tool}
*/
glow.widgets.Editor.Toolbar.prototype._update = function(domPath) { /*debug*///console.log("glow.widgets.Editor.Toolbar.prototype._update("+domPath+")")
var handled = false;
for (var i = 0, l = this._tools.length; i < l; i++) {
// ***
// The dropdown has multilple tag values, so we need to do things a bit different...
// MAYBE WE SHOULD MOVE THIS INTO THE DROPDOWN OBJECT?
if(this._tools[i].type == "dropdown") {
var regex = new RegExp("/\|(" + this._tools[i].tag + ")\|/"),
domPathMatches = domPath.match(regex);
if (domPathMatches != null) {
this._tools[i].label( this._tools[i].overlayMenu.getTitleFromTag(domPathMatches[0]) );
}
else {
this._tools[i].label( "Normal" );
}
}
else if (domPath.indexOf("|"+this._tools[i].tag+"|") > -1) {
this._tools[i].activate();
handled = true;
}
else {
this._tools[i].deactivate();
}
}
return handled;
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar#_shortcut
@description
@function
@param {String} name
@returns {glow.widgets.Editor.Toolbar.Tool}
*/
glow.widgets.Editor.Toolbar.prototype._shortcut = function(letter) {
var i = this._tools.length;
var handled = false;
while (--i >= 0) {
if (this._tools[i].shortcut == letter) {
this._tools[i].press();
return true;
}
}
return false;
}
////////
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool
@constructor
@description Generic base class for all Tools.
*/
glow.widgets.Editor.Toolbar.Tool = function(name, opts, context) { /*debug*///console.log("glow.widgets.Editor.Toolbar.Tool("+name+", "+opts.toSource()+")")
this.name = name;
this.opts = opts || {};
this.action = this.opts.action || function(){};
this.tag = this.opts.tag;
this.command = this.opts.command;
this.shortcut = this.opts.shortcut;
this.isActive = false;
this.isEnabled = true;
if (this.opts.onDeactivate) glow.events.addListener(this, "deactivate", this.opts.onDeactivate, context);
if (this.opts.onActivate) glow.events.addListener(this, "activate", this.opts.onActivate, context);
if (this.opts.onDisable) glow.events.addListener(this, "disable", this.opts.onDisable, context);
if (this.opts.onEnable) glow.events.addListener(this, "enable", this.opts.onEnable, context);
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool#activate
@description
@function
*/
glow.widgets.Editor.Toolbar.Tool.prototype.activate = function() { /*debug*///console.log(this.type+".activate()");
this.isActive = true;
glow.events.fire(this, 'activate');
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool#deactivate
@description
@function
*/
glow.widgets.Editor.Toolbar.Tool.prototype.deactivate = function() { /*debug*///console.log(this.type+".deactivate()");
this.isActive = false;
glow.events.fire(this, 'deactivate');
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool#disable
@description
@function
*/
glow.widgets.Editor.Toolbar.Tool.prototype.disable = function() {
this.isEnabled = false;
glow.events.fire(this, 'disable');
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool#enable
@description
@function
*/
glow.widgets.Editor.Toolbar.Tool.prototype.enable = function() {
this.isEnabled = true;
glow.events.fire(this, 'enable');
}
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Tool#press
@description
@function
*/
glow.widgets.Editor.Toolbar.Tool.prototype.press = function() {
if (this.isEnabled) {
this.action.call(this);
if (!this.isActive && this.type == "button") this.activate();
else this.deactivate();
this.editor._lastDomPath = null; // invalidate the current dom path (this is required on some browsers that "wrap" selections) to force ondompathchange to fire when the user clicks away from the current selection
}
}
////////
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.Button
@constructor
@extends glow.widgets.Editor.Toolbar.Tool
*/
glow.widgets.Editor.Toolbar.Button = function(name, opts) { /*debug*///console.log("new glow.widgets.Editor.Toolbar.Button("+name+", "+opts.toSource()+")")
this.Base = arguments.callee.base; this.base = this.Base.prototype;
this.Base.apply(this, arguments);
this.type = "button";
// a button's CSS classname is defined here
var buttonClass = name.toLowerCase() + "-button";
this.element = glow.dom.create('<li class="editor-toolbar-item"><span class="editor-toolbar-button"><a href="#" title="'+(opts.title || name)+'" tabindex="-1"><span class="editor-toolbar-icon '+buttonClass+'"><span>'+(opts.label || name)+'<\/span><\/span><\/a><\/span><\/li>');
// shortcuts
var toolLink = this.element.get("a");
this.icon = this.element.get(".editor-toolbar-icon");
var key_listener;
glow.events.addListener(this.icon, "mouseover", function() { if (this.isEnabled && !this.isActive) toolLink.addClass("hover"); }, this);
glow.events.addListener(toolLink, "focus", function() { if (this.isEnabled) {toolLink.addClass("hover"); key_listener = enable_key_listener(this);} }, this);
glow.events.addListener(this.icon, "mouseout", function() { toolLink.removeClass("hover"); }, this);
glow.events.addListener(toolLink, "blur", function() { toolLink.removeClass("hover"); glow.events.removeListener(key_listener);}, this);
glow.events.addListener(this, "disable", function() { toolLink.addClass("disabled"); }, this);
glow.events.addListener(this, "enable", function() { toolLink.removeClass("disabled"); }, this);
glow.events.addListener(this, "activate", function() { if (this.isEnabled) {toolLink.addClass("active");} }, this);
glow.events.addListener(this, "deactivate", function() { toolLink.removeClass("active"); }, this);
var that = this;
glow.events.addListener(this.element.get("a"), "mousedown", function() { that.press(); return false; }, this); // bind the click handler context to the Tool (not the HTMLElement)
glow.events.addListener(this.element.get("a"), "click", function() { return false; });
}
glow.lang.extend(glow.widgets.Editor.Toolbar.Button, glow.widgets.Editor.Toolbar.Tool);
/**
@ignore
@private
@name glow.widgets.Editor.Toolbar.DropDown
@constructor
@extends glow.widgets.Editor.Toolbar.Tool
*/
glow.widgets.Editor.Toolbar.DropDown = function(name, opts) { /*debug*///console.log("new glow.widgets.Editor.Toolbar.DropDown("+name+", "+opts.toSource()+")")
// this is at the begining of all the objects that extend glow.widgets.editor.toolbar.tool - is this the right way to do it?
this.Base = arguments.callee.base;
this.base = this.Base.prototype;
this.Base.apply(this, arguments);
this.type = "dropdown";
this._opts = {
title: opts.title || name,
label: opts.lable || name,
theme: opts.theme || 'light'
};
// create dropdown tool-link element
var buttonClass = name.toLowerCase() + "-dropDown"; // like: formatblock-dropDown
this.element = glow.dom.create('<li class="editor-toolbar-item"><span class="editor-toolbar-dropdown"><a href="#" title="'+this._opts.title+'" tabindex="-1"><span class="'+buttonClass+'"><span>'+this._opts.label+'<\/span><\/span><\/a><\/span><\/li>');
// shortcuts
var that = this,
toolLink = this.element.get("a");
this.icon = this.element.get(".editor-toolbar-dropdown");
// create overlaymenu child obect - this is used as the menu when the drop-down tool-link is clicked on
this.overlayMenu = new glow.widgets.Editor.Toolbar.OverlayMenu(
this,
{
menuItems: opts.menuItems,
onClick: function(e) {
// change the dropdown tool-link label
that.label(that.overlayMenu.menuItems[that.overlayMenu.selected].title);
// fire tool-link - again, this is done from the overlayMenu because the tool-link isn't being interacted with
//events.fire(that, "activate");
that.press();
//events.fire(that, "deactivate");
//that.deactivate();
if (glow.env.ie) {
that.editor.editArea.contentWindow.focus();
}
}
}
);
// getter/setter for the label of the tool-link
this.label = function(newLabel) {
if (typeof newLabel != "undefined") {
that.element.get("a span span").html(newLabel);
return this;
}
else {
return that.element.get("a span span").html();
}
}
// set the label to be the last text entry (normal text) in the overlaymenu
this.label(this.overlayMenu.menuItems[this.overlayMenu.selected].title);
// ***
// mouse events
// clicking on the tool-link opens the overlaymenu (closing the overlaymenu handled inside overlaymenu object)
glow.events.addListener(that.element.get("a"), "click", function() {
_openOverlayMenu();
return false;
});
glow.events.addListener(this.element.get("a"), "mousedown", function() { return false; });
// ***
// ***
// roll over events
glow.events.addListener(that.icon, "mouseover", function() {
if (this.isEnabled && !this.isActive) {
toolLink.addClass("hover");
}