Line # Revision Author
1 3 ahitrov@rambler.ru /**
2 * element_common.js
3 *
4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
6 *
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
9 */
10
11 tinyMCEPopup.requireLangPack();
12
13 function initCommonAttributes(elm) {
14 var formObj = document.forms[0], dom = tinyMCEPopup.editor.dom;
15
16 // Setup form data for common element attributes
17 setFormValue('title', dom.getAttrib(elm, 'title'));
18 setFormValue('id', dom.getAttrib(elm, 'id'));
19 selectByValue(formObj, 'class', dom.getAttrib(elm, 'class'), true);
20 setFormValue('style', dom.getAttrib(elm, 'style'));
21 selectByValue(formObj, 'dir', dom.getAttrib(elm, 'dir'));
22 setFormValue('lang', dom.getAttrib(elm, 'lang'));
23 setFormValue('onfocus', dom.getAttrib(elm, 'onfocus'));
24 setFormValue('onblur', dom.getAttrib(elm, 'onblur'));
25 setFormValue('onclick', dom.getAttrib(elm, 'onclick'));
26 setFormValue('ondblclick', dom.getAttrib(elm, 'ondblclick'));
27 setFormValue('onmousedown', dom.getAttrib(elm, 'onmousedown'));
28 setFormValue('onmouseup', dom.getAttrib(elm, 'onmouseup'));
29 setFormValue('onmouseover', dom.getAttrib(elm, 'onmouseover'));
30 setFormValue('onmousemove', dom.getAttrib(elm, 'onmousemove'));
31 setFormValue('onmouseout', dom.getAttrib(elm, 'onmouseout'));
32 setFormValue('onkeypress', dom.getAttrib(elm, 'onkeypress'));
33 setFormValue('onkeydown', dom.getAttrib(elm, 'onkeydown'));
34 setFormValue('onkeyup', dom.getAttrib(elm, 'onkeyup'));
35 }
36
37 function setFormValue(name, value) {
38 if(document.forms[0].elements[name]) document.forms[0].elements[name].value = value;
39 }
40
41 function insertDateTime(id) {
42 document.getElementById(id).value = getDateTime(new Date(), "%Y-%m-%dT%H:%M:%S");
43 }
44
45 function getDateTime(d, fmt) {
46 fmt = fmt.replace("%D", "%m/%d/%y");
47 fmt = fmt.replace("%r", "%I:%M:%S %p");
48 fmt = fmt.replace("%Y", "" + d.getFullYear());
49 fmt = fmt.replace("%y", "" + d.getYear());
50 fmt = fmt.replace("%m", addZeros(d.getMonth()+1, 2));
51 fmt = fmt.replace("%d", addZeros(d.getDate(), 2));
52 fmt = fmt.replace("%H", "" + addZeros(d.getHours(), 2));
53 fmt = fmt.replace("%M", "" + addZeros(d.getMinutes(), 2));
54 fmt = fmt.replace("%S", "" + addZeros(d.getSeconds(), 2));
55 fmt = fmt.replace("%I", "" + ((d.getHours() + 11) % 12 + 1));
56 fmt = fmt.replace("%p", "" + (d.getHours() < 12 ? "AM" : "PM"));
57 fmt = fmt.replace("%%", "%");
58
59 return fmt;
60 }
61
62 function addZeros(value, len) {
63 var i;
64
65 value = "" + value;
66
67 if (value.length < len) {
68 for (i=0; i<(len-value.length); i++)
69 value = "0" + value;
70 }
71
72 return value;
73 }
74
75 function selectByValue(form_obj, field_name, value, add_custom, ignore_case) {
76 if (!form_obj || !form_obj.elements[field_name])
77 return;
78
79 var sel = form_obj.elements[field_name];
80
81 var found = false;
82 for (var i=0; i<sel.options.length; i++) {
83 var option = sel.options[i];
84
85 if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
86 option.selected = true;
87 found = true;
88 } else
89 option.selected = false;
90 }
91
92 if (!found && add_custom && value != '') {
93 var option = new Option('Value: ' + value, value);
94 option.selected = true;
95 sel.options[sel.options.length] = option;
96 }
97
98 return found;
99 }
100
101 function setAttrib(elm, attrib, value) {
102 var formObj = document.forms[0];
103 var valueElm = formObj.elements[attrib.toLowerCase()];
104 tinyMCEPopup.editor.dom.setAttrib(elm, attrib, value || valueElm.value);
105 }
106
107 function setAllCommonAttribs(elm) {
108 setAttrib(elm, 'title');
109 setAttrib(elm, 'id');
110 setAttrib(elm, 'class');
111 setAttrib(elm, 'style');
112 setAttrib(elm, 'dir');
113 setAttrib(elm, 'lang');
114 /*setAttrib(elm, 'onfocus');
115 setAttrib(elm, 'onblur');
116 setAttrib(elm, 'onclick');
117 setAttrib(elm, 'ondblclick');
118 setAttrib(elm, 'onmousedown');
119 setAttrib(elm, 'onmouseup');
120 setAttrib(elm, 'onmouseover');
121 setAttrib(elm, 'onmousemove');
122 setAttrib(elm, 'onmouseout');
123 setAttrib(elm, 'onkeypress');
124 setAttrib(elm, 'onkeydown');
125 setAttrib(elm, 'onkeyup');*/
126 }
127
128 SXE = {
129 currentAction : "insert",
130 inst : tinyMCEPopup.editor,
131 updateElement : null
132 }
133
134 SXE.focusElement = SXE.inst.selection.getNode();
135
136 SXE.initElementDialog = function(element_name) {
137 addClassesToList('class', 'xhtmlxtras_styles');
138 TinyMCE_EditableSelects.init();
139
140 element_name = element_name.toLowerCase();
141 var elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase());
142 if (elm != null && elm.nodeName.toUpperCase() == element_name.toUpperCase()) {
143 SXE.currentAction = "update";
144 }
145
146 if (SXE.currentAction == "update") {
147 initCommonAttributes(elm);
148 SXE.updateElement = elm;
149 }
150
151 document.forms[0].insert.value = tinyMCEPopup.getLang(SXE.currentAction, 'Insert', true);
152 }
153
154 SXE.insertElement = function(element_name) {
155 var elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase()), h, tagName;
156
157 tinyMCEPopup.execCommand('mceBeginUndoLevel');
158 if (elm == null) {
159 var s = SXE.inst.selection.getContent();
160 if(s.length > 0) {
161 tagName = element_name;
162
163 insertInlineElement(element_name);
164 var elementArray = tinymce.grep(SXE.inst.dom.select(element_name));
165 for (var i=0; i<elementArray.length; i++) {
166 var elm = elementArray[i];
167
168 if (SXE.inst.dom.getAttrib(elm, '_mce_new')) {
169 elm.id = '';
170 elm.setAttribute('id', '');
171 elm.removeAttribute('id');
172 elm.removeAttribute('_mce_new');
173
174 setAllCommonAttribs(elm);
175 }
176 }
177 }
178 } else {
179 setAllCommonAttribs(elm);
180 }
181 SXE.inst.nodeChanged();
182 tinyMCEPopup.execCommand('mceEndUndoLevel');
183 }
184
185 SXE.removeElement = function(element_name){
186 element_name = element_name.toLowerCase();
187 elm = SXE.inst.dom.getParent(SXE.focusElement, element_name.toUpperCase());
188 if(elm && elm.nodeName.toUpperCase() == element_name.toUpperCase()){
189 tinyMCEPopup.execCommand('mceBeginUndoLevel');
190 tinyMCE.execCommand('mceRemoveNode', false, elm);
191 SXE.inst.nodeChanged();
192 tinyMCEPopup.execCommand('mceEndUndoLevel');
193 }
194 }
195
196 SXE.showRemoveButton = function() {
197 document.getElementById("remove").style.display = '';
198 }
199
200 SXE.containsClass = function(elm,cl) {
201 return (elm.className.indexOf(cl) > -1) ? true : false;
202 }
203
204 SXE.removeClass = function(elm,cl) {
205 if(elm.className == null || elm.className == "" || !SXE.containsClass(elm,cl)) {
206 return true;
207 }
208 var classNames = elm.className.split(" ");
209 var newClassNames = "";
210 for (var x = 0, cnl = classNames.length; x < cnl; x++) {
211 if (classNames[x] != cl) {
212 newClassNames += (classNames[x] + " ");
213 }
214 }
215 elm.className = newClassNames.substring(0,newClassNames.length-1); //removes extra space at the end
216 }
217
218 SXE.addClass = function(elm,cl) {
219 if(!SXE.containsClass(elm,cl)) elm.className ? elm.className += " " + cl : elm.className = cl;
220 return true;
221 }
222
223 function insertInlineElement(en) {
224 var ed = tinyMCEPopup.editor, dom = ed.dom;
225
226 ed.getDoc().execCommand('FontName', false, 'mceinline');
227 tinymce.each(dom.select('span,font'), function(n) {
228 if (n.style.fontFamily == 'mceinline' || n.face == 'mceinline')
229 dom.replace(dom.create(en, {_mce_new : 1}), n, 1);
230 });
231 }