PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/editor/tinymce/tiny_mce/3.4.6/plugins/advlist/editor_plugin_src.js

http://github.com/moodle/moodle
JavaScript | 176 lines | 120 code | 36 blank | 20 comment | 36 complexity | 092569e368c90e1fec73ae8fcefb7482 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. if (tinymce.isIE && /MSIE [2-7]/.test(navigator.userAgent))
  32. t.isIE7 = true;
  33. },
  34. createControl: function(name, cm) {
  35. var t = this, btn, format, editor = t.editor;
  36. if (name == 'numlist' || name == 'bullist') {
  37. // Default to first item if it's a default item
  38. if (t[name][0].title == 'advlist.def')
  39. format = t[name][0];
  40. function hasFormat(node, format) {
  41. var state = true;
  42. each(format.styles, function(value, name) {
  43. // Format doesn't match
  44. if (editor.dom.getStyle(node, name) != value) {
  45. state = false;
  46. return false;
  47. }
  48. });
  49. return state;
  50. };
  51. function applyListFormat() {
  52. var list, dom = editor.dom, sel = editor.selection;
  53. // Check for existing list element
  54. list = dom.getParent(sel.getNode(), 'ol,ul');
  55. // Switch/add list type if needed
  56. if (!list || list.nodeName == (name == 'bullist' ? 'OL' : 'UL') || hasFormat(list, format))
  57. editor.execCommand(name == 'bullist' ? 'InsertUnorderedList' : 'InsertOrderedList');
  58. // Append styles to new list element
  59. if (format) {
  60. list = dom.getParent(sel.getNode(), 'ol,ul');
  61. if (list) {
  62. dom.setStyles(list, format.styles);
  63. list.removeAttribute('data-mce-style');
  64. }
  65. }
  66. editor.focus();
  67. };
  68. btn = cm.createSplitButton(name, {
  69. title : 'advanced.' + name + '_desc',
  70. 'class' : 'mce_' + name,
  71. onclick : function() {
  72. applyListFormat();
  73. }
  74. });
  75. btn.onRenderMenu.add(function(btn, menu) {
  76. menu.onHideMenu.add(function() {
  77. if (t.bookmark) {
  78. editor.selection.moveToBookmark(t.bookmark);
  79. t.bookmark = 0;
  80. }
  81. });
  82. menu.onShowMenu.add(function() {
  83. var dom = editor.dom, list = dom.getParent(editor.selection.getNode(), 'ol,ul'), fmtList;
  84. if (list || format) {
  85. fmtList = t[name];
  86. // Unselect existing items
  87. each(menu.items, function(item) {
  88. var state = true;
  89. item.setSelected(0);
  90. if (list && !item.isDisabled()) {
  91. each(fmtList, function(fmt) {
  92. if (fmt.id == item.id) {
  93. if (!hasFormat(list, fmt)) {
  94. state = false;
  95. return false;
  96. }
  97. }
  98. });
  99. if (state)
  100. item.setSelected(1);
  101. }
  102. });
  103. // Select the current format
  104. if (!list)
  105. menu.items[format.id].setSelected(1);
  106. }
  107. editor.focus();
  108. // IE looses it's selection so store it away and restore it later
  109. if (tinymce.isIE) {
  110. t.bookmark = editor.selection.getBookmark(1);
  111. }
  112. });
  113. menu.add({id : editor.dom.uniqueId(), title : 'advlist.types', 'class' : 'mceMenuItemTitle', titleItem: true}).setDisabled(1);
  114. each(t[name], function(item) {
  115. // IE<8 doesn't support lower-greek, skip it
  116. if (t.isIE7 && item.styles.listStyleType == 'lower-greek')
  117. return;
  118. item.id = editor.dom.uniqueId();
  119. menu.add({id : item.id, title : item.title, onclick : function() {
  120. format = item;
  121. applyListFormat();
  122. }});
  123. });
  124. });
  125. return btn;
  126. }
  127. },
  128. getInfo : function() {
  129. return {
  130. longname : 'Advanced lists',
  131. author : 'Moxiecode Systems AB',
  132. authorurl : 'http://tinymce.moxiecode.com',
  133. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlist',
  134. version : tinymce.majorVersion + "." + tinymce.minorVersion
  135. };
  136. }
  137. });
  138. // Register plugin
  139. tinymce.PluginManager.add('advlist', tinymce.plugins.AdvListPlugin);
  140. })();