/pigeoncms/Plugins/tiny_mce/plugins/advlist/editor_plugin_src.js

http://pigeoncms.googlecode.com/ · JavaScript · 154 lines · 105 code · 31 blank · 18 comment · 29 complexity · f6527ec1e7b0c8a9019de139e7650a2e MD5 · raw file

  1. /**
  2. * editor_plugin_src.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function() {
  11. var each = tinymce.each;
  12. tinymce.create('tinymce.plugins.AdvListPlugin', {
  13. init : function(ed, url) {
  14. var t = this;
  15. t.editor = ed;
  16. function buildFormats(str) {
  17. var formats = [];
  18. each(str.split(/,/), function(type) {
  19. formats.push({
  20. title : 'advlist.' + (type == 'default' ? 'def' : type.replace(/-/g, '_')),
  21. styles : {
  22. listStyleType : type == 'default' ? '' : type
  23. }
  24. });
  25. });
  26. return formats;
  27. };
  28. // Setup number formats from config or default
  29. t.numlist = ed.getParam("advlist_number_styles") || buildFormats("default,lower-alpha,lower-greek,lower-roman,upper-alpha,upper-roman");
  30. t.bullist = ed.getParam("advlist_bullet_styles") || buildFormats("default,circle,disc,square");
  31. },
  32. createControl: function(name, cm) {
  33. var t = this, btn, format;
  34. if (name == 'numlist' || name == 'bullist') {
  35. // Default to first item if it's a default item
  36. if (t[name][0].title == 'advlist.def')
  37. format = t[name][0];
  38. function hasFormat(node, format) {
  39. var state = true;
  40. each(format.styles, function(value, name) {
  41. // Format doesn't match
  42. if (t.editor.dom.getStyle(node, name) != value) {
  43. state = false;
  44. return false;
  45. }
  46. });
  47. return state;
  48. };
  49. function applyListFormat() {
  50. var list, ed = t.editor, dom = ed.dom, sel = ed.selection;
  51. // Check for existing list element
  52. list = dom.getParent(sel.getNode(), 'ol,ul');
  53. // Switch/add list type if needed
  54. if (!list || list.nodeName == (name == 'bullist' ? 'OL' : 'UL') || hasFormat(list, format))
  55. ed.execCommand(name == 'bullist' ? 'InsertUnorderedList' : 'InsertOrderedList');
  56. // Append styles to new list element
  57. if (format) {
  58. list = dom.getParent(sel.getNode(), 'ol,ul');
  59. if (list) {
  60. dom.setStyles(list, format.styles);
  61. list.removeAttribute('_mce_style');
  62. }
  63. }
  64. };
  65. btn = cm.createSplitButton(name, {
  66. title : 'advanced.' + name + '_desc',
  67. 'class' : 'mce_' + name,
  68. onclick : function() {
  69. applyListFormat();
  70. }
  71. });
  72. btn.onRenderMenu.add(function(btn, menu) {
  73. menu.onShowMenu.add(function() {
  74. var dom = t.editor.dom, list = dom.getParent(t.editor.selection.getNode(), 'ol,ul'), fmtList;
  75. if (list || format) {
  76. fmtList = t[name];
  77. // Unselect existing items
  78. each(menu.items, function(item) {
  79. var state = true;
  80. item.setSelected(0);
  81. if (list && !item.isDisabled()) {
  82. each(fmtList, function(fmt) {
  83. if (fmt.id == item.id) {
  84. if (!hasFormat(list, fmt)) {
  85. state = false;
  86. return false;
  87. }
  88. }
  89. });
  90. if (state)
  91. item.setSelected(1);
  92. }
  93. });
  94. // Select the current format
  95. if (!list)
  96. menu.items[format.id].setSelected(1);
  97. }
  98. });
  99. menu.add({id : t.editor.dom.uniqueId(), title : 'advlist.types', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
  100. each(t[name], function(item) {
  101. item.id = t.editor.dom.uniqueId();
  102. menu.add({id : item.id, title : item.title, onclick : function() {
  103. format = item;
  104. applyListFormat();
  105. }});
  106. });
  107. });
  108. return btn;
  109. }
  110. },
  111. getInfo : function() {
  112. return {
  113. longname : 'Advanced lists',
  114. author : 'Moxiecode Systems AB',
  115. authorurl : 'http://tinymce.moxiecode.com',
  116. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlist',
  117. version : tinymce.majorVersion + "." + tinymce.minorVersion
  118. };
  119. }
  120. });
  121. // Register plugin
  122. tinymce.PluginManager.add('advlist', tinymce.plugins.AdvListPlugin);
  123. })();