Mercurial > defr > drupal > scald > dnd
comparison js/tinymce/forcecontainer/editor_plugin_src.js @ 31:767ebf925654
Added forcecontainer tinymce plugin, lots and lots and lots of refactorizing.
author | David Eads <eads@chicagotech.org> |
---|---|
date | Thu, 19 Mar 2009 15:58:36 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
30:2d49adbd8992 | 31:767ebf925654 |
---|---|
1 /** | |
2 * $Id$ | |
3 * | |
4 * A plugin to handle forcing a selection to an outer container with a given | |
5 * class. | |
6 * | |
7 * Options | |
8 * | |
9 * forcecontainer_class: | |
10 * | |
11 * Elements with this class will be forced to the outer container on certain | |
12 * events. | |
13 * | |
14 * forcecontainer_trigger_dnd: | |
15 * | |
16 * Custom option -- enables triggering of a custom event via jQuery for the | |
17 * Drag and Drop Library. | |
18 */ | |
19 | |
20 (function() { | |
21 var Event = tinymce.dom.Event; | |
22 | |
23 tinymce.create('tinymce.plugins.ForceContainerPlugin', { | |
24 getInfo : function() { | |
25 return { | |
26 longname : 'Force an element to its outer container', | |
27 author : 'David Eads', | |
28 authorurl : 'http://invisibleinstitute.com/eads', | |
29 infourl : '', | |
30 version : tinymce.majorVersion + "." + tinymce.minorVersion | |
31 }; | |
32 }, | |
33 | |
34 init : function(ed, url) { | |
35 var t = this, forceContainerClass; | |
36 | |
37 t.editor = ed; | |
38 forceContainerClass = ed.getParam("forcecontainer_class", "mceForceContainer"); | |
39 | |
40 ed.onNodeChange.addToTop(function(ed, cm, n) { | |
41 var sc, ec, c; | |
42 | |
43 // Block if start or end is inside a non editable element | |
44 sc = ed.dom.getParent(ed.selection.getStart(), function(n) { | |
45 return ed.dom.hasClass(n, forceContainerClass) && !ed.dom.hasClass(n, 'force-container-processed'); | |
46 }); | |
47 | |
48 ec = ed.dom.getParent(ed.selection.getEnd(), function(n) { | |
49 return ed.dom.hasClass(n, forceContainerClass) && !ed.dom.hasClass(n, 'force-container-processed'); | |
50 }); | |
51 | |
52 // Block or unblock | |
53 if (sc || ec) { | |
54 c = sc ? sc : ec; | |
55 ed.selection.select(c); | |
56 t._setDisabled(1); | |
57 return false; | |
58 } else | |
59 t._setDisabled(0); | |
60 }); | |
61 }, | |
62 | |
63 _block : function(ed, e) { | |
64 var k = e.keyCode, s = ed.selection, n = s.getNode(), reparent, forceContainerClass; | |
65 | |
66 // Reparent node | |
67 forceContainerClass = ed.getParam("forcecontainer_class", "mceForceContainer"); | |
68 | |
69 // Block if start or end is inside a non editable element | |
70 sc = ed.dom.getParent(ed.selection.getStart(), function(n) { | |
71 return ed.dom.hasClass(n, forceContainerClass) && !ed.dom.hasClass(n, 'force-container-processed'); | |
72 }); | |
73 | |
74 ec = ed.dom.getParent(ed.selection.getEnd(), function(n) { | |
75 return ed.dom.hasClass(n, forceContainerClass) && !ed.dom.hasClass(n, 'force-container-processed'); | |
76 }); | |
77 | |
78 if (sc || ec) { | |
79 n = (sc) ? sc : ec; | |
80 } | |
81 | |
82 // Pass F1-F12, alt, ctrl, shift, page up, page down, arrow keys | |
83 if ((k > 111 && k < 124) || k == 16 || k == 17 || k == 18 || k == 27 || (k > 32 && k < 41)) { | |
84 return; | |
85 } | |
86 | |
87 // Step out to parent and delete | |
88 if (k == 8 || k == 46) { | |
89 if (ed.getParam("forcecontainer_trigger_dnd", false)) { | |
90 // @TODO -- this is getting called twice!!! | |
91 $('#' + ed.id + '-wrapper iframe').trigger('dnd_delete', { 'node' : n }); | |
92 } | |
93 ed.execCommand('Delete', false, null); | |
94 } | |
95 | |
96 // Typing some common characters | |
97 if (k == 13 || (k > 47 && k < 91) || (k > 95 && k < 112) || (k > 185 && k < 223)) { | |
98 var c = ed.dom.get('__caret'), p; | |
99 if (!c) { | |
100 p = ed.dom.create('p', {}, ((k != 13) ? String.fromCharCode(k) : '') + '<span id="__caret">_</span>'); | |
101 ed.dom.insertAfter(p, n); | |
102 s.select(c); | |
103 } else { | |
104 s.select(c); | |
105 ed.execCommand('Delete', false, null); | |
106 } | |
107 } | |
108 | |
109 return Event.cancel(e); | |
110 }, | |
111 | |
112 | |
113 _setDisabled : function(s) { | |
114 var t = this, ed = t.editor, n = t.container; | |
115 | |
116 tinymce.each(ed.controlManager.controls, function(c) { | |
117 c.setDisabled(s); | |
118 }); | |
119 | |
120 if (s !== t.disabled) { | |
121 if (s) { | |
122 ed.onClick.addToTop(t._block); | |
123 ed.onMouseDown.addToTop(t._block); | |
124 ed.onKeyDown.addToTop(t._block); | |
125 ed.onKeyPress.addToTop(t._block); | |
126 ed.onKeyUp.addToTop(t._block); | |
127 ed.onPaste.addToTop(t._block); | |
128 } else { | |
129 ed.onClick.remove(t._block); // @TODO causing buggy behavior if you click twice | |
130 ed.onMouseDown.remove(t._block); | |
131 ed.onKeyDown.remove(t._block); | |
132 ed.onKeyPress.remove(t._block); | |
133 ed.onKeyUp.remove(t._block); | |
134 ed.onPaste.remove(t._block); | |
135 } | |
136 | |
137 t.disabled = s; | |
138 } | |
139 } | |
140 }); | |
141 | |
142 // Register plugin | |
143 tinymce.PluginManager.add('forcecontainer', tinymce.plugins.ForceContainerPlugin); | |
144 })(); |