applied my changes - initial import
[boxroom-stian.git] / public / javascripts / tiny_mce / plugins / noneditable / editor_plugin_src.js
blobe484bc02933d419226ce510d1b857955cebd2483
1 /**\r
2  * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $\r
3  *\r
4  * @author Moxiecode\r
5  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.\r
6  */\r
7 \r
8 var TinyMCE_NonEditablePlugin = {\r
9         getInfo : function() {\r
10                 return {\r
11                         longname : 'Non editable elements',\r
12                         author : 'Moxiecode Systems AB',\r
13                         authorurl : 'http://tinymce.moxiecode.com',\r
14                         infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_noneditable.html',\r
15                         version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion\r
16                 };\r
17         },\r
19         initInstance : function(inst) {\r
20                 tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/noneditable/css/noneditable.css");\r
22                 // Ugly hack\r
23                 if (tinyMCE.isMSIE5_0)\r
24                         tinyMCE.settings['plugins'] = tinyMCE.settings['plugins'].replace(/noneditable/gi, 'Noneditable');\r
25         },\r
27         handleEvent : function(e) {\r
28                 return this._moveSelection(e, tinyMCE.selectedInstance);\r
29         },\r
31         cleanup : function(type, content, inst) {\r
32                 // Pass through Gecko\r
33                 if (tinyMCE.isGecko)\r
34                         return content;\r
36                 switch (type) {\r
37                         case "insert_to_editor_dom":\r
38                                 var nodes = tinyMCE.getNodeTree(content, new Array(), 1);\r
39                                 var editClass = tinyMCE.getParam("noneditable_editable_class", "mceItemEditable");\r
40                                 var nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceItemNonEditable");\r
42                                 for (var i=0; i<nodes.length; i++) {\r
43                                         var elm = nodes[i];\r
45                                         // Convert contenteditable to classes\r
46                                         var editable = tinyMCE.getAttrib(elm, "contenteditable");\r
47                                         if (new RegExp("true|false","gi").test(editable))\r
48                                                 TinyMCE_NonEditablePlugin._setEditable(elm, editable == "true");\r
50                                         if (tinyMCE.isMSIE) {\r
51                                                 var className = elm.className ? elm.className : "";\r
53                                                 if (className.indexOf(editClass) != -1)\r
54                                                         elm.contentEditable = true;\r
56                                                 if (className.indexOf(nonEditClass) != -1)\r
57                                                         elm.contentEditable = false;\r
58                                         }\r
59                                 }\r
61                                 break;\r
63                         case "insert_to_editor":\r
64                                 if (tinyMCE.isMSIE) {\r
65                                         var editClass = tinyMCE.getParam("noneditable_editable_class", "mceItemEditable");\r
66                                         var nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceItemNonEditable");\r
68                                         content = content.replace(new RegExp("class=\"(.*)(" + editClass + ")([^\"]*)\"", "gi"), 'class="$1$2$3" contenteditable="true"');\r
69                                         content = content.replace(new RegExp("class=\"(.*)(" + nonEditClass + ")([^\"]*)\"", "gi"), 'class="$1$2$3" contenteditable="false"');\r
70                                 }\r
72                                 break;\r
74                         case "get_from_editor_dom":\r
75                                 if (tinyMCE.getParam("noneditable_leave_contenteditable", false)) {\r
76                                         var nodes = tinyMCE.getNodeTree(content, new Array(), 1);\r
78                                         for (var i=0; i<nodes.length; i++)\r
79                                                 nodes[i].removeAttribute("contenteditable");\r
80                                 }\r
82                                 break;\r
83                 }\r
85                 return content;\r
86         },\r
88         _moveSelection : function(e, inst) {\r
89                 var s, r, sc, ec, el, c = tinyMCE.getParam('noneditable_editable_class', 'mceItemNonEditable');\r
91                 if (!inst)\r
92                         return true;\r
94                 // Always select whole element\r
95                 if (tinyMCE.isGecko) {\r
96                         s = inst.selection.getSel();\r
97                         r = s.getRangeAt(0);\r
98                         sc = tinyMCE.getParentNode(r.startContainer, function (n) {return tinyMCE.hasCSSClass(n, c);});\r
99                         ec = tinyMCE.getParentNode(r.endContainer, function (n) {return tinyMCE.hasCSSClass(n, c);});\r
101                         sc && r.setStartBefore(sc);\r
102                         ec && r.setEndAfter(ec);\r
104                         if (sc || ec) {\r
105                                 if (e.type == 'keypress' && e.keyCode == 39) {\r
106                                         el = sc || ec;\r
108                                         // Try!!\r
109                                 }\r
111                                 s.removeAllRanges();\r
112                                 s.addRange(r);\r
114                                 return tinyMCE.cancelEvent(e);\r
115                         }\r
116                 }\r
118                 return true;\r
119         },\r
121         _setEditable : function(elm, state) {\r
122                 var editClass = tinyMCE.getParam("noneditable_editable_class", "mceItemEditable");\r
123                 var nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceItemNonEditable");\r
125                 var className = elm.className ? elm.className : "";\r
127                 if (className.indexOf(editClass) != -1 || className.indexOf(nonEditClass) != -1)\r
128                         return;\r
130                 if ((className = tinyMCE.getAttrib(elm, "class")) != "")\r
131                         className += " ";\r
133                 className += state ? editClass : nonEditClass;\r
135                 elm.setAttribute("class", className);\r
136                 elm.className = className;\r
137         }\r
138 };\r
140 tinyMCE.addPlugin("noneditable", TinyMCE_NonEditablePlugin);\r