/
editor.js
executable file
·1398 lines (1235 loc) · 51.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
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Atto editor.
*
* @package editor_atto
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Classes constants.
*/
CSS = {
CONTENT: 'editor_atto_content',
CONTENTWRAPPER: 'editor_atto_content_wrap',
TOOLBAR: 'editor_atto_toolbar',
WRAPPER: 'editor_atto',
HIGHLIGHT: 'highlight'
};
/**
* Atto editor main class.
* Common functions required by editor plugins.
*
* @package editor_atto
* @copyright 2013 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
M.editor_atto = M.editor_atto || {
/**
* List of known block level tags.
* Taken from "https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements".
*
* @type {Array}
*/
BLOCK_TAGS : [
'address',
'article',
'aside',
'audio',
'blockquote',
'canvas',
'dd',
'div',
'dl',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hgroup',
'hr',
'noscript',
'ol',
'output',
'p',
'pre',
'section',
'table',
'tfoot',
'ul',
'video'],
PLACEHOLDER_FONTNAME: 'yui-tmp',
ALL_NODES_SELECTOR: '[style],font[face]',
FONT_FAMILY: 'fontFamily',
/**
* List of attached button handlers to prevent duplicates.
*/
buttonhandlers : {},
/**
* List of attached handlers.
*/
textupdatedhandlers : {},
/**
* List of YUI overlays for custom menus.
*/
menus : {},
/**
* List of attached menu handlers to prevent duplicates.
*/
menuhandlers : {},
/**
* List of file picker options for specific editor instances.
*/
filepickeroptions : {},
/**
* List of buttons and menus that have been added to the toolbar.
*/
widgets : {},
/**
* List of saved selections per editor instance.
*/
selections : {},
focusfromclick : false,
/**
* Toggle a menu.
* @param event e
*/
showhide_menu_handler : function(e) {
e.preventDefault();
var disabled = this.getAttribute('disabled');
var overlayid = this.getAttribute('data-menu');
var overlay = M.editor_atto.menus[overlayid];
var menu = overlay.get('bodyContent');
if (overlay.get('visible') || disabled) {
overlay.hide();
menu.detach('clickoutside');
} else {
menu.on('clickoutside', function(ev) {
if ((ev.target.ancestor() !== this) && (ev.target !== this)) {
if (overlay.get('visible')) {
menu.detach('clickoutside');
overlay.hide();
}
}
}, this);
overlay.align(Y.one(Y.config.doc.body), [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL]);
overlay.show();
var icon = e.target.ancestor('button', true).one('img');
overlay.align(icon, [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL]);
overlay.get('boundingBox').one('a').focus();
}
},
/**
* Handle clicks on editor buttons.
* @param event e
*/
buttonclicked_handler : function(e) {
var elementid = this.getAttribute('data-editor');
var plugin = this.getAttribute('data-plugin');
var button = this.getAttribute('data-button');
var handler = this.getAttribute('data-handler');
var overlay = M.editor_atto.menus[plugin + '_' + elementid];
var toolbar = M.editor_atto.get_toolbar_node(elementid);
var currentid = toolbar.getAttribute('aria-activedescendant');
// Right now, currentid is the id of the previously selected button.
if (currentid) {
current = Y.one('#' + currentid);
// We only ever want one button with a tabindex of 0 at any one time.
current.setAttribute('tabindex', '-1');
}
this.setAttribute('tabindex', 0);
// And update the activedescendant to point at the currently selected button.
toolbar.setAttribute('aria-activedescendant', this.generateID());
if (overlay) {
overlay.hide();
}
if (M.editor_atto.is_enabled(elementid, plugin, button)) {
// Pass it on.
handler = M.editor_atto.buttonhandlers[handler];
return handler(e, elementid);
}
},
/**
* Disable all buttons and menus in the toolbar.
* @param string elementid, the element id of this editor.
*/
disable_all_widgets : function(elementid) {
var plugin, element, toolbar = M.editor_atto.get_toolbar_node(elementid);
for (plugin in M.editor_atto.widgets) {
element = toolbar.one('.atto_' + plugin + '_button');
if (element) {
element.setAttribute('disabled', 'true');
}
}
},
/**
* Get the node of the original textarea element that this editor replaced.
*
* @param string elementid, the element id of this editor.
* @return Y.Node
*/
get_textarea_node : function(elementid) {
// Note - it is not safe to use a CSS selector like '#' + elementid
// because the id may have colons in it - e.g. quiz.
return Y.one(document.getElementById(elementid));
},
/**
* Get the node of the toolbar container for this editor.
*
* @param string elementid, the element id of this editor.
* @return Y.Node
*/
get_toolbar_node : function(elementid) {
// Note - it is not safe to use a CSS selector like '#' + elementid
// because the id may have colons in it - e.g. quiz.
return Y.one(document.getElementById(elementid + '_toolbar'));
},
/**
* Get the node of the contenteditable container for this editor.
*
* @param string elementid, the element id of this editor.
* @return Y.Node
*/
get_editable_node : function(elementid) {
// Note - it is not safe to use a CSS selector like '#' + elementid
// because the id may have colons in it - e.g. quiz.
return Y.one(document.getElementById(elementid + 'editable'));
},
/**
* Determine if the specified toolbar button/menu is enabled.
* @param string elementid, the element id of this editor.
* @param string plugin, the plugin that created the button/menu.
* @param string buttonname, optional - used when a plugin has multiple buttons.
*/
is_enabled : function(elementid, plugin, button) {
var buttonpath = plugin;
if (button) {
buttonpath += '_' + button;
}
var element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + buttonpath + '_button');
return !element.hasAttribute('disabled');
},
/**
* Determine if the specified toolbar button/menu is highlighted.
* @param string elementid, the element id of this editor.
* @param string plugin, the plugin that created the button/menu.
* @param string buttonname, optional - used when a plugin has multiple buttons.
*/
is_highlighted : function(elementid, plugin, button) {
var buttonpath = plugin;
if (button) {
buttonpath += '_' + button;
}
var element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + buttonpath + '_button');
return !element.hasClass(CSS.HIGHLIGHT);
},
/**
* Enable a single widget in the toolbar.
* @param string elementid, the element id of this editor.
* @param string plugin, the name of the plugin that created the widget.
* @param string buttonname, optional - used when a plugin has multiple buttons.
*/
enable_widget : function(elementid, plugin, button) {
var buttonpath = plugin;
if (button) {
buttonpath += '_' + button;
}
var element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + buttonpath + '_button');
if (element) {
element.removeAttribute('disabled');
}
},
/**
* Highlight a single widget in the toolbar.
* @param string elementid, the element id of this editor.
* @param string plugin, the name of the plugin that created the widget.
* @param string buttonname, optional - used when a plugin has multiple buttons.
*/
add_widget_highlight : function(elementid, plugin, button) {
var buttonpath = plugin;
if (button) {
buttonpath += '_' + button;
}
var element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + buttonpath + '_button');
if (element) {
element.addClass(CSS.HIGHLIGHT);
}
},
/**
* Unhighlight a single widget in the toolbar.
* @param string elementid, the element id of this editor.
* @param string plugin, the name of the plugin that created the widget.
* @param string buttonname, optional - used when a plugin has multiple buttons.
*/
remove_widget_highlight : function(elementid, plugin, button) {
var buttonpath = plugin;
if (button) {
buttonpath += '_' + button;
}
var element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + buttonpath + '_button');
if (element) {
element.removeClass(CSS.HIGHLIGHT);
}
},
/**
* Enable all buttons and menus in the toolbar.
* @param string elementid, the element id of this editor.
*/
enable_all_widgets : function(elementid) {
var path, element;
for (path in M.editor_atto.widgets) {
element = M.editor_atto.get_toolbar_node(elementid).one('.atto_' + path + '_button');
if (element) {
element.removeAttribute('disabled');
}
}
},
/**
* Add a content update handler to be called whenever the content is updated.
*
* @param string elementid - the id of the textarea we created this editor from.
* @handler function callback - The function to do the cleaning.
* @param object context - the context to set for the callback.
* @handler function handler - A function to call when the button is clicked.
*/
add_text_updated_handler : function(elementid, callback) {
if (!(elementid in M.editor_atto.textupdatedhandlers)) {
M.editor_atto.textupdatedhandlers[elementid] = [];
}
M.editor_atto.textupdatedhandlers[elementid].push(callback);
},
/**
* Add a button to the toolbar belonging to the editor for element with id "elementid".
* @param string elementid - the id of the textarea we created this editor from.
* @param string plugin - the plugin defining the button
* @param string icon - the html used for the content of the button
* @param string groupname - the group the button should be appended to.
* @param array entries - List of menu entries with the string (entry.text) and the handlers (entry.handler).
* @param string buttonname - (optional) a name for the button. Required if a plugin creates more than one button.
* @param string buttontitle - (optional) a title for the button. Required if a plugin creates more than one button.
* @param int overlaywidth - the overlay width size in 'ems'.
* @param string menucolor - menu icon background color
*/
add_toolbar_menu : function(elementid, plugin, iconurl, groupname, entries, buttonname, buttontitle, overlaywidth, menucolor) {
var toolbar = M.editor_atto.get_toolbar_node(elementid),
group = toolbar.one('.atto_group.' + groupname + '_group'),
currentfocus,
button,
buttonpath,
expimgurl;
if (buttonname) {
buttonpath = plugin + '_' + buttonname;
} else {
buttonname = '';
buttonpath = plugin;
}
if (!buttontitle) {
buttontitle = M.util.get_string('pluginname', 'atto_' + plugin);
}
if ((typeof overlaywidth) === 'undefined') {
overlaywidth = '14';
}
if ((typeof menucolor) === 'undefined') {
menucolor = 'transparent';
}
if (!group) {
group = Y.Node.create('<div class="atto_group ' + groupname + '_group"></div>');
toolbar.append(group);
}
expimgurl = M.util.image_url('t/expanded', 'moodle');
button = Y.Node.create('<button class="atto_' + buttonpath + '_button atto_hasmenu" ' +
'data-editor="' + Y.Escape.html(elementid) + '" ' +
'tabindex="-1" ' +
'type="button" ' +
'data-menu="' + buttonpath + '_' + elementid + '" ' +
'title="' + Y.Escape.html(buttontitle) + '">' +
'<img class="icon" aria-hidden="true" role="presentation" width="16" height="16" '+
'style="background-color:' + menucolor + ';" src="' + iconurl + '"/>' +
'<img class="icon" aria-hidden="true" role="presentation" width="16" height="16" src="' + expimgurl + '"/>' +
'</button>');
group.append(button);
currentfocus = toolbar.getAttribute('aria-activedescendant');
if (!currentfocus) {
// Initially set the first button in the toolbar to be the default on keyboard focus.
button.setAttribute('tabindex', '0');
toolbar.setAttribute('aria-activedescendant', button.generateID());
}
// Save the name of the plugin.
M.editor_atto.widgets[buttonpath] = buttonpath;
var menu = Y.Node.create('<div class="atto_' + buttonpath + '_menu' +
' atto_menu" data-editor="' + Y.Escape.html(elementid) + '"' +
' style="min-width:' + (overlaywidth-2) + 'em"' +
'"></div>');
var i = 0, entry = {};
for (i = 0; i < entries.length; i++) {
entry = entries[i];
menu.append(Y.Node.create('<div class="atto_menuentry">' +
'<a href="#" class="atto_' + buttonpath + '_action_' + i + '" ' +
'data-editor="' + Y.Escape.html(elementid) + '" ' +
'data-plugin="' + Y.Escape.html(plugin) + '" ' +
'data-button="' + Y.Escape.html(buttonname) + '" ' +
'data-handler="' + Y.Escape.html(buttonpath + '_action_' + i) + '">' +
entry.text +
'</a>' +
'</div>'));
if (!M.editor_atto.buttonhandlers[plugin + '_action_' + i]) {
Y.one('body').delegate('click', M.editor_atto.buttonclicked_handler, '.atto_' + buttonpath + '_action_' + i);
// Activate the link on space or enter.
Y.one('body').delegate('key', M.editor_atto.buttonclicked_handler, '32,enter', '.atto_' + buttonpath + '_action_' + i);
M.editor_atto.buttonhandlers[buttonpath + '_action_' + i] = entry.handler;
}
}
if (!M.editor_atto.buttonhandlers[buttonpath]) {
Y.one('body').delegate('click', M.editor_atto.showhide_menu_handler, '.atto_' + buttonpath + '_button');
M.editor_atto.buttonhandlers[buttonpath] = true;
}
var overlay = new M.core.dialogue({
bodyContent : menu,
visible : false,
width: overlaywidth + 'em',
closeButton: false,
center : false,
render: true
});
M.editor_atto.menus[buttonpath + '_' + elementid] = overlay;
overlay.align(button, [Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BL]);
overlay.headerNode.hide();
},
/**
* Add a button to the toolbar belonging to the editor for element with id "elementid".
* @param string elementid - the id of the textarea we created this editor from.
* @param string plugin - the plugin defining the button.
* @param string icon - the url to the image for the icon
* @param string groupname - the group the button should be appended to.
* @handler function handler- A function to call when the button is clicked.
* @param string buttonname - (optional) a name for the button. Required if a plugin creates more than one button.
* @param string buttontitle - (optional) a title for the button. Required if a plugin creates more than one button.
*/
add_toolbar_button : function(elementid, plugin, iconurl, groupname, handler, buttonname, buttontitle) {
var toolbar = M.editor_atto.get_toolbar_node(elementid),
group = toolbar.one('.atto_group.' + groupname + '_group'),
button,
buttonpath,
currentfocus;
if (buttonname) {
buttonpath = plugin + '_' + buttonname;
} else {
buttonname = '';
buttonpath = plugin;
}
if (!buttontitle) {
buttontitle = M.util.get_string('pluginname', 'atto_' + plugin);
}
if (!group) {
group = Y.Node.create('<div class="atto_group ' + groupname +'_group"></div>');
toolbar.append(group);
}
button = Y.Node.create('<button class="atto_' + buttonpath + '_button" ' +
'data-editor="' + Y.Escape.html(elementid) + '" ' +
'data-plugin="' + Y.Escape.html(plugin) + '" ' +
'data-button="' + Y.Escape.html(buttonname) + '" ' +
'tabindex="-1" ' +
'data-handler="' + Y.Escape.html(buttonpath) + '" ' +
'title="' + Y.Escape.html(buttontitle) + '">' +
'<img class="icon" aria-hidden="true" role="presentation" width="16" height="16" src="' + iconurl + '"/>' +
'</button>');
group.append(button);
currentfocus = toolbar.getAttribute('aria-activedescendant');
if (!currentfocus) {
// Initially set the first button in the toolbar to be the default on keyboard focus.
button.setAttribute('tabindex', '0');
toolbar.setAttribute('aria-activedescendant', button.generateID());
}
// We only need to attach this once.
if (!M.editor_atto.buttonhandlers[buttonpath]) {
Y.one('body').delegate('click', M.editor_atto.buttonclicked_handler, '.atto_' + buttonpath + '_button');
M.editor_atto.buttonhandlers[buttonpath] = handler;
}
// Save the name of the plugin.
M.editor_atto.widgets[buttonpath] = buttonpath;
},
/**
* Work out if the cursor is in the editable area for this editor instance.
* @param string elementid of this editor
* @return bool
*/
is_active : function(elementid) {
var selection = M.editor_atto.get_selection();
if (selection.length && selection.pop) {
selection = selection.pop();
}
var node = null;
if (selection.parentElement) {
node = Y.one(selection.parentElement());
} else {
node = Y.one(selection.startContainer);
}
var editable = M.editor_atto.get_editable_node(elementid);
return node && editable.contains(node);
},
/**
* Focus on the editable area for this editor.
* @param string elementid of this editor
*/
focus : function(elementid) {
M.editor_atto.get_editable_node(elementid).focus();
},
/**
* Initialise the editor
* @param object params for this editor instance.
*/
init : function(params) {
var wrapper = Y.Node.create('<div class="' + CSS.WRAPPER + '" />');
var atto = Y.Node.create('<div id="' + params.elementid + 'editable" ' +
'contenteditable="true" ' +
'role="textbox" ' +
'spellcheck="true" ' +
'aria-live="off" ' +
'class="' + CSS.CONTENT + '" '+
'data-editor="' + params.elementid + '" />');
var toolbar = Y.Node.create('<div class="' + CSS.TOOLBAR + '" id="' + params.elementid + '_toolbar" role="toolbar" aria-live="off"/>');
// Editable content wrapper.
var content = Y.Node.create('<div class="' + CSS.CONTENTWRAPPER + '" />');
var textarea = M.editor_atto.get_textarea_node(params.elementid);
var label = Y.one('[for="' + params.elementid + '"]');
// Add a labelled-by attribute to the contenteditable.
if (label) {
label.generateID();
atto.setAttribute('aria-labelledby', label.get("id"));
toolbar.setAttribute('aria-labelledby', label.get("id"));
}
content.appendChild(atto);
// Add everything to the wrapper.
wrapper.appendChild(toolbar);
wrapper.appendChild(content);
// Style the editor.
atto.setStyle('minHeight', (1.2 * (textarea.getAttribute('rows'))) + 'em');
// Copy text to editable div.
atto.append(textarea.get('value'));
// Clean it.
atto.cleanHTML();
// Add the toolbar and editable zone to the page.
textarea.get('parentNode').insert(wrapper, textarea);
// Disable odd inline CSS styles.
M.editor_atto.disable_css_styling();
// Hide the old textarea.
textarea.hide();
this.publish_events();
atto.on('atto:selectionchanged', this.save_selection, this, params.elementid);
atto.on('focus', this.restore_selection, this, params.elementid);
// Do not restore selection when focus is from a click event.
atto.on('mousedown', function() { this.focusfromclick = true; }, this);
// Copy the current value back to the textarea when focus leaves us and save the current selection.
atto.on('blur', function() {
this.focusfromclick = false;
this.text_updated(params.elementid);
}, this);
// Listen for Arrow left and Arrow right keys.
Y.one(Y.config.doc.body).delegate('key',
this.keyboard_navigation,
'down:37,39',
'#' + params.elementid + '_toolbar',
this,
params.elementid);
// Save the file picker options for later.
M.editor_atto.filepickeroptions[params.elementid] = params.filepickeroptions;
// Init each of the plugins
var i, j, group, plugin;
for (i = 0; i < params.plugins.length; i++) {
group = params.plugins[i].group;
for (j = 0; j < params.plugins[i].plugins.length; j++) {
plugin = params.plugins[i].plugins[j];
plugin.params.elementid = params.elementid;
plugin.params.group = group;
M['atto_' + plugin.name].init(plugin.params);
}
}
// Let the plugins run some init code once all plugins are in the page.
for (i = 0; i < params.plugins.length; i++) {
group = params.plugins[i].group;
for (j = 0; j < params.plugins[i].plugins.length; j++) {
plugin = params.plugins[i].plugins[j];
plugin.params.elementid = params.elementid;
plugin.params.group = group;
if (typeof M['atto_' + plugin.name].after_init !== 'undefined') {
M['atto_' + plugin.name].after_init(plugin.params);
}
}
}
},
/**
* Add code to trigger the a set of custom events on either the toolbar, or the contenteditable node
* that can be listened to by plugins (or even this class).
* @param string elementid - the id of the textarea we created this editor from.
*/
publish_events : function() {
Y.Event.define("atto:selectionchanged", {
/**
* Catch the keydown/mouseup events, and fire a synthetic event for the change event.
* @param {Y.Event} e - The real event that triggers this synthetic one.
* @param {Object} params - Object containing the subscription object and the notifier.
*/
changeHandler: function (e, params) {
// Add 3 properties to the event.
// 1. add the elementid.
var elementid = params.sub.node.getAttribute('id');
// Strip 'editable' from the end of the id.
elementid = elementid.substring(0, elementid.length - 8);
e.elementid = elementid;
// 2. The list of leaf nodes contained in the selection.
e.selectedNodes = M.editor_atto.get_selected_nodes(elementid);
// 3. The current selection (Range)
e.selection = M.editor_atto.get_selection();
params.notifier.fire(e);
},
/**
* Subscribe to the atto:selectionchanged event.
* @param {Y.Node} node - The node for the subscription - must be the contenteditable node.
* @param {Y.Subscription} sub - YUI Subscription object.
* @param {Y.Event.Notifier} notifier - YUI notifier object.
*/
on: function (node, sub, notifier) {
var params = { notifier: notifier, sub: sub };
sub.attoKeyDownDetach = node.on('keydown', Y.throttle(this.changeHandler, 100), this, params);
sub.attoMouseUpDetach = node.on('mouseup', Y.throttle(this.changeHandler, 100), this, params);
sub.attoFocusDetach = node.on('focus', Y.throttle(this.changeHandler, 100), this, params);
},
/**
* Detach the atto:selectionchanged event.
* @param {Y.Node} node - The node for the subscription - must be the contenteditable node.
* @param {Y.Subscription} sub - YUI Subscription object.
*/
detach: function (node, sub) {
sub.attoKeyDownDetach.detach();
sub.attoMouseUpDetach.detach();
sub.attoFocusDetach.detach();
},
/**
* Delegate the atto:selectionchanged event.
*
* @param {Y.Node} node - The node for the subscription - must be the contenteditable node.
* @param {Y.Subscription} sub - YUI Subscription object.
* @param {Y.Event.Notifier} notifier - YUI notifier object.
* @param {String} filter - CSS selector for the filter.
*/
delegate: function(node, sub, notifier, filter) {
var params = { notifier: notifier, sub: sub };
sub.attoKeyDownDetachDelegate = node.delegate('keydown', Y.throttle(this.changeHandler, 100), filter, this, params);
sub.attoMouseUpDetachDelegate = node.delegate('mouseup', Y.throttle(this.changeHandler, 100), filter, this, params);
sub.attoFocusDetachDelegate = node.delegate('focus', Y.throttle(this.changeHandler, 100), filter, this, params);
},
/**
* Detach a delegated atto:selectionchanged event.
*
* @param {Y.Node} node - The node for the subscription - must be the contenteditable node.
* @param {Y.Subscription} sub - YUI Subscription object.
*/
detachDelegate: function(node, sub) {
sub.attoKeyDownDetachDelegate.detach();
sub.attoMouseUpDetachDelegate.detach();
sub.attoFocusDetachDelegate.detach();
}
});
},
/**
* The text in the contenteditable region has been updated,
* clean and copy the buffer to the text area.
* @param string elementid - the id of the textarea we created this editor from.
*/
text_updated : function(elementid) {
var textarea = M.editor_atto.get_textarea_node(elementid),
cleancontent = this.get_clean_html(elementid);
textarea.set('value', cleancontent);
// Trigger the onchange callback on the textarea, essentially to notify moodle-core-formchangechecker.
textarea.simulate('change');
// Trigger handlers for this action.
var i = 0;
if (elementid in M.editor_atto.textupdatedhandlers) {
for (i = 0; i < M.editor_atto.textupdatedhandlers[elementid].length; i++) {
var callback = M.editor_atto.textupdatedhandlers[elementid][i];
callback(elementid);
}
}
},
/**
* Remove all YUI ids from the generated HTML.
* @param string elementid - the id of the textarea we created this editor from.
* @return string HTML stripped of YUI ids
*/
get_clean_html : function(elementid) {
var atto = M.editor_atto.get_editable_node(elementid).cloneNode(true);
Y.each(atto.all('[id]'), function(node) {
var id = node.get('id');
if (id.indexOf('yui') === 0) {
node.removeAttribute('id');
}
});
// Remove any and all nasties from source.
atto.cleanHTML();
return atto.getHTML();
},
/**
* Implement arrow key navigation for the buttons in the toolbar.
* @param Event e - the keyboard event.
* @param string elementid - the id of the textarea we created this editor from.
*/
keyboard_navigation : function(e, elementid) {
var buttons,
current,
currentid,
currentindex,
toolbar = M.editor_atto.get_toolbar_node(elementid);
e.preventDefault();
// This workaround is because we cannot do ".atto_group:not([hidden]) button" in ie8 (even with selector-css3).
// Create an empty NodeList.
buttons = toolbar.all('empty');
toolbar.all('.atto_group').each(function(group) {
if (!group.hasAttribute('hidden')) {
// Append the visible buttons to the buttons list.
buttons = buttons.concat(group.all('button'));
}
});
// The currentid is the id of the previously selected button.
currentid = toolbar.getAttribute('aria-activedescendant');
if (!currentid) {
return;
}
// We only ever want one button with a tabindex of 0.
current = Y.one('#' + currentid);
current.setAttribute('tabindex', '-1');
currentindex = buttons.indexOf(current);
if (e.keyCode === 37) {
// Left
currentindex--;
if (currentindex < 0) {
currentindex = buttons.size()-1;
}
} else {
// Right
currentindex++;
if (currentindex >= buttons.size()) {
currentindex = 0;
}
}
// Currentindex has been updated to point to the new button.
current = buttons.item(currentindex);
current.setAttribute('tabindex', '0');
current.focus();
toolbar.setAttribute('aria-activedescendant', current.generateID());
},
/**
* Should we show the filepicker for this filetype?
*
* @param string elementid for this editor instance.
* @param string type The media type for the file picker
* @return boolean
*/
can_show_filepicker : function(elementid, type) {
var options = M.editor_atto.filepickeroptions[elementid];
return ((typeof options[type]) !== "undefined");
},
/**
* Show the filepicker.
* @param string elementid for this editor instance.
* @param string type The media type for the file picker
* @param function callback
*/
show_filepicker : function(elementid, type, callback) {
Y.use('core_filepicker', function (Y) {
var options = M.editor_atto.filepickeroptions[elementid][type];
options.formcallback = callback;
M.core_filepicker.show(Y, options);
});
},
/**
* Create a cross browser selection object that represents a yui node.
* @param Node yui node for the selection
* @return range (browser dependent)
*/
get_selection_from_node: function(node) {
var range;
if (window.getSelection) {
range = document.createRange();
range.setStartBefore(node.getDOMNode());
range.setEndAfter(node.getDOMNode());
return [range];
} else if (document.selection) {
range = document.body.createTextRange();
range.moveToElementText(node.getDOMNode());
return range;
}
return false;
},
/**
* Save the current selection on blur, allows more reliable keyboard navigation.
* @param Y.Event event
* @param string elementid
*/
save_selection : function(event, elementid) {
if (this.is_active(elementid)) {
var sel = this.get_selection();
if (sel.length > 0) {
this.selections[elementid] = sel;
}
}
},
/**
* Restore any current selection when the editor gets focus again.
* @param Y.Event event
* @param string elementid
*/
restore_selection : function(event, elementid) {
event.preventDefault();
if (!this.focusfromclick) {
if (typeof this.selections[elementid] !== "undefined") {
this.set_selection(this.selections[elementid]);
}
}
this.focusfromclick = false;
},
/**
* Get the selection object that can be passed back to set_selection.
* @return range (browser dependent)
*/
get_selection : function() {
if (window.getSelection) {
var sel = window.getSelection();
var ranges = [], i = 0;
for (i = 0; i < sel.rangeCount; i++) {
ranges.push(sel.getRangeAt(i));
}
return ranges;
} else if (document.selection) {
// IE < 9
if (document.selection.createRange) {
return document.selection.createRange();
}
}
return false;
},
/**
* Check that a YUI node it at least partly contained by the selection.
* @param Y.Node node
* @return boolean
*/
selection_contains_node : function(node) {
var range, sel;
if (window.getSelection) {
sel = window.getSelection();
if (sel.containsNode) {
return sel.containsNode(node.getDOMNode(), true);
}
}
sel = document.selection.createRange();
range = sel.duplicate();
range.moveToElementText(node.getDOMNode());
return sel.inRange(range);
},
/**
* Runs a selector on each node in the selection and will only return true if all nodes
* in the selection match the filter.
*
* @param {String} elementid
* @param {String} selector
* @param {NodeList} selectednodes (Optional) - for performance we can pass this instead of looking it up.
* @return {Boolean}
*/
selection_filter_matches : function(elementid, selector, selectednodes) {
var result = true;
if (!selectednodes) {
// Find this because it was not passed as a param.
selectednodes = M.editor_atto.get_selected_nodes(elementid);
}
selector = '.' + CSS.CONTENT + ' ' + selector;
if (selectednodes.size() === 0) {
return false;
}
selectednodes.each(function(node) {
if (!node.ancestor(selector, true)) {
result = false;
}
});
return result;
},
/**
* Returns a list of nodes that are ancestors of the selection nodes,
* and match the specified css selector (and are contained within the editable div).
*
* @param {String} elementid
* @param {String} selector
* @return Y.NodeList
*/