PageRenderTime 36ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/tiny_mce/plugins/advlink/js/advlink.js

http://n23.googlecode.com/
JavaScript | 528 lines | 386 code | 120 blank | 22 comment | 114 complexity | 0aea2bd24bae136bc73f9ab2055f5397 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. /* Functions for the advlink plugin popup */
  2. tinyMCEPopup.requireLangPack();
  3. var templates = {
  4. "window.open" : "window.open('${url}','${target}','${options}')"
  5. };
  6. function preinit() {
  7. var url;
  8. if (url = tinyMCEPopup.getParam("external_link_list_url"))
  9. document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
  10. }
  11. function changeClass() {
  12. var f = document.forms[0];
  13. f.classes.value = getSelectValue(f, 'classlist');
  14. }
  15. function init() {
  16. tinyMCEPopup.resizeToInnerSize();
  17. var formObj = document.forms[0];
  18. var inst = tinyMCEPopup.editor;
  19. var elm = inst.selection.getNode();
  20. var action = "insert";
  21. var html;
  22. document.getElementById('hrefbrowsercontainer').innerHTML = getBrowserHTML('hrefbrowser','href','file','advlink');
  23. document.getElementById('popupurlbrowsercontainer').innerHTML = getBrowserHTML('popupurlbrowser','popupurl','file','advlink');
  24. document.getElementById('linklisthrefcontainer').innerHTML = getLinkListHTML('linklisthref','href');
  25. document.getElementById('anchorlistcontainer').innerHTML = getAnchorListHTML('anchorlist','href');
  26. document.getElementById('targetlistcontainer').innerHTML = getTargetListHTML('targetlist','target');
  27. // Link list
  28. html = getLinkListHTML('linklisthref','href');
  29. if (html == "")
  30. document.getElementById("linklisthrefrow").style.display = 'none';
  31. else
  32. document.getElementById("linklisthrefcontainer").innerHTML = html;
  33. // Resize some elements
  34. if (isVisible('hrefbrowser'))
  35. document.getElementById('href').style.width = '260px';
  36. if (isVisible('popupurlbrowser'))
  37. document.getElementById('popupurl').style.width = '180px';
  38. elm = inst.dom.getParent(elm, "A");
  39. if (elm != null && elm.nodeName == "A")
  40. action = "update";
  41. formObj.insert.value = tinyMCEPopup.getLang(action, 'Insert', true);
  42. setPopupControlsDisabled(true);
  43. if (action == "update") {
  44. var href = inst.dom.getAttrib(elm, 'href');
  45. var onclick = inst.dom.getAttrib(elm, 'onclick');
  46. // Setup form data
  47. setFormValue('href', href);
  48. setFormValue('title', inst.dom.getAttrib(elm, 'title'));
  49. setFormValue('id', inst.dom.getAttrib(elm, 'id'));
  50. setFormValue('style', inst.dom.getAttrib(elm, "style"));
  51. setFormValue('rel', inst.dom.getAttrib(elm, 'rel'));
  52. setFormValue('rev', inst.dom.getAttrib(elm, 'rev'));
  53. setFormValue('charset', inst.dom.getAttrib(elm, 'charset'));
  54. setFormValue('hreflang', inst.dom.getAttrib(elm, 'hreflang'));
  55. setFormValue('dir', inst.dom.getAttrib(elm, 'dir'));
  56. setFormValue('lang', inst.dom.getAttrib(elm, 'lang'));
  57. setFormValue('tabindex', inst.dom.getAttrib(elm, 'tabindex', typeof(elm.tabindex) != "undefined" ? elm.tabindex : ""));
  58. setFormValue('accesskey', inst.dom.getAttrib(elm, 'accesskey', typeof(elm.accesskey) != "undefined" ? elm.accesskey : ""));
  59. setFormValue('type', inst.dom.getAttrib(elm, 'type'));
  60. setFormValue('onfocus', inst.dom.getAttrib(elm, 'onfocus'));
  61. setFormValue('onblur', inst.dom.getAttrib(elm, 'onblur'));
  62. setFormValue('onclick', onclick);
  63. setFormValue('ondblclick', inst.dom.getAttrib(elm, 'ondblclick'));
  64. setFormValue('onmousedown', inst.dom.getAttrib(elm, 'onmousedown'));
  65. setFormValue('onmouseup', inst.dom.getAttrib(elm, 'onmouseup'));
  66. setFormValue('onmouseover', inst.dom.getAttrib(elm, 'onmouseover'));
  67. setFormValue('onmousemove', inst.dom.getAttrib(elm, 'onmousemove'));
  68. setFormValue('onmouseout', inst.dom.getAttrib(elm, 'onmouseout'));
  69. setFormValue('onkeypress', inst.dom.getAttrib(elm, 'onkeypress'));
  70. setFormValue('onkeydown', inst.dom.getAttrib(elm, 'onkeydown'));
  71. setFormValue('onkeyup', inst.dom.getAttrib(elm, 'onkeyup'));
  72. setFormValue('target', inst.dom.getAttrib(elm, 'target'));
  73. setFormValue('classes', inst.dom.getAttrib(elm, 'class'));
  74. // Parse onclick data
  75. if (onclick != null && onclick.indexOf('window.open') != -1)
  76. parseWindowOpen(onclick);
  77. else
  78. parseFunction(onclick);
  79. // Select by the values
  80. selectByValue(formObj, 'dir', inst.dom.getAttrib(elm, 'dir'));
  81. selectByValue(formObj, 'rel', inst.dom.getAttrib(elm, 'rel'));
  82. selectByValue(formObj, 'rev', inst.dom.getAttrib(elm, 'rev'));
  83. selectByValue(formObj, 'linklisthref', href);
  84. if (href.charAt(0) == '#')
  85. selectByValue(formObj, 'anchorlist', href);
  86. addClassesToList('classlist', 'advlink_styles');
  87. selectByValue(formObj, 'classlist', inst.dom.getAttrib(elm, 'class'), true);
  88. selectByValue(formObj, 'targetlist', inst.dom.getAttrib(elm, 'target'), true);
  89. } else
  90. addClassesToList('classlist', 'advlink_styles');
  91. }
  92. function checkPrefix(n) {
  93. if (n.value && Validator.isEmail(n) && !/^\s*mailto:/i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_email')))
  94. n.value = 'mailto:' + n.value;
  95. if (/^\s*www./i.test(n.value) && confirm(tinyMCEPopup.getLang('advlink_dlg.is_external')))
  96. n.value = 'http://' + n.value;
  97. }
  98. function setFormValue(name, value) {
  99. document.forms[0].elements[name].value = value;
  100. }
  101. function parseWindowOpen(onclick) {
  102. var formObj = document.forms[0];
  103. // Preprocess center code
  104. if (onclick.indexOf('return false;') != -1) {
  105. formObj.popupreturn.checked = true;
  106. onclick = onclick.replace('return false;', '');
  107. } else
  108. formObj.popupreturn.checked = false;
  109. var onClickData = parseLink(onclick);
  110. if (onClickData != null) {
  111. formObj.ispopup.checked = true;
  112. setPopupControlsDisabled(false);
  113. var onClickWindowOptions = parseOptions(onClickData['options']);
  114. var url = onClickData['url'];
  115. formObj.popupname.value = onClickData['target'];
  116. formObj.popupurl.value = url;
  117. formObj.popupwidth.value = getOption(onClickWindowOptions, 'width');
  118. formObj.popupheight.value = getOption(onClickWindowOptions, 'height');
  119. formObj.popupleft.value = getOption(onClickWindowOptions, 'left');
  120. formObj.popuptop.value = getOption(onClickWindowOptions, 'top');
  121. if (formObj.popupleft.value.indexOf('screen') != -1)
  122. formObj.popupleft.value = "c";
  123. if (formObj.popuptop.value.indexOf('screen') != -1)
  124. formObj.popuptop.value = "c";
  125. formObj.popuplocation.checked = getOption(onClickWindowOptions, 'location') == "yes";
  126. formObj.popupscrollbars.checked = getOption(onClickWindowOptions, 'scrollbars') == "yes";
  127. formObj.popupmenubar.checked = getOption(onClickWindowOptions, 'menubar') == "yes";
  128. formObj.popupresizable.checked = getOption(onClickWindowOptions, 'resizable') == "yes";
  129. formObj.popuptoolbar.checked = getOption(onClickWindowOptions, 'toolbar') == "yes";
  130. formObj.popupstatus.checked = getOption(onClickWindowOptions, 'status') == "yes";
  131. formObj.popupdependent.checked = getOption(onClickWindowOptions, 'dependent') == "yes";
  132. buildOnClick();
  133. }
  134. }
  135. function parseFunction(onclick) {
  136. var formObj = document.forms[0];
  137. var onClickData = parseLink(onclick);
  138. // TODO: Add stuff here
  139. }
  140. function getOption(opts, name) {
  141. return typeof(opts[name]) == "undefined" ? "" : opts[name];
  142. }
  143. function setPopupControlsDisabled(state) {
  144. var formObj = document.forms[0];
  145. formObj.popupname.disabled = state;
  146. formObj.popupurl.disabled = state;
  147. formObj.popupwidth.disabled = state;
  148. formObj.popupheight.disabled = state;
  149. formObj.popupleft.disabled = state;
  150. formObj.popuptop.disabled = state;
  151. formObj.popuplocation.disabled = state;
  152. formObj.popupscrollbars.disabled = state;
  153. formObj.popupmenubar.disabled = state;
  154. formObj.popupresizable.disabled = state;
  155. formObj.popuptoolbar.disabled = state;
  156. formObj.popupstatus.disabled = state;
  157. formObj.popupreturn.disabled = state;
  158. formObj.popupdependent.disabled = state;
  159. setBrowserDisabled('popupurlbrowser', state);
  160. }
  161. function parseLink(link) {
  162. link = link.replace(new RegExp('&#39;', 'g'), "'");
  163. var fnName = link.replace(new RegExp("\\s*([A-Za-z0-9\.]*)\\s*\\(.*", "gi"), "$1");
  164. // Is function name a template function
  165. var template = templates[fnName];
  166. if (template) {
  167. // Build regexp
  168. var variableNames = template.match(new RegExp("'?\\$\\{[A-Za-z0-9\.]*\\}'?", "gi"));
  169. var regExp = "\\s*[A-Za-z0-9\.]*\\s*\\(";
  170. var replaceStr = "";
  171. for (var i=0; i<variableNames.length; i++) {
  172. // Is string value
  173. if (variableNames[i].indexOf("'${") != -1)
  174. regExp += "'(.*)'";
  175. else // Number value
  176. regExp += "([0-9]*)";
  177. replaceStr += "$" + (i+1);
  178. // Cleanup variable name
  179. variableNames[i] = variableNames[i].replace(new RegExp("[^A-Za-z0-9]", "gi"), "");
  180. if (i != variableNames.length-1) {
  181. regExp += "\\s*,\\s*";
  182. replaceStr += "<delim>";
  183. } else
  184. regExp += ".*";
  185. }
  186. regExp += "\\);?";
  187. // Build variable array
  188. var variables = [];
  189. variables["_function"] = fnName;
  190. var variableValues = link.replace(new RegExp(regExp, "gi"), replaceStr).split('<delim>');
  191. for (var i=0; i<variableNames.length; i++)
  192. variables[variableNames[i]] = variableValues[i];
  193. return variables;
  194. }
  195. return null;
  196. }
  197. function parseOptions(opts) {
  198. if (opts == null || opts == "")
  199. return [];
  200. // Cleanup the options
  201. opts = opts.toLowerCase();
  202. opts = opts.replace(/;/g, ",");
  203. opts = opts.replace(/[^0-9a-z=,]/g, "");
  204. var optionChunks = opts.split(',');
  205. var options = [];
  206. for (var i=0; i<optionChunks.length; i++) {
  207. var parts = optionChunks[i].split('=');
  208. if (parts.length == 2)
  209. options[parts[0]] = parts[1];
  210. }
  211. return options;
  212. }
  213. function buildOnClick() {
  214. var formObj = document.forms[0];
  215. if (!formObj.ispopup.checked) {
  216. formObj.onclick.value = "";
  217. return;
  218. }
  219. var onclick = "window.open('";
  220. var url = formObj.popupurl.value;
  221. onclick += url + "','";
  222. onclick += formObj.popupname.value + "','";
  223. if (formObj.popuplocation.checked)
  224. onclick += "location=yes,";
  225. if (formObj.popupscrollbars.checked)
  226. onclick += "scrollbars=yes,";
  227. if (formObj.popupmenubar.checked)
  228. onclick += "menubar=yes,";
  229. if (formObj.popupresizable.checked)
  230. onclick += "resizable=yes,";
  231. if (formObj.popuptoolbar.checked)
  232. onclick += "toolbar=yes,";
  233. if (formObj.popupstatus.checked)
  234. onclick += "status=yes,";
  235. if (formObj.popupdependent.checked)
  236. onclick += "dependent=yes,";
  237. if (formObj.popupwidth.value != "")
  238. onclick += "width=" + formObj.popupwidth.value + ",";
  239. if (formObj.popupheight.value != "")
  240. onclick += "height=" + formObj.popupheight.value + ",";
  241. if (formObj.popupleft.value != "") {
  242. if (formObj.popupleft.value != "c")
  243. onclick += "left=" + formObj.popupleft.value + ",";
  244. else
  245. onclick += "left='+(screen.availWidth/2-" + (formObj.popupwidth.value/2) + ")+',";
  246. }
  247. if (formObj.popuptop.value != "") {
  248. if (formObj.popuptop.value != "c")
  249. onclick += "top=" + formObj.popuptop.value + ",";
  250. else
  251. onclick += "top='+(screen.availHeight/2-" + (formObj.popupheight.value/2) + ")+',";
  252. }
  253. if (onclick.charAt(onclick.length-1) == ',')
  254. onclick = onclick.substring(0, onclick.length-1);
  255. onclick += "');";
  256. if (formObj.popupreturn.checked)
  257. onclick += "return false;";
  258. // tinyMCE.debug(onclick);
  259. formObj.onclick.value = onclick;
  260. if (formObj.href.value == "")
  261. formObj.href.value = url;
  262. }
  263. function setAttrib(elm, attrib, value) {
  264. var formObj = document.forms[0];
  265. var valueElm = formObj.elements[attrib.toLowerCase()];
  266. var dom = tinyMCEPopup.editor.dom;
  267. if (typeof(value) == "undefined" || value == null) {
  268. value = "";
  269. if (valueElm)
  270. value = valueElm.value;
  271. }
  272. // Clean up the style
  273. if (attrib == 'style')
  274. value = dom.serializeStyle(dom.parseStyle(value));
  275. dom.setAttrib(elm, attrib, value);
  276. }
  277. function getAnchorListHTML(id, target) {
  278. var inst = tinyMCEPopup.editor;
  279. var nodes = inst.dom.select('a.mceItemAnchor,img.mceItemAnchor'), name, i;
  280. var html = "";
  281. html += '<select id="' + id + '" name="' + id + '" class="mceAnchorList" o2nfocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target + '.value=';
  282. html += 'this.options[this.selectedIndex].value;">';
  283. html += '<option value="">---</option>';
  284. for (i=0; i<nodes.length; i++) {
  285. if ((name = inst.dom.getAttrib(nodes[i], "name")) != "")
  286. html += '<option value="#' + name + '">' + name + '</option>';
  287. }
  288. html += '</select>';
  289. return html;
  290. }
  291. function insertAction() {
  292. var inst = tinyMCEPopup.editor;
  293. var elm, elementArray, i;
  294. elm = inst.selection.getNode();
  295. checkPrefix(document.forms[0].href);
  296. elm = inst.dom.getParent(elm, "A");
  297. // Remove element if there is no href
  298. if (!document.forms[0].href.value) {
  299. tinyMCEPopup.execCommand("mceBeginUndoLevel");
  300. i = inst.selection.getBookmark();
  301. inst.dom.remove(elm, 1);
  302. inst.selection.moveToBookmark(i);
  303. tinyMCEPopup.execCommand("mceEndUndoLevel");
  304. tinyMCEPopup.close();
  305. return;
  306. }
  307. tinyMCEPopup.execCommand("mceBeginUndoLevel");
  308. // Create new anchor elements
  309. if (elm == null) {
  310. tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
  311. elementArray = tinymce.grep(inst.dom.select("a"), function(n) {return inst.dom.getAttrib(n, 'href') == '#mce_temp_url#';});
  312. for (i=0; i<elementArray.length; i++)
  313. setAllAttribs(elm = elementArray[i]);
  314. } else
  315. setAllAttribs(elm);
  316. // Don't move caret if selection was image
  317. if (elm.childNodes.length != 1 || elm.firstChild.nodeName != 'IMG') {
  318. inst.focus();
  319. inst.selection.select(elm);
  320. inst.selection.collapse(0);
  321. tinyMCEPopup.storeSelection();
  322. }
  323. tinyMCEPopup.execCommand("mceEndUndoLevel");
  324. tinyMCEPopup.close();
  325. }
  326. function setAllAttribs(elm) {
  327. var formObj = document.forms[0];
  328. var href = formObj.href.value;
  329. var target = getSelectValue(formObj, 'targetlist');
  330. setAttrib(elm, 'href', href);
  331. setAttrib(elm, 'mce_href', href);
  332. setAttrib(elm, 'title');
  333. setAttrib(elm, 'target', target == '_self' ? '' : target);
  334. setAttrib(elm, 'id');
  335. setAttrib(elm, 'style');
  336. setAttrib(elm, 'class', getSelectValue(formObj, 'classlist'));
  337. setAttrib(elm, 'rel');
  338. setAttrib(elm, 'rev');
  339. setAttrib(elm, 'charset');
  340. setAttrib(elm, 'hreflang');
  341. setAttrib(elm, 'dir');
  342. setAttrib(elm, 'lang');
  343. setAttrib(elm, 'tabindex');
  344. setAttrib(elm, 'accesskey');
  345. setAttrib(elm, 'type');
  346. setAttrib(elm, 'onfocus');
  347. setAttrib(elm, 'onblur');
  348. setAttrib(elm, 'onclick');
  349. setAttrib(elm, 'ondblclick');
  350. setAttrib(elm, 'onmousedown');
  351. setAttrib(elm, 'onmouseup');
  352. setAttrib(elm, 'onmouseover');
  353. setAttrib(elm, 'onmousemove');
  354. setAttrib(elm, 'onmouseout');
  355. setAttrib(elm, 'onkeypress');
  356. setAttrib(elm, 'onkeydown');
  357. setAttrib(elm, 'onkeyup');
  358. // Refresh in old MSIE
  359. if (tinyMCE.isMSIE5)
  360. elm.outerHTML = elm.outerHTML;
  361. }
  362. function getSelectValue(form_obj, field_name) {
  363. var elm = form_obj.elements[field_name];
  364. if (elm == null || elm.options == null)
  365. return "";
  366. return elm.options[elm.selectedIndex].value;
  367. }
  368. function getLinkListHTML(elm_id, target_form_element, onchange_func) {
  369. if (typeof(tinyMCELinkList) == "undefined" || tinyMCELinkList.length == 0)
  370. return "";
  371. var html = "";
  372. html += '<select id="' + elm_id + '" name="' + elm_id + '"';
  373. html += ' class="mceLinkList" onfoc2us="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
  374. html += 'this.options[this.selectedIndex].value;';
  375. if (typeof(onchange_func) != "undefined")
  376. html += onchange_func + '(\'' + target_form_element + '\',this.options[this.selectedIndex].text,this.options[this.selectedIndex].value);';
  377. html += '"><option value="">---</option>';
  378. for (var i=0; i<tinyMCELinkList.length; i++)
  379. html += '<option value="' + tinyMCELinkList[i][1] + '">' + tinyMCELinkList[i][0] + '</option>';
  380. html += '</select>';
  381. return html;
  382. // tinyMCE.debug('-- image list start --', html, '-- image list end --');
  383. }
  384. function getTargetListHTML(elm_id, target_form_element) {
  385. var targets = tinyMCEPopup.getParam('theme_advanced_link_targets', '').split(';');
  386. var html = '';
  387. html += '<select id="' + elm_id + '" name="' + elm_id + '" onf2ocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
  388. html += 'this.options[this.selectedIndex].value;">';
  389. html += '<option value="_self">' + tinyMCEPopup.getLang('advlink_dlg.target_same') + '</option>';
  390. html += '<option value="_blank">' + tinyMCEPopup.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';
  391. html += '<option value="_parent">' + tinyMCEPopup.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';
  392. html += '<option value="_top">' + tinyMCEPopup.getLang('advlink_dlg.target_top') + ' (_top)</option>';
  393. for (var i=0; i<targets.length; i++) {
  394. var key, value;
  395. if (targets[i] == "")
  396. continue;
  397. key = targets[i].split('=')[0];
  398. value = targets[i].split('=')[1];
  399. html += '<option value="' + key + '">' + value + ' (' + key + ')</option>';
  400. }
  401. html += '</select>';
  402. return html;
  403. }
  404. // While loading
  405. preinit();
  406. tinyMCEPopup.onInit.add(init);