PageRenderTime 38ms CodeModel.GetById 15ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/lib/js/modules/tinymce3.4.3.2/plugins/advlist/editor_plugin_src.js

http://github.com/zotonic/zotonic
JavaScript | 176 lines | 120 code | 36 blank | 20 comment | 36 complexity | 82a21dbd1ddc6f9c5e9bb3e3c93bdd31 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
 11(function() {
 12	var each = tinymce.each;
 13
 14	tinymce.create('tinymce.plugins.AdvListPlugin', {
 15		init : function(ed, url) {
 16			var t = this;
 17
 18			t.editor = ed;
 19
 20			function buildFormats(str) {
 21				var formats = [];
 22
 23				each(str.split(/,/), function(type) {
 24					formats.push({
 25						title : 'advlist.' + (type == 'default' ? 'def' : type.replace(/-/g, '_')),
 26						styles : {
 27							listStyleType : type == 'default' ? '' : type
 28						}
 29					});
 30				});
 31
 32				return formats;
 33			};
 34
 35			// Setup number formats from config or default
 36			t.numlist = ed.getParam("advlist_number_styles") || buildFormats("default,lower-alpha,lower-greek,lower-roman,upper-alpha,upper-roman");
 37			t.bullist = ed.getParam("advlist_bullet_styles") || buildFormats("default,circle,disc,square");
 38
 39			if (tinymce.isIE && /MSIE [2-7]/.test(navigator.userAgent))
 40				t.isIE7 = true;
 41		},
 42
 43		createControl: function(name, cm) {
 44			var t = this, btn, format, editor = t.editor;
 45
 46			if (name == 'numlist' || name == 'bullist') {
 47				// Default to first item if it's a default item
 48				if (t[name][0].title == 'advlist.def')
 49					format = t[name][0];
 50
 51				function hasFormat(node, format) {
 52					var state = true;
 53
 54					each(format.styles, function(value, name) {
 55						// Format doesn't match
 56						if (editor.dom.getStyle(node, name) != value) {
 57							state = false;
 58							return false;
 59						}
 60					});
 61
 62					return state;
 63				};
 64
 65				function applyListFormat() {
 66					var list, dom = editor.dom, sel = editor.selection;
 67
 68					// Check for existing list element
 69					list = dom.getParent(sel.getNode(), 'ol,ul');
 70
 71					// Switch/add list type if needed
 72					if (!list || list.nodeName == (name == 'bullist' ? 'OL' : 'UL') || hasFormat(list, format))
 73						editor.execCommand(name == 'bullist' ? 'InsertUnorderedList' : 'InsertOrderedList');
 74
 75					// Append styles to new list element
 76					if (format) {
 77						list = dom.getParent(sel.getNode(), 'ol,ul');
 78						if (list) {
 79							dom.setStyles(list, format.styles);
 80							list.removeAttribute('data-mce-style');
 81						}
 82					}
 83
 84					editor.focus();
 85				};
 86
 87				btn = cm.createSplitButton(name, {
 88					title : 'advanced.' + name + '_desc',
 89					'class' : 'mce_' + name,
 90					onclick : function() {
 91						applyListFormat();
 92					}
 93				});
 94
 95				btn.onRenderMenu.add(function(btn, menu) {
 96					menu.onHideMenu.add(function() {
 97						if (t.bookmark) {
 98							editor.selection.moveToBookmark(t.bookmark);
 99							t.bookmark = 0;
100						}
101					});
102
103					menu.onShowMenu.add(function() {
104						var dom = editor.dom, list = dom.getParent(editor.selection.getNode(), 'ol,ul'), fmtList;
105
106						if (list || format) {
107							fmtList = t[name];
108
109							// Unselect existing items
110							each(menu.items, function(item) {
111								var state = true;
112
113								item.setSelected(0);
114
115								if (list && !item.isDisabled()) {
116									each(fmtList, function(fmt) {
117										if (fmt.id == item.id) {
118											if (!hasFormat(list, fmt)) {
119												state = false;
120												return false;
121											}
122										}
123									});
124
125									if (state)
126										item.setSelected(1);
127								}
128							});
129
130							// Select the current format
131							if (!list)
132								menu.items[format.id].setSelected(1);
133						}
134	
135						editor.focus();
136
137						// IE looses it's selection so store it away and restore it later
138						if (tinymce.isIE) {
139							t.bookmark = editor.selection.getBookmark(1);
140						}
141					});
142
143					menu.add({id : editor.dom.uniqueId(), title : 'advlist.types', 'class' : 'mceMenuItemTitle', titleItem: true}).setDisabled(1);
144
145					each(t[name], function(item) {
146						// IE<8 doesn't support lower-greek, skip it
147						if (t.isIE7 && item.styles.listStyleType == 'lower-greek')
148							return;
149
150						item.id = editor.dom.uniqueId();
151
152						menu.add({id : item.id, title : item.title, onclick : function() {
153							format = item;
154							applyListFormat();
155						}});
156					});
157				});
158
159				return btn;
160			}
161		},
162
163		getInfo : function() {
164			return {
165				longname : 'Advanced lists',
166				author : 'Moxiecode Systems AB',
167				authorurl : 'http://tinymce.moxiecode.com',
168				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlist',
169				version : tinymce.majorVersion + "." + tinymce.minorVersion
170			};
171		}
172	});
173
174	// Register plugin
175	tinymce.PluginManager.add('advlist', tinymce.plugins.AdvListPlugin);
176})();