2 * $Id: editor_plugin_src.js 42 2006-08-08 14:32:24Z spocke $
4 * @author Moxiecode - based on work by Andrew Tetlaw
5 * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
8 function initCommonAttributes(elm
) {
9 var formObj
= document
.forms
[0];
11 // Setup form data for common element attributes
12 setFormValue('title', tinyMCE
.getAttrib(elm
, 'title'));
13 setFormValue('id', tinyMCE
.getAttrib(elm
, 'id'));
14 selectByValue(formObj
, 'class', tinyMCE
.getAttrib(elm
, 'class'), true);
15 setFormValue('style', tinyMCE
.getAttrib(elm
, 'style'));
16 selectByValue(formObj
, 'dir', tinyMCE
.getAttrib(elm
, 'dir'));
17 setFormValue('lang', tinyMCE
.getAttrib(elm
, 'lang'));
18 setFormValue('onfocus', tinyMCE
.getAttrib(elm
, 'onfocus'));
19 setFormValue('onblur', tinyMCE
.getAttrib(elm
, 'onblur'));
20 setFormValue('onclick', tinyMCE
.getAttrib(elm
, 'onclick'));
21 setFormValue('ondblclick', tinyMCE
.getAttrib(elm
, 'ondblclick'));
22 setFormValue('onmousedown', tinyMCE
.getAttrib(elm
, 'onmousedown'));
23 setFormValue('onmouseup', tinyMCE
.getAttrib(elm
, 'onmouseup'));
24 setFormValue('onmouseover', tinyMCE
.getAttrib(elm
, 'onmouseover'));
25 setFormValue('onmousemove', tinyMCE
.getAttrib(elm
, 'onmousemove'));
26 setFormValue('onmouseout', tinyMCE
.getAttrib(elm
, 'onmouseout'));
27 setFormValue('onkeypress', tinyMCE
.getAttrib(elm
, 'onkeypress'));
28 setFormValue('onkeydown', tinyMCE
.getAttrib(elm
, 'onkeydown'));
29 setFormValue('onkeyup', tinyMCE
.getAttrib(elm
, 'onkeyup'));
32 function setFormValue(name
, value
) {
33 if(document
.forms
[0].elements
[name
]) document
.forms
[0].elements
[name
].value
= value
;
36 function insertDateTime(id
) {
37 document
.getElementById(id
).value
= getDateTime(new Date(), "%Y-%m-%dT%H:%M:%S");
40 function getDateTime(d
, fmt
) {
41 fmt
= fmt
.replace("%D", "%m/%d/%y");
42 fmt
= fmt
.replace("%r", "%I:%M:%S %p");
43 fmt
= fmt
.replace("%Y", "" + d
.getFullYear());
44 fmt
= fmt
.replace("%y", "" + d
.getYear());
45 fmt
= fmt
.replace("%m", addZeros(d
.getMonth()+1, 2));
46 fmt
= fmt
.replace("%d", addZeros(d
.getDate(), 2));
47 fmt
= fmt
.replace("%H", "" + addZeros(d
.getHours(), 2));
48 fmt
= fmt
.replace("%M", "" + addZeros(d
.getMinutes(), 2));
49 fmt
= fmt
.replace("%S", "" + addZeros(d
.getSeconds(), 2));
50 fmt
= fmt
.replace("%I", "" + ((d
.getHours() + 11) % 12 + 1));
51 fmt
= fmt
.replace("%p", "" + (d
.getHours() < 12 ? "AM" : "PM"));
52 fmt
= fmt
.replace("%%", "%");
57 function addZeros(value
, len
) {
62 if (value
.length
< len
) {
63 for (i
=0; i
<(len
-value
.length
); i
++)
70 function selectByValue(form_obj
, field_name
, value
, add_custom
, ignore_case
) {
71 if (!form_obj
|| !form_obj
.elements
[field_name
])
74 var sel
= form_obj
.elements
[field_name
];
77 for (var i
=0; i
<sel
.options
.length
; i
++) {
78 var option
= sel
.options
[i
];
80 if (option
.value
== value
|| (ignore_case
&& option
.value
.toLowerCase() == value
.toLowerCase())) {
81 option
.selected
= true;
84 option
.selected
= false;
87 if (!found
&& add_custom
&& value
!= '') {
88 var option
= new Option('Value: ' + value
, value
);
89 option
.selected
= true;
90 sel
.options
[sel
.options
.length
] = option
;
96 function setAttrib(elm
, attrib
, value
) {
97 var formObj
= document
.forms
[0];
98 var valueElm
= formObj
.elements
[attrib
.toLowerCase()];
100 if (typeof(value
) == "undefined" || value
== null) {
104 value
= valueElm
.value
;
108 if (attrib
== "style")
109 attrib
= "style.cssText";
111 if (attrib
.substring(0, 2) == 'on')
112 value
= 'return true;' + value
;
114 if (attrib
== "class") {
115 tinyMCE
.addCSSClass(elm
, value
);
119 elm
.setAttribute(attrib
.toLowerCase(), value
);
121 elm
.removeAttribute(attrib
);
124 function setAllCommonAttribs(elm
) {
125 setAttrib(elm
, 'title');
126 setAttrib(elm
, 'id');
127 setAttrib(elm
, 'class');
128 setAttrib(elm
, 'style');
129 setAttrib(elm
, 'dir');
130 setAttrib(elm
, 'lang');
131 /*setAttrib(elm, 'onfocus');
132 setAttrib(elm, 'onblur');
133 setAttrib(elm, 'onclick');
134 setAttrib(elm, 'ondblclick');
135 setAttrib(elm, 'onmousedown');
136 setAttrib(elm, 'onmouseup');
137 setAttrib(elm, 'onmouseover');
138 setAttrib(elm, 'onmousemove');
139 setAttrib(elm, 'onmouseout');
140 setAttrib(elm, 'onkeypress');
141 setAttrib(elm, 'onkeydown');
142 setAttrib(elm, 'onkeyup');*/
146 currentAction
: "insert",
147 inst
: tinyMCE
.getInstanceById(tinyMCE
.getWindowArg('editor_id')),
151 SXE
.focusElement
= SXE
.inst
.getFocusElement();
153 SXE
.initElementDialog = function(element_name
) {
154 addClassesToList('class', 'xhtmlxtras_styles');
155 TinyMCE_EditableSelects
.init();
157 element_name
= element_name
.toLowerCase();
158 var elm
= tinyMCE
.getParentElement(SXE
.focusElement
, element_name
);
159 if (elm
!= null && elm
.nodeName
== element_name
.toUpperCase()) {
160 SXE
.currentAction
= "update";
163 if (SXE
.currentAction
== "update") {
164 initCommonAttributes(elm
);
165 SXE
.updateElement
= elm
;
168 document
.forms
[0].insert
.value
= tinyMCE
.getLang('lang_' + SXE
.currentAction
, 'Insert', true);
171 SXE
.insertElement = function(element_name
) {
172 var elm
= tinyMCE
.getParentElement(SXE
.focusElement
, element_name
), h
, tagName
;
174 tinyMCEPopup
.execCommand('mceBeginUndoLevel');
176 var s
= SXE
.inst
.selection
.getSelectedHTML();
178 tagName
= element_name
;
180 if (tinyMCE
.isIE
&& !tinyMCE
.isOpera
&& element_name
.indexOf('html:') == 0)
181 element_name
= element_name
.substring(5).toLowerCase();
183 h
= '<' + tagName
+ ' id="#sxe_temp_' + element_name
+ '#">' + s
+ '</' + tagName
+ '>';
185 tinyMCEPopup
.execCommand('mceInsertContent', false, h
);
187 var elementArray
= tinyMCE
.getElementsByAttributeValue(SXE
.inst
.getBody(), element_name
, 'id', '#sxe_temp_' + element_name
+ '#');
188 for (var i
=0; i
<elementArray
.length
; i
++) {
189 var elm
= elementArray
[i
];
192 elm
.setAttribute('id', '');
193 elm
.removeAttribute('id');
195 setAllCommonAttribs(elm
);
199 setAllCommonAttribs(elm
);
201 tinyMCE
.triggerNodeChange();
202 tinyMCEPopup
.execCommand('mceEndUndoLevel');
205 SXE
.removeElement = function(element_name
){
206 element_name
= element_name
.toLowerCase();
207 elm
= tinyMCE
.getParentElement(SXE
.focusElement
, element_name
);
208 if(elm
&& elm
.nodeName
== element_name
.toUpperCase()){
209 tinyMCEPopup
.execCommand('mceBeginUndoLevel');
210 tinyMCE
.execCommand('mceRemoveNode', false, elm
);
211 tinyMCE
.triggerNodeChange();
212 tinyMCEPopup
.execCommand('mceEndUndoLevel');
216 SXE
.showRemoveButton = function() {
217 document
.getElementById("remove").style
.display
= 'block';
220 SXE
.containsClass = function(elm
,cl
) {
221 return (elm
.className
.indexOf(cl
) > -1) ? true : false;
224 SXE
.removeClass = function(elm
,cl
) {
225 if(elm
.className
== null || elm
.className
== "" || !SXE
.containsClass(elm
,cl
)) {
228 var classNames
= elm
.className
.split(" ");
229 var newClassNames
= "";
230 for (var x
= 0, cnl
= classNames
.length
; x
< cnl
; x
++) {
231 if (classNames
[x
] != cl
) {
232 newClassNames
+= (classNames
[x
] + " ");
235 elm
.className
= newClassNames
.substring(0,newClassNames
.length
-1); //removes extra space at the end
238 SXE
.addClass = function(elm
,cl
) {
239 if(!SXE
.containsClass(elm
,cl
)) elm
.className
? elm
.className
+= " " + cl
: elm
.className
= cl
;