createlink-base.js revision 5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887
266bfbd67fc220029bdadabd3c49e733f9f39360Luke SmithYUI.add('createlink-base', function(Y) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Adds prompt style link creation. Adds an override for the <a href="Plugin.ExecCommand.html#method_COMMANDS.createlink">createlink execCommand</a>.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @module editor
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @submodule createlink-base
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Adds prompt style link creation. Adds an override for the <a href="Plugin.ExecCommand.html#method_COMMANDS.createlink">createlink execCommand</a>.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @class CreateLinkBase
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @namespace Plugin
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith var CreateLinkBase = {};
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Strings used by the plugin
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @property STRINGS
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith CreateLinkBase.STRINGS = {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * String used for the Prompt
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @property PROMPT
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith PROMPT: 'Please enter the URL for the link to point to:',
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * String used as the default value of the Prompt
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @property DEFAULT
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith DEFAULT: 'http://'
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith };
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith Y.namespace('Plugin');
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith Y.Plugin.CreateLinkBase = CreateLinkBase;
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith Y.mix(Y.Plugin.ExecCommand.COMMANDS, {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Override for the createlink method from the <a href="Plugin.CreateLinkBase.html">CreateLinkBase</a> plugin.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @for ExecCommand
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method COMMANDS.createlink
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} cmd The command executed: createlink
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @return {Node} Node instance of the item touched by this command.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith createlink: function(cmd) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith var inst = this.get('host').getInstance(), out, a, sel,
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith url = prompt(CreateLinkBase.STRINGS.PROMPT, CreateLinkBase.STRINGS.DEFAULT);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (url) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith Y.log('Adding link: ' + url, 'info', 'createLinkBase');
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith this.get('host')._execCommand(cmd, url);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith sel = new inst.Selection();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith out = sel.getSelected();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (!sel.isCollapsed && out.size()) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith //We have a selection
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith a = out.item(0).one('a');
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith out.item(0).replace(a);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith } else {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith //No selection, insert a new node..
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith this.get('host').execCommand('inserthtml', '<a href="' + url + '">' + url + '</a>');
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith return a;
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith });
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith});
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith