1 /* Functions for the advlink plugin popup */
3 tinyMCEPopup
.requireLangPack();
6 "window.open" : "window.open('${url}','${target}','${options}')"
12 if (url
= tinyMCEPopup
.getParam("external_link_list_url"))
13 document
.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup
.editor
.documentBaseURI
.toAbsolute(url
) + '"></script>');
16 function changeClass() {
17 var f
= document
.forms
[0];
19 f
.classes
.value
= getSelectValue(f
, 'classlist');
23 tinyMCEPopup
.resizeToInnerSize();
25 var formObj
= document
.forms
[0];
26 var inst
= tinyMCEPopup
.editor
;
27 var elm
= inst
.selection
.getNode();
28 var action
= "insert";
31 document
.getElementById('hrefbrowsercontainer').innerHTML
= getBrowserHTML('hrefbrowser','href','file','advlink');
32 document
.getElementById('popupurlbrowsercontainer').innerHTML
= getBrowserHTML('popupurlbrowser','popupurl','file','advlink');
33 document
.getElementById('targetlistcontainer').innerHTML
= getTargetListHTML('targetlist','target');
36 html
= getLinkListHTML('linklisthref','href');
38 document
.getElementById("linklisthrefrow").style
.display
= 'none';
40 document
.getElementById("linklisthrefcontainer").innerHTML
= html
;
43 html
= getAnchorListHTML('anchorlist','href');
45 document
.getElementById("anchorlistrow").style
.display
= 'none';
47 document
.getElementById("anchorlistcontainer").innerHTML
= html
;
49 // Resize some elements
50 if (isVisible('hrefbrowser'))
51 document
.getElementById('href').style
.width
= '260px';
53 if (isVisible('popupurlbrowser'))
54 document
.getElementById('popupurl').style
.width
= '180px';
56 elm
= inst
.dom
.getParent(elm
, "A");
58 var prospect
= inst
.dom
.create("p", null, inst
.selection
.getContent());
59 if (prospect
.childNodes
.length
=== 1) {
60 elm
= prospect
.firstChild
;
64 if (elm
!= null && elm
.nodeName
== "A")
67 formObj
.insert
.value
= tinyMCEPopup
.getLang(action
, 'Insert', true);
69 setPopupControlsDisabled(true);
71 if (action
== "update") {
72 var href
= inst
.dom
.getAttrib(elm
, 'href');
73 var onclick
= inst
.dom
.getAttrib(elm
, 'onclick');
76 setFormValue('href', href
);
77 setFormValue('title', inst
.dom
.getAttrib(elm
, 'title'));
78 setFormValue('id', inst
.dom
.getAttrib(elm
, 'id'));
79 setFormValue('style', inst
.dom
.getAttrib(elm
, "style"));
80 setFormValue('rel', inst
.dom
.getAttrib(elm
, 'rel'));
81 setFormValue('rev', inst
.dom
.getAttrib(elm
, 'rev'));
82 setFormValue('charset', inst
.dom
.getAttrib(elm
, 'charset'));
83 setFormValue('hreflang', inst
.dom
.getAttrib(elm
, 'hreflang'));
84 setFormValue('dir', inst
.dom
.getAttrib(elm
, 'dir'));
85 setFormValue('lang', inst
.dom
.getAttrib(elm
, 'lang'));
86 setFormValue('tabindex', inst
.dom
.getAttrib(elm
, 'tabindex', typeof(elm
.tabindex
) != "undefined" ? elm
.tabindex
: ""));
87 setFormValue('accesskey', inst
.dom
.getAttrib(elm
, 'accesskey', typeof(elm
.accesskey
) != "undefined" ? elm
.accesskey
: ""));
88 setFormValue('type', inst
.dom
.getAttrib(elm
, 'type'));
89 setFormValue('onfocus', inst
.dom
.getAttrib(elm
, 'onfocus'));
90 setFormValue('onblur', inst
.dom
.getAttrib(elm
, 'onblur'));
91 setFormValue('onclick', onclick
);
92 setFormValue('ondblclick', inst
.dom
.getAttrib(elm
, 'ondblclick'));
93 setFormValue('onmousedown', inst
.dom
.getAttrib(elm
, 'onmousedown'));
94 setFormValue('onmouseup', inst
.dom
.getAttrib(elm
, 'onmouseup'));
95 setFormValue('onmouseover', inst
.dom
.getAttrib(elm
, 'onmouseover'));
96 setFormValue('onmousemove', inst
.dom
.getAttrib(elm
, 'onmousemove'));
97 setFormValue('onmouseout', inst
.dom
.getAttrib(elm
, 'onmouseout'));
98 setFormValue('onkeypress', inst
.dom
.getAttrib(elm
, 'onkeypress'));
99 setFormValue('onkeydown', inst
.dom
.getAttrib(elm
, 'onkeydown'));
100 setFormValue('onkeyup', inst
.dom
.getAttrib(elm
, 'onkeyup'));
101 setFormValue('target', inst
.dom
.getAttrib(elm
, 'target'));
102 setFormValue('classes', inst
.dom
.getAttrib(elm
, 'class'));
104 // Parse onclick data
105 if (onclick
!= null && onclick
.indexOf('window.open') != -1)
106 parseWindowOpen(onclick
);
108 parseFunction(onclick
);
110 // Select by the values
111 selectByValue(formObj
, 'dir', inst
.dom
.getAttrib(elm
, 'dir'));
112 selectByValue(formObj
, 'rel', inst
.dom
.getAttrib(elm
, 'rel'));
113 selectByValue(formObj
, 'rev', inst
.dom
.getAttrib(elm
, 'rev'));
114 selectByValue(formObj
, 'linklisthref', href
);
116 if (href
.charAt(0) == '#')
117 selectByValue(formObj
, 'anchorlist', href
);
119 addClassesToList('classlist', 'advlink_styles');
121 selectByValue(formObj
, 'classlist', inst
.dom
.getAttrib(elm
, 'class'), true);
122 selectByValue(formObj
, 'targetlist', inst
.dom
.getAttrib(elm
, 'target'), true);
124 addClassesToList('classlist', 'advlink_styles');
127 function checkPrefix(n
) {
128 if (n
.value
&& Validator
.isEmail(n
) && !/^\s*mailto:/i.test(n
.value
) && confirm(tinyMCEPopup
.getLang('advlink_dlg.is_email')))
129 n
.value
= 'mailto:' + n
.value
;
131 if (/^\s*www\./i.test(n
.value
) && confirm(tinyMCEPopup
.getLang('advlink_dlg.is_external')))
132 n
.value
= 'http://' + n
.value
;
135 function setFormValue(name
, value
) {
136 document
.forms
[0].elements
[name
].value
= value
;
139 function parseWindowOpen(onclick
) {
140 var formObj
= document
.forms
[0];
142 // Preprocess center code
143 if (onclick
.indexOf('return false;') != -1) {
144 formObj
.popupreturn
.checked
= true;
145 onclick
= onclick
.replace('return false;', '');
147 formObj
.popupreturn
.checked
= false;
149 var onClickData
= parseLink(onclick
);
151 if (onClickData
!= null) {
152 formObj
.ispopup
.checked
= true;
153 setPopupControlsDisabled(false);
155 var onClickWindowOptions
= parseOptions(onClickData
['options']);
156 var url
= onClickData
['url'];
158 formObj
.popupname
.value
= onClickData
['target'];
159 formObj
.popupurl
.value
= url
;
160 formObj
.popupwidth
.value
= getOption(onClickWindowOptions
, 'width');
161 formObj
.popupheight
.value
= getOption(onClickWindowOptions
, 'height');
163 formObj
.popupleft
.value
= getOption(onClickWindowOptions
, 'left');
164 formObj
.popuptop
.value
= getOption(onClickWindowOptions
, 'top');
166 if (formObj
.popupleft
.value
.indexOf('screen') != -1)
167 formObj
.popupleft
.value
= "c";
169 if (formObj
.popuptop
.value
.indexOf('screen') != -1)
170 formObj
.popuptop
.value
= "c";
172 formObj
.popuplocation
.checked
= getOption(onClickWindowOptions
, 'location') == "yes";
173 formObj
.popupscrollbars
.checked
= getOption(onClickWindowOptions
, 'scrollbars') == "yes";
174 formObj
.popupmenubar
.checked
= getOption(onClickWindowOptions
, 'menubar') == "yes";
175 formObj
.popupresizable
.checked
= getOption(onClickWindowOptions
, 'resizable') == "yes";
176 formObj
.popuptoolbar
.checked
= getOption(onClickWindowOptions
, 'toolbar') == "yes";
177 formObj
.popupstatus
.checked
= getOption(onClickWindowOptions
, 'status') == "yes";
178 formObj
.popupdependent
.checked
= getOption(onClickWindowOptions
, 'dependent') == "yes";
184 function parseFunction(onclick
) {
185 var formObj
= document
.forms
[0];
186 var onClickData
= parseLink(onclick
);
188 // TODO: Add stuff here
191 function getOption(opts
, name
) {
192 return typeof(opts
[name
]) == "undefined" ? "" : opts
[name
];
195 function setPopupControlsDisabled(state
) {
196 var formObj
= document
.forms
[0];
198 formObj
.popupname
.disabled
= state
;
199 formObj
.popupurl
.disabled
= state
;
200 formObj
.popupwidth
.disabled
= state
;
201 formObj
.popupheight
.disabled
= state
;
202 formObj
.popupleft
.disabled
= state
;
203 formObj
.popuptop
.disabled
= state
;
204 formObj
.popuplocation
.disabled
= state
;
205 formObj
.popupscrollbars
.disabled
= state
;
206 formObj
.popupmenubar
.disabled
= state
;
207 formObj
.popupresizable
.disabled
= state
;
208 formObj
.popuptoolbar
.disabled
= state
;
209 formObj
.popupstatus
.disabled
= state
;
210 formObj
.popupreturn
.disabled
= state
;
211 formObj
.popupdependent
.disabled
= state
;
213 setBrowserDisabled('popupurlbrowser', state
);
216 function parseLink(link
) {
217 link
= link
.replace(new RegExp(''', 'g'), "'");
219 var fnName
= link
.replace(new RegExp("\\s*([A-Za-z0-9\.]*)\\s*\\(.*", "gi"), "$1");
221 // Is function name a template function
222 var template
= templates
[fnName
];
225 var variableNames
= template
.match(new RegExp("'?\\$\\{[A-Za-z0-9\.]*\\}'?", "gi"));
226 var regExp
= "\\s*[A-Za-z0-9\.]*\\s*\\(";
228 for (var i
=0; i
<variableNames
.length
; i
++) {
230 if (variableNames
[i
].indexOf("'${") != -1)
233 regExp
+= "([0-9]*)";
235 replaceStr
+= "$" + (i
+1);
237 // Cleanup variable name
238 variableNames
[i
] = variableNames
[i
].replace(new RegExp("[^A-Za-z0-9]", "gi"), "");
240 if (i
!= variableNames
.length
-1) {
241 regExp
+= "\\s*,\\s*";
242 replaceStr
+= "<delim>";
249 // Build variable array
251 variables
["_function"] = fnName
;
252 var variableValues
= link
.replace(new RegExp(regExp
, "gi"), replaceStr
).split('<delim>');
253 for (var i
=0; i
<variableNames
.length
; i
++)
254 variables
[variableNames
[i
]] = variableValues
[i
];
262 function parseOptions(opts
) {
263 if (opts
== null || opts
== "")
266 // Cleanup the options
267 opts
= opts
.toLowerCase();
268 opts
= opts
.replace(/;/g
, ",");
269 opts
= opts
.replace(/[^0-9a-z=,]/g, "");
271 var optionChunks
= opts
.split(',');
274 for (var i
=0; i
<optionChunks
.length
; i
++) {
275 var parts
= optionChunks
[i
].split('=');
277 if (parts
.length
== 2)
278 options
[parts
[0]] = parts
[1];
284 function buildOnClick() {
285 var formObj
= document
.forms
[0];
287 if (!formObj
.ispopup
.checked
) {
288 formObj
.onclick
.value
= "";
292 var onclick
= "window.open('";
293 var url
= formObj
.popupurl
.value
;
295 onclick
+= url
+ "','";
296 onclick
+= formObj
.popupname
.value
+ "','";
298 if (formObj
.popuplocation
.checked
)
299 onclick
+= "location=yes,";
301 if (formObj
.popupscrollbars
.checked
)
302 onclick
+= "scrollbars=yes,";
304 if (formObj
.popupmenubar
.checked
)
305 onclick
+= "menubar=yes,";
307 if (formObj
.popupresizable
.checked
)
308 onclick
+= "resizable=yes,";
310 if (formObj
.popuptoolbar
.checked
)
311 onclick
+= "toolbar=yes,";
313 if (formObj
.popupstatus
.checked
)
314 onclick
+= "status=yes,";
316 if (formObj
.popupdependent
.checked
)
317 onclick
+= "dependent=yes,";
319 if (formObj
.popupwidth
.value
!= "")
320 onclick
+= "width=" + formObj
.popupwidth
.value
+ ",";
322 if (formObj
.popupheight
.value
!= "")
323 onclick
+= "height=" + formObj
.popupheight
.value
+ ",";
325 if (formObj
.popupleft
.value
!= "") {
326 if (formObj
.popupleft
.value
!= "c")
327 onclick
+= "left=" + formObj
.popupleft
.value
+ ",";
329 onclick
+= "left='+(screen.availWidth/2-" + (formObj
.popupwidth
.value
/2) + ")+',";
332 if (formObj
.popuptop
.value
!= "") {
333 if (formObj
.popuptop
.value
!= "c")
334 onclick
+= "top=" + formObj
.popuptop
.value
+ ",";
336 onclick
+= "top='+(screen.availHeight/2-" + (formObj
.popupheight
.value
/2) + ")+',";
339 if (onclick
.charAt(onclick
.length
-1) == ',')
340 onclick
= onclick
.substring(0, onclick
.length
-1);
344 if (formObj
.popupreturn
.checked
)
345 onclick
+= "return false;";
347 // tinyMCE.debug(onclick);
349 formObj
.onclick
.value
= onclick
;
351 if (formObj
.href
.value
== "")
352 formObj
.href
.value
= url
;
355 function setAttrib(elm
, attrib
, value
) {
356 var formObj
= document
.forms
[0];
357 var valueElm
= formObj
.elements
[attrib
.toLowerCase()];
358 var dom
= tinyMCEPopup
.editor
.dom
;
360 if (typeof(value
) == "undefined" || value
== null) {
364 value
= valueElm
.value
;
367 // Clean up the style
368 if (attrib
== 'style')
369 value
= dom
.serializeStyle(dom
.parseStyle(value
), 'a');
371 dom
.setAttrib(elm
, attrib
, value
);
374 function getAnchorListHTML(id
, target
) {
375 var ed
= tinyMCEPopup
.editor
, nodes
= ed
.dom
.select('a'), name
, i
, len
, html
= "";
377 for (i
=0, len
=nodes
.length
; i
<len
; i
++) {
378 if ((name
= ed
.dom
.getAttrib(nodes
[i
], "name")) != "")
379 html
+= '<option value="#' + name
+ '">' + name
+ '</option>';
385 html
= '<select id="' + id
+ '" name="' + id
+ '" class="mceAnchorList"'
386 + ' onchange="this.form.' + target
+ '.value=this.options[this.selectedIndex].value"'
388 + '<option value="">---</option>'
395 function insertAction() {
396 var inst
= tinyMCEPopup
.editor
;
397 var elm
, elementArray
, i
;
399 elm
= inst
.selection
.getNode();
400 checkPrefix(document
.forms
[0].href
);
402 elm
= inst
.dom
.getParent(elm
, "A");
404 // Remove element if there is no href
405 if (!document
.forms
[0].href
.value
) {
406 i
= inst
.selection
.getBookmark();
407 inst
.dom
.remove(elm
, 1);
408 inst
.selection
.moveToBookmark(i
);
409 tinyMCEPopup
.execCommand("mceEndUndoLevel");
410 tinyMCEPopup
.close();
414 // Create new anchor elements
416 inst
.getDoc().execCommand("unlink", false, null);
417 tinyMCEPopup
.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo
: 1});
419 elementArray
= tinymce
.grep(inst
.dom
.select("a"), function(n
) {return inst
.dom
.getAttrib(n
, 'href') == '#mce_temp_url#';});
420 for (i
=0; i
<elementArray
.length
; i
++)
421 setAllAttribs(elm
= elementArray
[i
]);
425 // Don't move caret if selection was image
426 if (elm
.childNodes
.length
!= 1 || elm
.firstChild
.nodeName
!= 'IMG') {
428 inst
.selection
.select(elm
);
429 inst
.selection
.collapse(0);
430 tinyMCEPopup
.storeSelection();
433 tinyMCEPopup
.execCommand("mceEndUndoLevel");
434 tinyMCEPopup
.close();
437 function setAllAttribs(elm
) {
438 var formObj
= document
.forms
[0];
439 var href
= formObj
.href
.value
.replace(/ /g
, '%20');
440 var target
= getSelectValue(formObj
, 'targetlist');
442 setAttrib(elm
, 'href', href
);
443 setAttrib(elm
, 'title');
444 setAttrib(elm
, 'target', target
== '_self' ? '' : target
);
445 setAttrib(elm
, 'id');
446 setAttrib(elm
, 'style');
447 setAttrib(elm
, 'class', getSelectValue(formObj
, 'classlist'));
448 setAttrib(elm
, 'rel');
449 setAttrib(elm
, 'rev');
450 setAttrib(elm
, 'charset');
451 setAttrib(elm
, 'hreflang');
452 setAttrib(elm
, 'dir');
453 setAttrib(elm
, 'lang');
454 setAttrib(elm
, 'tabindex');
455 setAttrib(elm
, 'accesskey');
456 setAttrib(elm
, 'type');
457 setAttrib(elm
, 'onfocus');
458 setAttrib(elm
, 'onblur');
459 setAttrib(elm
, 'onclick');
460 setAttrib(elm
, 'ondblclick');
461 setAttrib(elm
, 'onmousedown');
462 setAttrib(elm
, 'onmouseup');
463 setAttrib(elm
, 'onmouseover');
464 setAttrib(elm
, 'onmousemove');
465 setAttrib(elm
, 'onmouseout');
466 setAttrib(elm
, 'onkeypress');
467 setAttrib(elm
, 'onkeydown');
468 setAttrib(elm
, 'onkeyup');
470 // Refresh in old MSIE
472 elm
.outerHTML
= elm
.outerHTML
;
475 function getSelectValue(form_obj
, field_name
) {
476 var elm
= form_obj
.elements
[field_name
];
478 if (!elm
|| elm
.options
== null || elm
.selectedIndex
== -1)
481 return elm
.options
[elm
.selectedIndex
].value
;
484 function getLinkListHTML(elm_id
, target_form_element
, onchange_func
) {
485 if (typeof(tinyMCELinkList
) == "undefined" || tinyMCELinkList
.length
== 0)
490 html
+= '<select id="' + elm_id
+ '" name="' + elm_id
+ '"';
491 html
+= ' class="mceLinkList" onchange="this.form.' + target_form_element
+ '.value=';
492 html
+= 'this.options[this.selectedIndex].value;';
494 if (typeof(onchange_func
) != "undefined")
495 html
+= onchange_func
+ '(\'' + target_form_element
+ '\',this.options[this.selectedIndex].text,this.options[this.selectedIndex].value);';
497 html
+= '"><option value="">---</option>';
499 for (var i
=0; i
<tinyMCELinkList
.length
; i
++)
500 html
+= '<option value="' + tinyMCELinkList
[i
][1] + '">' + tinyMCELinkList
[i
][0] + '</option>';
506 // tinyMCE.debug('-- image list start --', html, '-- image list end --');
509 function getTargetListHTML(elm_id
, target_form_element
) {
510 var targets
= tinyMCEPopup
.getParam('theme_advanced_link_targets', '').split(';');
513 html
+= '<select id="' + elm_id
+ '" name="' + elm_id
+ '" onchange="this.form.' + target_form_element
+ '.value=';
514 html
+= 'this.options[this.selectedIndex].value;">';
515 html
+= '<option value="_self">' + tinyMCEPopup
.getLang('advlink_dlg.target_same') + '</option>';
516 html
+= '<option value="_blank">' + tinyMCEPopup
.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';
517 html
+= '<option value="_parent">' + tinyMCEPopup
.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';
518 html
+= '<option value="_top">' + tinyMCEPopup
.getLang('advlink_dlg.target_top') + ' (_top)</option>';
520 for (var i
=0; i
<targets
.length
; i
++) {
523 if (targets
[i
] == "")
526 key
= targets
[i
].split('=')[0];
527 value
= targets
[i
].split('=')[1];
529 html
+= '<option value="' + key
+ '">' + value
+ ' (' + key
+ ')</option>';
539 tinyMCEPopup
.onInit
.add(init
);