Advertisement
Guest User

wysiwyg.js

a guest
Jul 9th, 2022
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function ($) {
  3.     "use strict";
  4.  
  5.     if (undefined === $.wysiwyg) {
  6.         throw "wysiwyg.link.js depends on $.wysiwyg";
  7.     }
  8.  
  9.     if (!$.wysiwyg.controls) {
  10.         $.wysiwyg.controls = {};
  11.     }
  12.  
  13.     /*
  14.     * Wysiwyg namespace: public properties and methods
  15.     */
  16.     $.wysiwyg.controls.link = {
  17.         init: function (Wysiwyg) {
  18.             var self = this, elements, dialog, url, a, selection,
  19.                 formLinkHtml, dialogReplacements, key, translation, regexp,
  20.                 baseUrl, img;
  21.  
  22.             dialogReplacements = {
  23.                 legend: "Insert Link",
  24.                 url   : "Link URL",
  25.                 title : "Link Title",
  26.                 target: "Link Target",
  27.                 submit: "Insert Link",
  28.                 reset: "Cancel"
  29.             };
  30.  
  31.             formLinkHtml = '<form class="wysiwyg"><fieldset><legend>{legend}</legend>' +
  32.                 '<label>{url}: <input type="text" name="linkhref" value=""/></label>' +
  33.                 '<label>{title}: <input type="text" name="linktitle" value=""/></label>' +
  34.                 '<label>{target}: <input type="text" name="linktarget" value=""/></label>' +
  35.                 '<input type="submit" class="button" value="{submit}"/> ' +
  36.                 '<input type="reset" value="{reset}"/></fieldset></form>';
  37.  
  38.             for (key in dialogReplacements) {
  39.                 if ($.wysiwyg.i18n) {
  40.                     translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs.link");
  41.  
  42.                     if (translation === dialogReplacements[key]) { // if not translated search in dialogs
  43.                         translation = $.wysiwyg.i18n.t(dialogReplacements[key], "dialogs");
  44.                     }
  45.  
  46.                     dialogReplacements[key] = translation;
  47.                 }
  48.  
  49.                 regexp = new RegExp("{" + key + "}", "g");
  50.                 formLinkHtml = formLinkHtml.replace(regexp, dialogReplacements[key]);
  51.             }
  52.  
  53.             a = {
  54.                 self: Wysiwyg.dom.getElement("a"), // link to element node
  55.                 href: "http://",
  56.                 title: "",
  57.                 target: ""
  58.             };
  59.  
  60.             if (a.self) {
  61.                 a.href = a.self.href ? a.self.href : a.href;
  62.                 a.title = a.self.title ? a.self.title : "";
  63.                 a.target = a.self.target ? a.self.target : "";
  64.             }
  65.  
  66.       // IA: archive.less destroys the style (may no longer be true)
  67.       // of this jquery ui dialog box, so
  68.       // turn off dialog support for now, and fall back to window.prompt
  69.             //if ($.fn.dialog) {
  70.             if (false) {
  71.                 elements = $(formLinkHtml);
  72.                 elements.find("input[name=linkhref]").val(a.href);
  73.                 elements.find("input[name=linktitle]").val(a.title);
  74.                 elements.find("input[name=linktarget]").val(a.target);
  75.  
  76.                 if ($.browser.msie) {
  77.                     try {
  78.                         dialog = elements.appendTo(Wysiwyg.editorDoc.body);
  79.                     } catch (err) {
  80.                         dialog = elements.appendTo("body");
  81.                     }
  82.                 } else {
  83.                     dialog = elements.appendTo("body");
  84.                 }
  85.  
  86.                 dialog.dialog({
  87.                     modal: true,
  88.                     open: function (ev, ui) {
  89.                         $("input:submit", dialog).click(function (e) {
  90.                             e.preventDefault();
  91.  
  92.                             var url = $('input[name="linkhref"]', dialog).val(),
  93.                                 title = $('input[name="linktitle"]', dialog).val(),
  94.                                 target = $('input[name="linktarget"]', dialog).val(),
  95.                                 baseUrl,
  96.                                 img;
  97.  
  98.                             if (Wysiwyg.options.controlLink.forceRelativeUrls) {
  99.                                 baseUrl = window.location.protocol + "//" + window.location.hostname;
  100.                                 if (0 === url.indexOf(baseUrl)) {
  101.                                     url = url.substr(baseUrl.length);
  102.                                 }
  103.                             }
  104.  
  105.                             if (a.self) {
  106.                                 if ("string" === typeof (url)) {
  107.                                     if (url.length > 0) {
  108.                                         // to preserve all link attributes
  109.                                         $(a.self).attr("href", url).attr("title", title).attr("target", target);
  110.                                     } else {
  111.                                         $(a.self).replaceWith(a.self.innerHTML);
  112.                                     }
  113.                                 }
  114.                             } else {
  115.                                 if ($.browser.msie) {
  116.                                     Wysiwyg.ui.returnRange();
  117.                                 }
  118.  
  119.                                 //Do new link element
  120.                                 selection = Wysiwyg.getRangeText();
  121.                                 img = Wysiwyg.dom.getElement("img");
  122.  
  123.                                 if ((selection && selection.length > 0) || img) {
  124.                                     if ($.browser.msie) {
  125.                                         Wysiwyg.ui.focus();
  126.                                     }
  127.  
  128.                                     if ("string" === typeof (url)) {
  129.                                         if (url.length > 0) {
  130.                                             Wysiwyg.editorDoc.execCommand("createLink", false, url);
  131.                                         } else {
  132.                                             Wysiwyg.editorDoc.execCommand("unlink", false, null);
  133.                                         }
  134.                                     }
  135.  
  136.                                     a.self = Wysiwyg.dom.getElement("a");
  137.  
  138.                                     $(a.self).attr("href", url).attr("title", title);
  139.  
  140.                                     /**
  141.                                      * @url https://github.com/akzhan/jwysiwyg/issues/16
  142.                                      */
  143.                                     $(a.self).attr("target", target);
  144.                                 } else if (Wysiwyg.options.messages.nonSelection) {
  145.                                     window.alert(Wysiwyg.options.messages.nonSelection);
  146.                                 }
  147.                             }
  148.  
  149.                             Wysiwyg.saveContent();
  150.  
  151.                             $(dialog).dialog("close");
  152.  
  153.                              Wysiwyg.ui.focus();
  154.                              return false;
  155.                         });
  156.                         $("input:reset", dialog).click(function (e) {
  157.                             e.preventDefault();
  158.                             $(dialog).dialog("close");
  159.                             Wysiwyg.ui.focus();
  160.                         });
  161.                     },
  162.                     close: function (ev, ui) {
  163.                         dialog.dialog("destroy");
  164.                         dialog.remove();
  165.                         Wysiwyg.ui.focus();
  166.                     }
  167.                 });
  168.             } else {
  169.                 if (a.self) {
  170.                     url = window.prompt("URL", a.href);
  171.  
  172.                     if (Wysiwyg.options.controlLink.forceRelativeUrls) {
  173.                         baseUrl = window.location.protocol + "//" + window.location.hostname;
  174.                         if (0 === url.indexOf(baseUrl)) {
  175.                             url = url.substr(baseUrl.length);
  176.                         }
  177.                     }
  178.  
  179.                     if ("string" === typeof (url)) {
  180.                         if (url.length > 0) {
  181.                             $(a.self).attr("href", url);
  182.                         } else {
  183.                             $(a.self).replaceWith(a.self.innerHTML);
  184.                         }
  185.                     }
  186.                 } else {
  187.                     //Do new link element
  188.                     selection = Wysiwyg.getRangeText();
  189.                     img = Wysiwyg.dom.getElement("img");
  190.  
  191.                     if ((selection && selection.length > 0) || img) {
  192.                         if ($.browser.msie) {
  193.                             Wysiwyg.ui.focus();
  194.                             Wysiwyg.editorDoc.execCommand("createLink", true, null);
  195.                         } else {
  196.                             url = window.prompt(dialogReplacements.url, a.href);
  197.  
  198.                             if (Wysiwyg.options.controlLink.forceRelativeUrls) {
  199.                                 baseUrl = window.location.protocol + "//" + window.location.hostname;
  200.                                 if (0 === url.indexOf(baseUrl)) {
  201.                                     url = url.substr(baseUrl.length);
  202.                                 }
  203.                             }
  204.  
  205.                             if ("string" === typeof (url)) {
  206.                                 if (url.length > 0) {
  207.                                     Wysiwyg.editorDoc.execCommand("createLink", false, url);
  208.                                 } else {
  209.                                     Wysiwyg.editorDoc.execCommand("unlink", false, null);
  210.                                 }
  211.                             }
  212.                         }
  213.                     } else if (Wysiwyg.options.messages.nonSelection) {
  214.                         window.alert(Wysiwyg.options.messages.nonSelection);
  215.                     }
  216.                 }
  217.  
  218.                 Wysiwyg.saveContent();
  219.             }
  220.  
  221.             $(Wysiwyg.editorDoc).trigger("editorRefresh.wysiwyg");
  222.         }
  223.     };
  224.  
  225.     $.wysiwyg.createLink = function (object, url, title) {
  226.         return object.each(function () {
  227.             var oWysiwyg = $(this).data("wysiwyg"),
  228.                 selection;
  229.  
  230.             if (!oWysiwyg) {
  231.                 return this;
  232.             }
  233.  
  234.             if (!url || url.length === 0) {
  235.                 return this;
  236.             }
  237.  
  238.             selection = oWysiwyg.getRangeText();
  239.             // ability to link selected img - just hack
  240.             var internalRange = oWysiwyg.getInternalRange();
  241.             var isNodeSelected = false;
  242.             if (internalRange && internalRange.extractContents) {
  243.                 var rangeContents = internalRange.cloneContents();
  244.                 if (rangeContents!=null && rangeContents.childNodes && rangeContents.childNodes.length>0)
  245.                     isNodeSelected = true;
  246.             }
  247.  
  248.             if ( (selection && selection.length > 0) || isNodeSelected ) {
  249.                 if ($.browser.msie) {
  250.                     oWysiwyg.ui.focus();
  251.                 }
  252.                 oWysiwyg.editorDoc.execCommand("unlink", false, null);
  253.                 oWysiwyg.editorDoc.execCommand("createLink", false, url);
  254.             } else {
  255.                 if (title) {
  256.                     oWysiwyg.insertHtml('<a href="'+url+'">'+title+'</a>');
  257.                 } else {
  258.                     if (oWysiwyg.options.messages.nonSelection)
  259.                         window.alert(oWysiwyg.options.messages.nonSelection);
  260.                 }
  261.             }
  262.             return this;
  263.         });
  264.     };
  265. })(jQuery);
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement