4 * Copyright 2012, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 /*global tinymce:true */
13 tinymce.PluginManager.add('visualblocks', function(editor, url) {
14 var cssId, visualBlocksMenuItem, enabled;
16 // We don't support older browsers like IE6/7 and they don't provide prototypes for DOM objects
17 if (!window.NodeList) {
21 function toggleActiveState() {
26 editor.on('VisualBlocks', function() {
27 self.active(editor.dom.hasClass(editor.getBody(), 'mce-visualblocks'));
31 editor.addCommand('mceVisualBlocks', function() {
32 var dom = editor.dom, linkElm;
35 cssId = dom.uniqueId();
36 linkElm = dom.create('link', {
39 href: url + '/css/visualblocks.css'
42 editor.getDoc().getElementsByTagName('head')[0].appendChild(linkElm);
45 // Toggle on/off visual blocks while computing previews
46 editor.on("PreviewFormats AfterPreviewFormats", function(e) {
48 dom.toggleClass(editor.getBody(), 'mce-visualblocks', e.type == "afterpreviewformats");
52 dom.toggleClass(editor.getBody(), 'mce-visualblocks');
53 enabled = editor.dom.hasClass(editor.getBody(), 'mce-visualblocks');
55 if (visualBlocksMenuItem) {
56 visualBlocksMenuItem.active(dom.hasClass(editor.getBody(), 'mce-visualblocks'));
59 editor.fire('VisualBlocks');
62 editor.addButton('visualblocks', {
64 cmd: 'mceVisualBlocks',
65 onPostRender: toggleActiveState
68 editor.addMenuItem('visualblocks', {
70 cmd: 'mceVisualBlocks',
71 onPostRender: toggleActiveState,
74 prependToContext: true
77 editor.on('init', function() {
78 if (editor.settings.visualblocks_default_state) {
79 editor.execCommand('mceVisualBlocks', false, null, {skip_focus: true});
83 editor.on('remove', function() {
84 editor.dom.removeClass(editor.getBody(), 'mce-visualblocks');