anchor.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. tinyMCEPopup.requireLangPack();
  2. var AnchorDialog = {
  3. init : function(ed) {
  4. var action, elm, f = document.forms[0];
  5. this.editor = ed;
  6. elm = ed.dom.getParent(ed.selection.getNode(), 'A');
  7. v = ed.dom.getAttrib(elm, 'name') || ed.dom.getAttrib(elm, 'id');
  8. if (v) {
  9. this.action = 'update';
  10. f.anchorName.value = v;
  11. }
  12. f.insert.value = ed.getLang(elm ? 'update' : 'insert');
  13. },
  14. update : function() {
  15. var ed = this.editor, elm, name = document.forms[0].anchorName.value, attribName;
  16. if (!name || !/^[a-z][a-z0-9\-\_:\.]*$/i.test(name)) {
  17. tinyMCEPopup.alert('advanced_dlg.anchor_invalid');
  18. return;
  19. }
  20. tinyMCEPopup.restoreSelection();
  21. if (this.action != 'update')
  22. ed.selection.collapse(1);
  23. var aRule = ed.schema.getElementRule('a');
  24. if (!aRule || aRule.attributes.name) {
  25. attribName = 'name';
  26. } else {
  27. attribName = 'id';
  28. }
  29. elm = ed.dom.getParent(ed.selection.getNode(), 'A');
  30. if (elm) {
  31. elm.setAttribute(attribName, name);
  32. elm[attribName] = name;
  33. ed.undoManager.add();
  34. } else {
  35. // create with zero-sized nbsp so that in Webkit where anchor is on last line by itself caret cannot be placed after it
  36. var attrs = {'class' : 'mceItemAnchor'};
  37. attrs[attribName] = name;
  38. ed.execCommand('mceInsertContent', 0, ed.dom.createHTML('a', attrs, '\uFEFF'));
  39. ed.nodeChanged();
  40. }
  41. tinyMCEPopup.close();
  42. }
  43. };
  44. tinyMCEPopup.onInit.add(AnchorDialog.init, AnchorDialog);