applied my changes - initial import
[boxroom-stian.git] / public / javascripts / tiny_mce / plugins / codeblock / editor_plugin_src.js
blob65b4b777ac356fb8ecf4890748c43cd46755e91d
1 /**
2  * Codeblock plugin
3  *
4  * Some code is based on the Heading plugin by Andrey G and ggoodd.
5  * Base code stolen from Bit Santos blockquote plugin.
6  *
7  * @author Blake Watters
8  * @copyright Copyright © 2006, Blake Watters, All rights reserved.
9  */
11 tinyMCE.importPluginLanguagePack('codeblock', 'en');
12 var TinyMCE_CodeblockPlugin = {
14     getInfo : function() {
15         return {
16             longname :  'Codeblock plugin',
17             author :    'Blake Watters',
18             authorurl : 'http://thatswhatimtalkingabout.org/',
19             infourl :   'mailto:blake@near-time.com',
20             version :   '0.1'
21         };
22     },
24     initInstance : function(inst) {
25         inst.addShortcut('alt', '|', 'lang_codeblock_desc', 'mceCodeblock', false, 1);
26     },
28     getControlHTML : function(cn) {
29         switch (cn) {
30             case "codeblock": return tinyMCE.getButtonHTML(cn, 'lang_codeblock_desc', '{$pluginurl}/images/code.gif', 'mceCodeblock', false, 1);
31         }
33         return '';
34     },
37     execCommand : function(editor_id, element, command, user_interface, value) {
38         switch (command) {
39             case "mceCodeblock": {
40                                                                 //debugger;
41                 var currentNode = tinyMCE.selectedElement;
43                 // alert("Selected node: " + currentNode.nodeName.toLowerCase());
44                 
45                 var n = currentNode;
46                 while(n.nodeName.toLowerCase() != 'body') {
47                     if(n.nodeName.toLowerCase() == 'pre') {
48                         break;
49                     }
50                     n = n.parentNode;
51                 }
52                 
53                 if(n.nodeName.toLowerCase() != 'body') {
54                     if(currentNode == n) {
55                         //alert("I am a codeblock.");
56                         onlyChild = (currentNode.childNodes.length == 1);
57                     
58                         tinyMCE.execInstanceCommand(editor_id, 'mceRemoveNode', false);
59                         
60                         if(onlyChild){
61                                                                                                                 tinyMCE.execInstanceCommand(editor_id, 'FormatBlock', false, "<pre>");
62                                                                                                                 tinyMCE.execInstanceCommand(editor_id, 'FormatBlock', false, "<code>");
63                                                                                                 }
64                     }else {
65                         //alert("My parent is a codeblock.");
66                     
67                         var codeblock = n;
68                         var parent = codeblock.parentNode;
70                         for(var i = 0; i < codeblock.childNodes.length; i++) {
71                             // alert("Child #" + i + ": " + codeblock.childNodes[i].nodeName);
72                             parent.insertBefore(codeblock.childNodes[i].cloneNode(true), codeblock);
73                         }
74                         parent.removeChild(codeblock);
75                     }
76                 }else {
77                     tinyMCE.execInstanceCommand(editor_id, 'FormatBlock', false, "<code>");
78                                                                                 tinyMCE.execInstanceCommand(editor_id, 'FormatBlock', false, "<pre>");
79                 }
81                 return true;
82             }
83         }
84         return false;
85     },
88     handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
89         if (node == null)
90             return;
92         tinyMCE.switchClass(editor_id + '_codeblock', tinyMCE.getParentElement(node, "pre") ? 'mceButtonSelected' : 'mceButtonNormal');
94         return true;
95     }
98 tinyMCE.addPlugin("codeblock", TinyMCE_CodeblockPlugin);