* Removed the Kevin Roth rich text editor and replaced it with TinyMCE.
[citadel.git] / webcit / tiny_mce / plugins / searchreplace / editor_plugin_src.js
1 /* Import theme specific language pack */
2 tinyMCE.importPluginLanguagePack('searchreplace', 'en,sv,zh_cn,fa,fr_ca,fr,de,pl,pt_br,cs,nl');
3
4 function TinyMCE_searchreplace_getInfo() {
5         return {
6                 longname : 'Search/Replace',
7                 author : 'Moxiecode Systems',
8                 authorurl : 'http://tinymce.moxiecode.com',
9                 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_searchreplace.html',
10                 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
11         };
12 };
13
14 function TinyMCE_searchreplace_getControlHTML(control_name)     {
15         switch (control_name) {
16                 case "search":
17                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceSearch\',true);" onmousedown="return false;"><img id="{$editor_id}_search" src="{$pluginurl}/images/search.gif" title="{$lang_searchreplace_search_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';
18
19                 case "replace":
20                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'mceSearchReplace\',true);" onmousedown="return false;"><img id="{$editor_id}_replace" src="{$pluginurl}/images/replace.gif" title="{$lang_searchreplace_replace_desc}" width="20" height="20" class="mceButtonNormal" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';
21         }
22
23         return "";
24 }
25
26 /**
27  * Executes     the     search/replace commands.
28  */
29 function TinyMCE_searchreplace_execCommand(editor_id, element, command, user_interface, value) {
30         function defValue(key, default_value) {
31                 value[key] = typeof(value[key]) == "undefined" ? default_value : value[key];
32         }
33
34         function replaceSel(search_str, str) {
35                 // Get current selection
36                 if (!tinyMCE.isMSIE) {
37                         var sel = instance.contentWindow.getSelection();
38                         var rng = sel.getRangeAt(0);
39                 } else {
40                         var rng = instance.contentWindow.document.selection.createRange();
41                 }
42
43                 // Replace current one
44                 if (!tinyMCE.isMSIE) {
45                         var doc = instance.contentWindow.document;
46
47                         // This way works when the replace doesn't contain the search string
48                         if (str.indexOf(search_str) == -1) {
49                                 rng.deleteContents();
50                                 rng.insertNode(rng.createContextualFragment(str));
51                                 rng.collapse(false);
52                         } else {
53                                 // Insert content ugly way! Needed to move selection to after replace item
54                                 doc.execCommand("insertimage", false, "#mce_temp_url#");
55                                 var elm = tinyMCE.getElementByAttributeValue(doc.body, "img", "src", "#mce_temp_url#");
56                                 elm.parentNode.replaceChild(doc.createTextNode(str), elm);
57                         }
58                 } else {
59                         if (rng.item)
60                                 rng.item(0).outerHTML = str;
61                         else
62                                 rng.pasteHTML(str);
63                 }
64         }
65
66         var instance = tinyMCE.getInstanceById(editor_id);
67
68         if (!value)
69                 value = new Array();
70
71         // Setup defualt values
72         defValue("editor_id", editor_id);
73         defValue("searchstring", "");
74         defValue("replacestring", null);
75         defValue("replacemode", "none");
76         defValue("casesensitive", false);
77         defValue("backwards", false);
78         defValue("wrap", false);
79         defValue("wholeword", false);
80         defValue("inline", "yes");
81
82         // Handle commands
83         switch (command) {
84                 case "mceResetSearch":
85                         tinyMCE.lastSearchRng = null;
86                         return true;
87
88                 case "mceSearch":
89                         if (user_interface) {
90                                 // Open search dialog
91                                 var template = new Array();
92
93                                 if (value['replacestring'] != null) {
94                                         template['file'] = '../../plugins/searchreplace/replace.htm'; // Relative to theme
95                                         template['width'] = 320;
96                                         template['height'] = 120;
97                                 } else {
98                                         template['file'] = '../../plugins/searchreplace/search.htm'; // Relative to theme
99                                         template['width'] = 310;
100                                         template['height'] = 105;
101                                 }
102
103                                 tinyMCE.openWindow(template, value);
104                         } else {
105                                 var win = tinyMCE.getInstanceById(editor_id).contentWindow;
106                                 var doc = tinyMCE.getInstanceById(editor_id).contentWindow.document;
107                                 var body = tinyMCE.getInstanceById(editor_id).contentWindow.document.body;
108
109                                 // Whats the point
110                                 if (body.innerHTML == "") {
111                                         alert(tinyMCE.getLang('lang_searchreplace_notfound'));
112                                         return true;
113                                 }
114
115                                 // Handle replace current
116                                 if (value['replacemode'] == "current") {
117                                         replaceSel(value['string'], value['replacestring']);
118
119                                         // Search next one
120                                         value['replacemode'] = "none";
121                                         tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value, false);
122
123                                         return true;
124                                 }
125
126                                 if (tinyMCE.isMSIE) {
127                                         var rng = tinyMCE.lastSearchRng ? tinyMCE.lastSearchRng : doc.selection.createRange();
128                                         var flags = 0;
129
130                                         if (value['wholeword'])
131                                                 flags = flags | 2;
132
133                                         if (value['casesensitive'])
134                                                 flags = flags | 4;
135
136                                         // Handle replace all mode
137                                         if (value['replacemode'] == "all") {
138                                                 while (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) {
139                                                         rng.scrollIntoView();
140                                                         rng.select();
141                                                         rng.collapse(false);
142                                                         replaceSel(value['string'], value['replacestring']);
143                                                 }
144
145                                                 alert(tinyMCE.getLang('lang_searchreplace_allreplaced'));
146                                                 return true;
147                                         }
148
149                                         if (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) {
150                                                 rng.scrollIntoView();
151                                                 rng.select();
152                                                 rng.collapse(value['backwards']);
153                                                 tinyMCE.lastSearchRng = rng;
154                                         } else
155                                                 alert(tinyMCE.getLang('lang_searchreplace_notfound'));
156                                 } else {
157                                         if (value['replacemode'] == "all") {
158                                                 while (win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false))
159                                                         replaceSel(value['string'], value['replacestring']);
160
161                                                 alert(tinyMCE.getLang('lang_searchreplace_allreplaced'));
162                                                 return true;
163                                         }
164
165                                         if (!win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false))
166                                                 alert(tinyMCE.getLang('lang_searchreplace_notfound'));
167                                 }
168                         }
169                         return true;
170
171                 case "mceSearchReplace":
172                         value['replacestring'] = "";
173
174                         tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value, false);
175                         return true;
176         }
177
178         // Pass to next handler in chain
179         return false;
180 }
181
182 function TinyMCE_searchreplace_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
183         return true;
184 }