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");
57 if (elm != null && elm.nodeName == "A")
60 formObj.insert.value = tinyMCEPopup.getLang(action, 'Insert', true);
62 setPopupControlsDisabled(true);
64 if (action == "update") {
65 var href = inst.dom.getAttrib(elm, 'href');
66 var onclick = inst.dom.getAttrib(elm, 'onclick');
69 setFormValue('href', href);
70 setFormValue('title', inst.dom.getAttrib(elm, 'title'));
71 setFormValue('id', inst.dom.getAttrib(elm, 'id'));
72 setFormValue('style', inst.dom.getAttrib(elm, "style"));
73 setFormValue('rel', inst.dom.getAttrib(elm, 'rel'));
74 setFormValue('rev', inst.dom.getAttrib(elm, 'rev'));
75 setFormValue('charset', inst.dom.getAttrib(elm, 'charset'));
76 setFormValue('hreflang', inst.dom.getAttrib(elm, 'hreflang'));
77 setFormValue('dir', inst.dom.getAttrib(elm, 'dir'));
78 setFormValue('lang', inst.dom.getAttrib(elm, 'lang'));
79 setFormValue('tabindex', inst.dom.getAttrib(elm, 'tabindex', typeof(elm.tabindex) != "undefined" ? elm.tabindex : ""));
80 setFormValue('accesskey', inst.dom.getAttrib(elm, 'accesskey', typeof(elm.accesskey) != "undefined" ? elm.accesskey : ""));
81 setFormValue('type', inst.dom.getAttrib(elm, 'type'));
82 setFormValue('onfocus', inst.dom.getAttrib(elm, 'onfocus'));
83 setFormValue('onblur', inst.dom.getAttrib(elm, 'onblur'));
84 setFormValue('onclick', onclick);
85 setFormValue('ondblclick', inst.dom.getAttrib(elm, 'ondblclick'));
86 setFormValue('onmousedown', inst.dom.getAttrib(elm, 'onmousedown'));
87 setFormValue('onmouseup', inst.dom.getAttrib(elm, 'onmouseup'));
88 setFormValue('onmouseover', inst.dom.getAttrib(elm, 'onmouseover'));
89 setFormValue('onmousemove', inst.dom.getAttrib(elm, 'onmousemove'));
90 setFormValue('onmouseout', inst.dom.getAttrib(elm, 'onmouseout'));
91 setFormValue('onkeypress', inst.dom.getAttrib(elm, 'onkeypress'));
92 setFormValue('onkeydown', inst.dom.getAttrib(elm, 'onkeydown'));
93 setFormValue('onkeyup', inst.dom.getAttrib(elm, 'onkeyup'));
94 setFormValue('target', inst.dom.getAttrib(elm, 'target'));
95 setFormValue('classes', inst.dom.getAttrib(elm, 'class'));
98 if (onclick != null && onclick.indexOf('window.open') != -1)
99 parseWindowOpen(onclick);
101 parseFunction(onclick);
103 // Select by the values
104 selectByValue(formObj, 'dir', inst.dom.getAttrib(elm, 'dir'));
105 selectByValue(formObj, 'rel', inst.dom.getAttrib(elm, 'rel'));
106 selectByValue(formObj, 'rev', inst.dom.getAttrib(elm, 'rev'));
107 selectByValue(formObj, 'linklisthref', href);
109 if (href.charAt(0) == '#')
110 selectByValue(formObj, 'anchorlist', href);
112 addClassesToList('classlist', 'advlink_styles');
114 selectByValue(formObj, 'classlist', inst.dom.getAttrib(elm, 'class'), true);
115 selectByValue(formObj, 'targetlist', inst.dom.getAttrib(elm, 'target'), true);
117 addClassesToList('classlist', 'advlink_styles');
120 function checkPrefix(n) {
121 if (n.value && Validator.isEmail(n) && !/^\s*mailto:/i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_email')))
122 n.value = 'mailto:' + n.value;
124 if (/^\s*www\./i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_external')))
125 n.value = 'http://' + n.value;
128 function setFormValue(name, value) {
129 document.forms[0].elements[name].value = value;
132 function parseWindowOpen(onclick) {
133 var formObj = document.forms[0];
135 // Preprocess center code
136 if (onclick.indexOf('return false;') != -1) {
137 formObj.popupreturn.checked = true;
138 onclick = onclick.replace('return false;', '');
140 formObj.popupreturn.checked = false;
142 var onClickData = parseLink(onclick);
144 if (onClickData != null) {
145 formObj.ispopup.checked = true;
146 setPopupControlsDisabled(false);
148 var onClickWindowOptions = parseOptions(onClickData['options']);
149 var url = onClickData['url'];
151 formObj.popupname.value = onClickData['target'];
152 formObj.popupurl.value = url;
153 formObj.popupwidth.value = getOption(onClickWindowOptions, 'width');
154 formObj.popupheight.value = getOption(onClickWindowOptions, 'height');
156 formObj.popupleft.value = getOption(onClickWindowOptions, 'left');
157 formObj.popuptop.value = getOption(onClickWindowOptions, 'top');
159 if (formObj.popupleft.value.indexOf('screen') != -1)
160 formObj.popupleft.value = "c";
162 if (formObj.popuptop.value.indexOf('screen') != -1)
163 formObj.popuptop.value = "c";
165 formObj.popuplocation.checked = getOption(onClickWindowOptions, 'location') == "yes";
166 formObj.popupscrollbars.checked = getOption(onClickWindowOptions, 'scrollbars') == "yes";
167 formObj.popupmenubar.checked = getOption(onClickWindowOptions, 'menubar') == "yes";
168 formObj.popupresizable.checked = getOption(onClickWindowOptions, 'resizable') == "yes";
169 formObj.popuptoolbar.checked = getOption(onClickWindowOptions, 'toolbar') == "yes";
170 formObj.popupstatus.checked = getOption(onClickWindowOptions, 'status') == "yes";
171 formObj.popupdependent.checked = getOption(onClickWindowOptions, 'dependent') == "yes";
177 function parseFunction(onclick) {
178 var formObj = document.forms[0];
179 var onClickData = parseLink(onclick);
181 // TODO: Add stuff here
184 function getOption(opts, name) {
185 return typeof(opts[name]) == "undefined" ? "" : opts[name];
188 function setPopupControlsDisabled(state) {
189 var formObj = document.forms[0];
191 formObj.popupname.disabled = state;
192 formObj.popupurl.disabled = state;
193 formObj.popupwidth.disabled = state;
194 formObj.popupheight.disabled = state;
195 formObj.popupleft.disabled = state;
196 formObj.popuptop.disabled = state;
197 formObj.popuplocation.disabled = state;
198 formObj.popupscrollbars.disabled = state;
199 formObj.popupmenubar.disabled = state;
200 formObj.popupresizable.disabled = state;
201 formObj.popuptoolbar.disabled = state;
202 formObj.popupstatus.disabled = state;
203 formObj.popupreturn.disabled = state;
204 formObj.popupdependent.disabled = state;
206 setBrowserDisabled('popupurlbrowser', state);
209 function parseLink(link) {
210 link = link.replace(new RegExp(''', 'g'), "'");
212 var fnName = link.replace(new RegExp("\\s*([A-Za-z0-9\.]*)\\s*\\(.*", "gi"), "$1");
214 // Is function name a template function
215 var template = templates[fnName];
218 var variableNames = template.match(new RegExp("'?\\$\\{[A-Za-z0-9\.]*\\}'?", "gi"));
219 var regExp = "\\s*[A-Za-z0-9\.]*\\s*\\(";
221 for (var i=0; i<variableNames.length; i++) {
223 if (variableNames[i].indexOf("'${") != -1)
226 regExp += "([0-9]*)";
228 replaceStr += "$" + (i+1);
230 // Cleanup variable name
231 variableNames[i] = variableNames[i].replace(new RegExp("[^A-Za-z0-9]", "gi"), "");
233 if (i != variableNames.length-1) {
234 regExp += "\\s*,\\s*";
235 replaceStr += "<delim>";
242 // Build variable array
244 variables["_function"] = fnName;
245 var variableValues = link.replace(new RegExp(regExp, "gi"), replaceStr).split('<delim>');
246 for (var i=0; i<variableNames.length; i++)
247 variables[variableNames[i]] = variableValues[i];
255 function parseOptions(opts) {
256 if (opts == null || opts == "")
259 // Cleanup the options
260 opts = opts.toLowerCase();
261 opts = opts.replace(/;/g, ",");
262 opts = opts.replace(/[^0-9a-z=,]/g, "");
264 var optionChunks = opts.split(',');
267 for (var i=0; i<optionChunks.length; i++) {
268 var parts = optionChunks[i].split('=');
270 if (parts.length == 2)
271 options[parts[0]] = parts[1];
277 function buildOnClick() {
278 var formObj = document.forms[0];
280 if (!formObj.ispopup.checked) {
281 formObj.onclick.value = "";
285 var onclick = "window.open('";
286 var url = formObj.popupurl.value;
288 onclick += url + "','";
289 onclick += formObj.popupname.value + "','";
291 if (formObj.popuplocation.checked)
292 onclick += "location=yes,";
294 if (formObj.popupscrollbars.checked)
295 onclick += "scrollbars=yes,";
297 if (formObj.popupmenubar.checked)
298 onclick += "menubar=yes,";
300 if (formObj.popupresizable.checked)
301 onclick += "resizable=yes,";
303 if (formObj.popuptoolbar.checked)
304 onclick += "toolbar=yes,";
306 if (formObj.popupstatus.checked)
307 onclick += "status=yes,";
309 if (formObj.popupdependent.checked)
310 onclick += "dependent=yes,";
312 if (formObj.popupwidth.value != "")
313 onclick += "width=" + formObj.popupwidth.value + ",";
315 if (formObj.popupheight.value != "")
316 onclick += "height=" + formObj.popupheight.value + ",";
318 if (formObj.popupleft.value != "") {
319 if (formObj.popupleft.value != "c")
320 onclick += "left=" + formObj.popupleft.value + ",";
322 onclick += "left='+(screen.availWidth/2-" + (formObj.popupwidth.value/2) + ")+',";
325 if (formObj.popuptop.value != "") {
326 if (formObj.popuptop.value != "c")
327 onclick += "top=" + formObj.popuptop.value + ",";
329 onclick += "top='+(screen.availHeight/2-" + (formObj.popupheight.value/2) + ")+',";
332 if (onclick.charAt(onclick.length-1) == ',')
333 onclick = onclick.substring(0, onclick.length-1);
337 if (formObj.popupreturn.checked)
338 onclick += "return false;";
340 // tinyMCE.debug(onclick);
342 formObj.onclick.value = onclick;
344 if (formObj.href.value == "")
345 formObj.href.value = url;
348 function setAttrib(elm, attrib, value) {
349 var formObj = document.forms[0];
350 var valueElm = formObj.elements[attrib.toLowerCase()];
351 var dom = tinyMCEPopup.editor.dom;
353 if (typeof(value) == "undefined" || value == null) {
357 value = valueElm.value;
360 // Clean up the style
361 if (attrib == 'style')
362 value = dom.serializeStyle(dom.parseStyle(value), 'a');
364 dom.setAttrib(elm, attrib, value);
367 function getAnchorListHTML(id, target) {
368 var ed = tinyMCEPopup.editor, nodes = ed.dom.select('a'), name, i, len, html = "";
370 for (i=0, len=nodes.length; i<len; i++) {
371 if ((name = ed.dom.getAttrib(nodes[i], "name")) != "")
372 html += '<option value="#' + name + '">' + name + '</option>';
378 html = '<select id="' + id + '" name="' + id + '" class="mceAnchorList"'
379 + ' onchange="this.form.' + target + '.value=this.options[this.selectedIndex].value"'
381 + '<option value="">---</option>'
388 function insertAction() {
389 var inst = tinyMCEPopup.editor;
390 var elm, elementArray, i;
392 elm = inst.selection.getNode();
393 checkPrefix(document.forms[0].href);
395 elm = inst.dom.getParent(elm, "A");
397 // Remove element if there is no href
398 if (!document.forms[0].href.value) {
399 i = inst.selection.getBookmark();
400 inst.dom.remove(elm, 1);
401 inst.selection.moveToBookmark(i);
402 tinyMCEPopup.execCommand("mceEndUndoLevel");
403 tinyMCEPopup.close();
407 // Create new anchor elements
409 inst.getDoc().execCommand("unlink", false, null);
410 tinyMCEPopup.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo : 1});
412 elementArray = tinymce.grep(inst.dom.select("a"), function(n) {return inst.dom.getAttrib(n, 'href') == '#mce_temp_url#';});
413 for (i=0; i<elementArray.length; i++)
414 setAllAttribs(elm = elementArray[i]);
418 // Don't move caret if selection was image
419 if (elm.childNodes.length != 1 || elm.firstChild.nodeName != 'IMG') {
421 inst.selection.select(elm);
422 inst.selection.collapse(0);
423 tinyMCEPopup.storeSelection();
426 tinyMCEPopup.execCommand("mceEndUndoLevel");
427 tinyMCEPopup.close();
430 function setAllAttribs(elm) {
431 var formObj = document.forms[0];
432 var href = formObj.href.value.replace(/ /g, '%20');
433 var target = getSelectValue(formObj, 'targetlist');
435 setAttrib(elm, 'href', href);
436 setAttrib(elm, 'title');
437 setAttrib(elm, 'target', target == '_self' ? '' : target);
438 setAttrib(elm, 'id');
439 setAttrib(elm, 'style');
440 setAttrib(elm, 'class', getSelectValue(formObj, 'classlist'));
441 setAttrib(elm, 'rel');
442 setAttrib(elm, 'rev');
443 setAttrib(elm, 'charset');
444 setAttrib(elm, 'hreflang');
445 setAttrib(elm, 'dir');
446 setAttrib(elm, 'lang');
447 setAttrib(elm, 'tabindex');
448 setAttrib(elm, 'accesskey');
449 setAttrib(elm, 'type');
450 setAttrib(elm, 'onfocus');
451 setAttrib(elm, 'onblur');
452 setAttrib(elm, 'onclick');
453 setAttrib(elm, 'ondblclick');
454 setAttrib(elm, 'onmousedown');
455 setAttrib(elm, 'onmouseup');
456 setAttrib(elm, 'onmouseover');
457 setAttrib(elm, 'onmousemove');
458 setAttrib(elm, 'onmouseout');
459 setAttrib(elm, 'onkeypress');
460 setAttrib(elm, 'onkeydown');
461 setAttrib(elm, 'onkeyup');
463 // Refresh in old MSIE
465 elm.outerHTML = elm.outerHTML;
468 function getSelectValue(form_obj, field_name) {
469 var elm = form_obj.elements[field_name];
471 if (!elm || elm.options == null || elm.selectedIndex == -1)
474 return elm.options[elm.selectedIndex].value;
477 function getLinkListHTML(elm_id, target_form_element, onchange_func) {
478 if (typeof(tinyMCELinkList) == "undefined" || tinyMCELinkList.length == 0)
483 html += '<select id="' + elm_id + '" name="' + elm_id + '"';
484 html += ' class="mceLinkList" onfoc2us="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
485 html += 'this.options[this.selectedIndex].value;';
487 if (typeof(onchange_func) != "undefined")
488 html += onchange_func + '(\'' + target_form_element + '\',this.options[this.selectedIndex].text,this.options[this.selectedIndex].value);';
490 html += '"><option value="">---</option>';
492 for (var i=0; i<tinyMCELinkList.length; i++)
493 html += '<option value="' + tinyMCELinkList[i][1] + '">' + tinyMCELinkList[i][0] + '</option>';
499 // tinyMCE.debug('-- image list start --', html, '-- image list end --');
502 function getTargetListHTML(elm_id, target_form_element) {
503 var targets = tinyMCEPopup.getParam('theme_advanced_link_targets', '').split(';');
506 html += '<select id="' + elm_id + '" name="' + elm_id + '" onf2ocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
507 html += 'this.options[this.selectedIndex].value;">';
508 html += '<option value="_self">' + tinyMCEPopup.getLang('advlink_dlg.target_same') + '</option>';
509 html += '<option value="_blank">' + tinyMCEPopup.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';
510 html += '<option value="_parent">' + tinyMCEPopup.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';
511 html += '<option value="_top">' + tinyMCEPopup.getLang('advlink_dlg.target_top') + ' (_top)</option>';
513 for (var i=0; i<targets.length; i++) {
516 if (targets[i] == "")
519 key = targets[i].split('=')[0];
520 value = targets[i].split('=')[1];
522 html += '<option value="' + key + '">' + value + ' (' + key + ')</option>';
532 tinyMCEPopup.onInit.add(init);