API Docs for: 3.10.3
Show:

File: editor/js/createlink-base.js

  1.  
  2. /**
  3. * Adds prompt style link creation. Adds an override for the
  4. * <a href="Plugin.ExecCommand.html#method_COMMANDS.createlink">createlink execCommand</a>.
  5. * @class Plugin.CreateLinkBase
  6. * @static
  7. * @submodule createlink-base
  8. * @module editor
  9. */
  10.  
  11. var CreateLinkBase = {};
  12. /**
  13. * Strings used by the plugin
  14. * @property STRINGS
  15. * @static
  16. */
  17. CreateLinkBase.STRINGS = {
  18. /**
  19. * String used for the Prompt
  20. * @property PROMPT
  21. * @static
  22. */
  23. PROMPT: 'Please enter the URL for the link to point to:',
  24. /**
  25. * String used as the default value of the Prompt
  26. * @property DEFAULT
  27. * @static
  28. */
  29. DEFAULT: 'http://'
  30. };
  31.  
  32. Y.namespace('Plugin');
  33. Y.Plugin.CreateLinkBase = CreateLinkBase;
  34.  
  35. Y.mix(Y.Plugin.ExecCommand.COMMANDS, {
  36. /**
  37. * Override for the createlink method from the <a href="Plugin.CreateLinkBase.html">CreateLinkBase</a> plugin.
  38. * @for ExecCommand
  39. * @method COMMANDS.createlink
  40. * @static
  41. * @param {String} cmd The command executed: createlink
  42. * @return {Node} Node instance of the item touched by this command.
  43. */
  44. createlink: function(cmd) {
  45. var inst = this.get('host').getInstance(), out, a, sel, holder,
  46. url = prompt(CreateLinkBase.STRINGS.PROMPT, CreateLinkBase.STRINGS.DEFAULT);
  47.  
  48. if (url) {
  49. holder = inst.config.doc.createElement('div');
  50. url = url.replace(/"/g, '').replace(/'/g, ''); //Remove single & double quotes
  51. url = inst.config.doc.createTextNode(url);
  52. holder.appendChild(url);
  53. url = holder.innerHTML;
  54.  
  55. Y.log('Adding link: ' + url, 'info', 'createLinkBase');
  56.  
  57. this.get('host')._execCommand(cmd, url);
  58. sel = new inst.EditorSelection();
  59. out = sel.getSelected();
  60. if (!sel.isCollapsed && out.size()) {
  61. //We have a selection
  62. a = out.item(0).one('a');
  63. if (a) {
  64. out.item(0).replace(a);
  65. }
  66. if (Y.UA.gecko) {
  67. if (a.get('parentNode').test('span')) {
  68. if (a.get('parentNode').one('br.yui-cursor')) {
  69. a.get('parentNode').insert(a, 'before');
  70. }
  71. }
  72. }
  73. } else {
  74. //No selection, insert a new node..
  75. this.get('host').execCommand('inserthtml', '<a href="' + url + '">' + url + '</a>');
  76. }
  77. }
  78. return a;
  79. }
  80. });
  81.  
  82.