* upgraded new version of tiny_mce
[citadel.git] / webcit / tiny_mce / plugins / advlink / js / advlink.js
bloba89625014a404d81203c5b429dc191787a4923c8
1 /* Functions for the advlink plugin popup */\r
2 \r
3 tinyMCEPopup.requireLangPack();\r
4 \r
5 var templates = {\r
6         "window.open" : "window.open('${url}','${target}','${options}')"\r
7 };\r
8 \r
9 function preinit() {\r
10         var url;\r
12         if (url = tinyMCEPopup.getParam("external_link_list_url"))\r
13                 document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');\r
14 }\r
16 function changeClass() {\r
17         var f = document.forms[0];\r
19         f.classes.value = getSelectValue(f, 'classlist');\r
20 }\r
22 function init() {\r
23         tinyMCEPopup.resizeToInnerSize();\r
25         var formObj = document.forms[0];\r
26         var inst = tinyMCEPopup.editor;\r
27         var elm = inst.selection.getNode();\r
28         var action = "insert";\r
29         var html;\r
31         document.getElementById('hrefbrowsercontainer').innerHTML = getBrowserHTML('hrefbrowser','href','file','advlink');\r
32         document.getElementById('popupurlbrowsercontainer').innerHTML = getBrowserHTML('popupurlbrowser','popupurl','file','advlink');\r
33         document.getElementById('linklisthrefcontainer').innerHTML = getLinkListHTML('linklisthref','href');\r
34         document.getElementById('anchorlistcontainer').innerHTML = getAnchorListHTML('anchorlist','href');\r
35         document.getElementById('targetlistcontainer').innerHTML = getTargetListHTML('targetlist','target');\r
37         // Link list\r
38         html = getLinkListHTML('linklisthref','href');\r
39         if (html == "")\r
40                 document.getElementById("linklisthrefrow").style.display = 'none';\r
41         else\r
42                 document.getElementById("linklisthrefcontainer").innerHTML = html;\r
44         // Resize some elements\r
45         if (isVisible('hrefbrowser'))\r
46                 document.getElementById('href').style.width = '260px';\r
48         if (isVisible('popupurlbrowser'))\r
49                 document.getElementById('popupurl').style.width = '180px';\r
51         elm = inst.dom.getParent(elm, "A");\r
52         if (elm != null && elm.nodeName == "A")\r
53                 action = "update";\r
55         formObj.insert.value = tinyMCEPopup.getLang(action, 'Insert', true); \r
57         setPopupControlsDisabled(true);\r
59         if (action == "update") {\r
60                 var href = inst.dom.getAttrib(elm, 'href');\r
61                 var onclick = inst.dom.getAttrib(elm, 'onclick');\r
63                 // Setup form data\r
64                 setFormValue('href', href);\r
65                 setFormValue('title', inst.dom.getAttrib(elm, 'title'));\r
66                 setFormValue('id', inst.dom.getAttrib(elm, 'id'));\r
67                 setFormValue('style', inst.dom.getAttrib(elm, "style"));\r
68                 setFormValue('rel', inst.dom.getAttrib(elm, 'rel'));\r
69                 setFormValue('rev', inst.dom.getAttrib(elm, 'rev'));\r
70                 setFormValue('charset', inst.dom.getAttrib(elm, 'charset'));\r
71                 setFormValue('hreflang', inst.dom.getAttrib(elm, 'hreflang'));\r
72                 setFormValue('dir', inst.dom.getAttrib(elm, 'dir'));\r
73                 setFormValue('lang', inst.dom.getAttrib(elm, 'lang'));\r
74                 setFormValue('tabindex', inst.dom.getAttrib(elm, 'tabindex', typeof(elm.tabindex) != "undefined" ? elm.tabindex : ""));\r
75                 setFormValue('accesskey', inst.dom.getAttrib(elm, 'accesskey', typeof(elm.accesskey) != "undefined" ? elm.accesskey : ""));\r
76                 setFormValue('type', inst.dom.getAttrib(elm, 'type'));\r
77                 setFormValue('onfocus', inst.dom.getAttrib(elm, 'onfocus'));\r
78                 setFormValue('onblur', inst.dom.getAttrib(elm, 'onblur'));\r
79                 setFormValue('onclick', onclick);\r
80                 setFormValue('ondblclick', inst.dom.getAttrib(elm, 'ondblclick'));\r
81                 setFormValue('onmousedown', inst.dom.getAttrib(elm, 'onmousedown'));\r
82                 setFormValue('onmouseup', inst.dom.getAttrib(elm, 'onmouseup'));\r
83                 setFormValue('onmouseover', inst.dom.getAttrib(elm, 'onmouseover'));\r
84                 setFormValue('onmousemove', inst.dom.getAttrib(elm, 'onmousemove'));\r
85                 setFormValue('onmouseout', inst.dom.getAttrib(elm, 'onmouseout'));\r
86                 setFormValue('onkeypress', inst.dom.getAttrib(elm, 'onkeypress'));\r
87                 setFormValue('onkeydown', inst.dom.getAttrib(elm, 'onkeydown'));\r
88                 setFormValue('onkeyup', inst.dom.getAttrib(elm, 'onkeyup'));\r
89                 setFormValue('target', inst.dom.getAttrib(elm, 'target'));\r
90                 setFormValue('classes', inst.dom.getAttrib(elm, 'class'));\r
92                 // Parse onclick data\r
93                 if (onclick != null && onclick.indexOf('window.open') != -1)\r
94                         parseWindowOpen(onclick);\r
95                 else\r
96                         parseFunction(onclick);\r
98                 // Select by the values\r
99                 selectByValue(formObj, 'dir', inst.dom.getAttrib(elm, 'dir'));\r
100                 selectByValue(formObj, 'rel', inst.dom.getAttrib(elm, 'rel'));\r
101                 selectByValue(formObj, 'rev', inst.dom.getAttrib(elm, 'rev'));\r
102                 selectByValue(formObj, 'linklisthref', href);\r
104                 if (href.charAt(0) == '#')\r
105                         selectByValue(formObj, 'anchorlist', href);\r
107                 addClassesToList('classlist', 'advlink_styles');\r
109                 selectByValue(formObj, 'classlist', inst.dom.getAttrib(elm, 'class'), true);\r
110                 selectByValue(formObj, 'targetlist', inst.dom.getAttrib(elm, 'target'), true);\r
111         } else\r
112                 addClassesToList('classlist', 'advlink_styles');\r
115 function checkPrefix(n) {\r
116         if (n.value && Validator.isEmail(n) && !/^\s*mailto:/i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_email')))\r
117                 n.value = 'mailto:' + n.value;\r
119         if (/^\s*www./i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_external')))\r
120                 n.value = 'http://' + n.value;\r
123 function setFormValue(name, value) {\r
124         document.forms[0].elements[name].value = value;\r
127 function parseWindowOpen(onclick) {\r
128         var formObj = document.forms[0];\r
130         // Preprocess center code\r
131         if (onclick.indexOf('return false;') != -1) {\r
132                 formObj.popupreturn.checked = true;\r
133                 onclick = onclick.replace('return false;', '');\r
134         } else\r
135                 formObj.popupreturn.checked = false;\r
137         var onClickData = parseLink(onclick);\r
139         if (onClickData != null) {\r
140                 formObj.ispopup.checked = true;\r
141                 setPopupControlsDisabled(false);\r
143                 var onClickWindowOptions = parseOptions(onClickData['options']);\r
144                 var url = onClickData['url'];\r
146                 formObj.popupname.value = onClickData['target'];\r
147                 formObj.popupurl.value = url;\r
148                 formObj.popupwidth.value = getOption(onClickWindowOptions, 'width');\r
149                 formObj.popupheight.value = getOption(onClickWindowOptions, 'height');\r
151                 formObj.popupleft.value = getOption(onClickWindowOptions, 'left');\r
152                 formObj.popuptop.value = getOption(onClickWindowOptions, 'top');\r
154                 if (formObj.popupleft.value.indexOf('screen') != -1)\r
155                         formObj.popupleft.value = "c";\r
157                 if (formObj.popuptop.value.indexOf('screen') != -1)\r
158                         formObj.popuptop.value = "c";\r
160                 formObj.popuplocation.checked = getOption(onClickWindowOptions, 'location') == "yes";\r
161                 formObj.popupscrollbars.checked = getOption(onClickWindowOptions, 'scrollbars') == "yes";\r
162                 formObj.popupmenubar.checked = getOption(onClickWindowOptions, 'menubar') == "yes";\r
163                 formObj.popupresizable.checked = getOption(onClickWindowOptions, 'resizable') == "yes";\r
164                 formObj.popuptoolbar.checked = getOption(onClickWindowOptions, 'toolbar') == "yes";\r
165                 formObj.popupstatus.checked = getOption(onClickWindowOptions, 'status') == "yes";\r
166                 formObj.popupdependent.checked = getOption(onClickWindowOptions, 'dependent') == "yes";\r
168                 buildOnClick();\r
169         }\r
172 function parseFunction(onclick) {\r
173         var formObj = document.forms[0];\r
174         var onClickData = parseLink(onclick);\r
176         // TODO: Add stuff here\r
179 function getOption(opts, name) {\r
180         return typeof(opts[name]) == "undefined" ? "" : opts[name];\r
183 function setPopupControlsDisabled(state) {\r
184         var formObj = document.forms[0];\r
186         formObj.popupname.disabled = state;\r
187         formObj.popupurl.disabled = state;\r
188         formObj.popupwidth.disabled = state;\r
189         formObj.popupheight.disabled = state;\r
190         formObj.popupleft.disabled = state;\r
191         formObj.popuptop.disabled = state;\r
192         formObj.popuplocation.disabled = state;\r
193         formObj.popupscrollbars.disabled = state;\r
194         formObj.popupmenubar.disabled = state;\r
195         formObj.popupresizable.disabled = state;\r
196         formObj.popuptoolbar.disabled = state;\r
197         formObj.popupstatus.disabled = state;\r
198         formObj.popupreturn.disabled = state;\r
199         formObj.popupdependent.disabled = state;\r
201         setBrowserDisabled('popupurlbrowser', state);\r
204 function parseLink(link) {\r
205         link = link.replace(new RegExp('&#39;', 'g'), "'");\r
207         var fnName = link.replace(new RegExp("\\s*([A-Za-z0-9\.]*)\\s*\\(.*", "gi"), "$1");\r
209         // Is function name a template function\r
210         var template = templates[fnName];\r
211         if (template) {\r
212                 // Build regexp\r
213                 var variableNames = template.match(new RegExp("'?\\$\\{[A-Za-z0-9\.]*\\}'?", "gi"));\r
214                 var regExp = "\\s*[A-Za-z0-9\.]*\\s*\\(";\r
215                 var replaceStr = "";\r
216                 for (var i=0; i<variableNames.length; i++) {\r
217                         // Is string value\r
218                         if (variableNames[i].indexOf("'${") != -1)\r
219                                 regExp += "'(.*)'";\r
220                         else // Number value\r
221                                 regExp += "([0-9]*)";\r
223                         replaceStr += "$" + (i+1);\r
225                         // Cleanup variable name\r
226                         variableNames[i] = variableNames[i].replace(new RegExp("[^A-Za-z0-9]", "gi"), "");\r
228                         if (i != variableNames.length-1) {\r
229                                 regExp += "\\s*,\\s*";\r
230                                 replaceStr += "<delim>";\r
231                         } else\r
232                                 regExp += ".*";\r
233                 }\r
235                 regExp += "\\);?";\r
237                 // Build variable array\r
238                 var variables = [];\r
239                 variables["_function"] = fnName;\r
240                 var variableValues = link.replace(new RegExp(regExp, "gi"), replaceStr).split('<delim>');\r
241                 for (var i=0; i<variableNames.length; i++)\r
242                         variables[variableNames[i]] = variableValues[i];\r
244                 return variables;\r
245         }\r
247         return null;\r
250 function parseOptions(opts) {\r
251         if (opts == null || opts == "")\r
252                 return [];\r
254         // Cleanup the options\r
255         opts = opts.toLowerCase();\r
256         opts = opts.replace(/;/g, ",");\r
257         opts = opts.replace(/[^0-9a-z=,]/g, "");\r
259         var optionChunks = opts.split(',');\r
260         var options = [];\r
262         for (var i=0; i<optionChunks.length; i++) {\r
263                 var parts = optionChunks[i].split('=');\r
265                 if (parts.length == 2)\r
266                         options[parts[0]] = parts[1];\r
267         }\r
269         return options;\r
272 function buildOnClick() {\r
273         var formObj = document.forms[0];\r
275         if (!formObj.ispopup.checked) {\r
276                 formObj.onclick.value = "";\r
277                 return;\r
278         }\r
280         var onclick = "window.open('";\r
281         var url = formObj.popupurl.value;\r
283         onclick += url + "','";\r
284         onclick += formObj.popupname.value + "','";\r
286         if (formObj.popuplocation.checked)\r
287                 onclick += "location=yes,";\r
289         if (formObj.popupscrollbars.checked)\r
290                 onclick += "scrollbars=yes,";\r
292         if (formObj.popupmenubar.checked)\r
293                 onclick += "menubar=yes,";\r
295         if (formObj.popupresizable.checked)\r
296                 onclick += "resizable=yes,";\r
298         if (formObj.popuptoolbar.checked)\r
299                 onclick += "toolbar=yes,";\r
301         if (formObj.popupstatus.checked)\r
302                 onclick += "status=yes,";\r
304         if (formObj.popupdependent.checked)\r
305                 onclick += "dependent=yes,";\r
307         if (formObj.popupwidth.value != "")\r
308                 onclick += "width=" + formObj.popupwidth.value + ",";\r
310         if (formObj.popupheight.value != "")\r
311                 onclick += "height=" + formObj.popupheight.value + ",";\r
313         if (formObj.popupleft.value != "") {\r
314                 if (formObj.popupleft.value != "c")\r
315                         onclick += "left=" + formObj.popupleft.value + ",";\r
316                 else\r
317                         onclick += "left='+(screen.availWidth/2-" + (formObj.popupwidth.value/2) + ")+',";\r
318         }\r
320         if (formObj.popuptop.value != "") {\r
321                 if (formObj.popuptop.value != "c")\r
322                         onclick += "top=" + formObj.popuptop.value + ",";\r
323                 else\r
324                         onclick += "top='+(screen.availHeight/2-" + (formObj.popupheight.value/2) + ")+',";\r
325         }\r
327         if (onclick.charAt(onclick.length-1) == ',')\r
328                 onclick = onclick.substring(0, onclick.length-1);\r
330         onclick += "');";\r
332         if (formObj.popupreturn.checked)\r
333                 onclick += "return false;";\r
335         // tinyMCE.debug(onclick);\r
337         formObj.onclick.value = onclick;\r
339         if (formObj.href.value == "")\r
340                 formObj.href.value = url;\r
343 function setAttrib(elm, attrib, value) {\r
344         var formObj = document.forms[0];\r
345         var valueElm = formObj.elements[attrib.toLowerCase()];\r
346         var dom = tinyMCEPopup.editor.dom;\r
348         if (typeof(value) == "undefined" || value == null) {\r
349                 value = "";\r
351                 if (valueElm)\r
352                         value = valueElm.value;\r
353         }\r
355         // Clean up the style\r
356         if (attrib == 'style')\r
357                 value = dom.serializeStyle(dom.parseStyle(value));\r
359         dom.setAttrib(elm, attrib, value);\r
362 function getAnchorListHTML(id, target) {\r
363         var inst = tinyMCEPopup.editor;\r
364         var nodes = inst.dom.select('a.mceItemAnchor,img.mceItemAnchor'), name, i;\r
365         var html = "";\r
367         html += '<select id="' + id + '" name="' + id + '" class="mceAnchorList" o2nfocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target + '.value=';\r
368         html += 'this.options[this.selectedIndex].value;">';\r
369         html += '<option value="">---</option>';\r
371         for (i=0; i<nodes.length; i++) {\r
372                 if ((name = inst.dom.getAttrib(nodes[i], "name")) != "")\r
373                         html += '<option value="#' + name + '">' + name + '</option>';\r
374         }\r
376         html += '</select>';\r
378         return html;\r
381 function insertAction() {\r
382         var inst = tinyMCEPopup.editor;\r
383         var elm, elementArray, i;\r
385         elm = inst.selection.getNode();\r
386         checkPrefix(document.forms[0].href);\r
388         elm = inst.dom.getParent(elm, "A");\r
390         // Remove element if there is no href\r
391         if (!document.forms[0].href.value) {\r
392                 tinyMCEPopup.execCommand("mceBeginUndoLevel");\r
393                 i = inst.selection.getBookmark();\r
394                 inst.dom.remove(elm, 1);\r
395                 inst.selection.moveToBookmark(i);\r
396                 tinyMCEPopup.execCommand("mceEndUndoLevel");\r
397                 tinyMCEPopup.close();\r
398                 return;\r
399         }\r
401         tinyMCEPopup.execCommand("mceBeginUndoLevel");\r
403         // Create new anchor elements\r
404         if (elm == null) {\r
405                 tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});\r
407                 elementArray = tinymce.grep(inst.dom.select("a"), function(n) {return inst.dom.getAttrib(n, 'href') == '#mce_temp_url#';});\r
408                 for (i=0; i<elementArray.length; i++)\r
409                         setAllAttribs(elm = elementArray[i]);\r
410         } else\r
411                 setAllAttribs(elm);\r
413         // Don't move caret if selection was image\r
414         if (elm.childNodes.length != 1 || elm.firstChild.nodeName != 'IMG') {\r
415                 inst.focus();\r
416                 inst.selection.select(elm);\r
417                 inst.selection.collapse(0);\r
418                 tinyMCEPopup.storeSelection();\r
419         }\r
421         tinyMCEPopup.execCommand("mceEndUndoLevel");\r
422         tinyMCEPopup.close();\r
425 function setAllAttribs(elm) {\r
426         var formObj = document.forms[0];\r
427         var href = formObj.href.value;\r
428         var target = getSelectValue(formObj, 'targetlist');\r
430         setAttrib(elm, 'href', href);\r
431         setAttrib(elm, 'title');\r
432         setAttrib(elm, 'target', target == '_self' ? '' : target);\r
433         setAttrib(elm, 'id');\r
434         setAttrib(elm, 'style');\r
435         setAttrib(elm, 'class', getSelectValue(formObj, 'classlist'));\r
436         setAttrib(elm, 'rel');\r
437         setAttrib(elm, 'rev');\r
438         setAttrib(elm, 'charset');\r
439         setAttrib(elm, 'hreflang');\r
440         setAttrib(elm, 'dir');\r
441         setAttrib(elm, 'lang');\r
442         setAttrib(elm, 'tabindex');\r
443         setAttrib(elm, 'accesskey');\r
444         setAttrib(elm, 'type');\r
445         setAttrib(elm, 'onfocus');\r
446         setAttrib(elm, 'onblur');\r
447         setAttrib(elm, 'onclick');\r
448         setAttrib(elm, 'ondblclick');\r
449         setAttrib(elm, 'onmousedown');\r
450         setAttrib(elm, 'onmouseup');\r
451         setAttrib(elm, 'onmouseover');\r
452         setAttrib(elm, 'onmousemove');\r
453         setAttrib(elm, 'onmouseout');\r
454         setAttrib(elm, 'onkeypress');\r
455         setAttrib(elm, 'onkeydown');\r
456         setAttrib(elm, 'onkeyup');\r
458         // Refresh in old MSIE\r
459         if (tinyMCE.isMSIE5)\r
460                 elm.outerHTML = elm.outerHTML;\r
463 function getSelectValue(form_obj, field_name) {\r
464         var elm = form_obj.elements[field_name];\r
466         if (!elm || elm.options == null || elm.selectedIndex == -1)\r
467                 return "";\r
469         return elm.options[elm.selectedIndex].value;\r
472 function getLinkListHTML(elm_id, target_form_element, onchange_func) {\r
473         if (typeof(tinyMCELinkList) == "undefined" || tinyMCELinkList.length == 0)\r
474                 return "";\r
476         var html = "";\r
478         html += '<select id="' + elm_id + '" name="' + elm_id + '"';\r
479         html += ' class="mceLinkList" onfoc2us="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';\r
480         html += 'this.options[this.selectedIndex].value;';\r
482         if (typeof(onchange_func) != "undefined")\r
483                 html += onchange_func + '(\'' + target_form_element + '\',this.options[this.selectedIndex].text,this.options[this.selectedIndex].value);';\r
485         html += '"><option value="">---</option>';\r
487         for (var i=0; i<tinyMCELinkList.length; i++)\r
488                 html += '<option value="' + tinyMCELinkList[i][1] + '">' + tinyMCELinkList[i][0] + '</option>';\r
490         html += '</select>';\r
492         return html;\r
494         // tinyMCE.debug('-- image list start --', html, '-- image list end --');\r
497 function getTargetListHTML(elm_id, target_form_element) {\r
498         var targets = tinyMCEPopup.getParam('theme_advanced_link_targets', '').split(';');\r
499         var html = '';\r
501         html += '<select id="' + elm_id + '" name="' + elm_id + '" onf2ocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';\r
502         html += 'this.options[this.selectedIndex].value;">';\r
503         html += '<option value="_self">' + tinyMCEPopup.getLang('advlink_dlg.target_same') + '</option>';\r
504         html += '<option value="_blank">' + tinyMCEPopup.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';\r
505         html += '<option value="_parent">' + tinyMCEPopup.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';\r
506         html += '<option value="_top">' + tinyMCEPopup.getLang('advlink_dlg.target_top') + ' (_top)</option>';\r
508         for (var i=0; i<targets.length; i++) {\r
509                 var key, value;\r
511                 if (targets[i] == "")\r
512                         continue;\r
514                 key = targets[i].split('=')[0];\r
515                 value = targets[i].split('=')[1];\r
517                 html += '<option value="' + key + '">' + value + ' (' + key + ')</option>';\r
518         }\r
520         html += '</select>';\r
522         return html;\r
525 // While loading\r
526 preinit();\r
527 tinyMCEPopup.onInit.add(init);\r