PageRenderTime 38ms CodeModel.GetById 9ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/tinymce-advanced/mce/anchor/plugin.js

https://bitbucket.org/carloskikea/helpet
JavaScript | 117 lines | 108 code | 9 blank | 0 comment | 13 complexity | 1ef8c868dafafc3baa2e20f1be538ad6 MD5 | raw file
  1(function () {
  2var anchor = (function () {
  3  'use strict';
  4
  5  var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  6
  7  var isValidId = function (id) {
  8    return /^[A-Za-z][A-Za-z0-9\-:._]*$/.test(id);
  9  };
 10  var getId = function (editor) {
 11    var selectedNode = editor.selection.getNode();
 12    var isAnchor = selectedNode.tagName === 'A' && editor.dom.getAttrib(selectedNode, 'href') === '';
 13    return isAnchor ? selectedNode.id || selectedNode.name : '';
 14  };
 15  var insert = function (editor, id) {
 16    var selectedNode = editor.selection.getNode();
 17    var isAnchor = selectedNode.tagName === 'A' && editor.dom.getAttrib(selectedNode, 'href') === '';
 18    if (isAnchor) {
 19      selectedNode.removeAttribute('name');
 20      selectedNode.id = id;
 21    } else {
 22      editor.focus();
 23      editor.selection.collapse(true);
 24      editor.execCommand('mceInsertContent', false, editor.dom.createHTML('a', { id: id }));
 25    }
 26  };
 27  var $_4ofr6484jfuviwhx = {
 28    isValidId: isValidId,
 29    getId: getId,
 30    insert: insert
 31  };
 32
 33  var insertAnchor = function (editor, newId) {
 34    if (!$_4ofr6484jfuviwhx.isValidId(newId)) {
 35      editor.windowManager.alert('Id should start with a letter, followed only by letters, numbers, dashes, dots, colons or underscores.');
 36      return true;
 37    } else {
 38      $_4ofr6484jfuviwhx.insert(editor, newId);
 39      return false;
 40    }
 41  };
 42  var open = function (editor) {
 43    var currentId = $_4ofr6484jfuviwhx.getId(editor);
 44    editor.windowManager.open({
 45      title: 'Anchor',
 46      body: {
 47        type: 'textbox',
 48        name: 'id',
 49        size: 40,
 50        label: 'Id',
 51        value: currentId
 52      },
 53      onsubmit: function (e) {
 54        var newId = e.data.id;
 55        if (insertAnchor(editor, newId)) {
 56          e.preventDefault();
 57        }
 58      }
 59    });
 60  };
 61  var $_5ybsb183jfuviwhw = { open: open };
 62
 63  var register = function (editor) {
 64    editor.addCommand('mceAnchor', function () {
 65      $_5ybsb183jfuviwhw.open(editor);
 66    });
 67  };
 68  var $_bu68pd82jfuviwhv = { register: register };
 69
 70  var isAnchorNode = function (node) {
 71    return !node.attr('href') && (node.attr('id') || node.attr('name')) && !node.firstChild;
 72  };
 73  var setContentEditable = function (state) {
 74    return function (nodes) {
 75      for (var i = 0; i < nodes.length; i++) {
 76        if (isAnchorNode(nodes[i])) {
 77          nodes[i].attr('contenteditable', state);
 78        }
 79      }
 80    };
 81  };
 82  var setup = function (editor) {
 83    editor.on('PreInit', function () {
 84      editor.parser.addNodeFilter('a', setContentEditable('false'));
 85      editor.serializer.addNodeFilter('a', setContentEditable(null));
 86    });
 87  };
 88  var $_7q0hk685jfuviwhy = { setup: setup };
 89
 90  var register$1 = function (editor) {
 91    editor.addButton('anchor', {
 92      icon: 'anchor',
 93      tooltip: 'Anchor',
 94      cmd: 'mceAnchor',
 95      stateSelector: 'a:not([href])'
 96    });
 97    editor.addMenuItem('anchor', {
 98      icon: 'anchor',
 99      text: 'Anchor',
100      context: 'insert',
101      cmd: 'mceAnchor'
102    });
103  };
104  var $_5nc4wj86jfuviwi0 = { register: register$1 };
105
106  global.add('anchor', function (editor) {
107    $_7q0hk685jfuviwhy.setup(editor);
108    $_bu68pd82jfuviwhv.register(editor);
109    $_5nc4wj86jfuviwi0.register(editor);
110  });
111  function Plugin () {
112  }
113
114  return Plugin;
115
116}());
117})();