svncommit
2006-11-21 84f9312e1d17725db6040554a993db38292d46bd
commit | author | age
a0109c 1 /**
f0ea59 2  * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $
a0109c 3  *
S 4  * @author Moxiecode
5  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
6  */
7
f0ea59 8 tinyMCE.importPluginLanguagePack('searchreplace');
a0109c 9
S 10 var TinyMCE_SearchReplacePlugin = {
11     getInfo : function() {
12         return {
13             longname : 'Search/Replace',
f0ea59 14             author : 'Moxiecode Systems AB',
a0109c 15             authorurl : 'http://tinymce.moxiecode.com',
S 16             infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_searchreplace.html',
17             version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
18         };
19     },
20
f0ea59 21     initInstance : function (inst) {
a0109c 22         inst.addShortcut('ctrl', 'f', 'lang_searchreplace_search_desc', 'mceSearch', true);
f0ea59 23         // No CTRL+R for "replace" because browsers will reload page instead of executing plugin
a0109c 24     },
S 25
f0ea59 26     getControlHTML : function (cn) {
a0109c 27         switch (cn) {
f0ea59 28             case "search" :
S 29                 return tinyMCE.getButtonHTML(cn, 'lang_searchreplace_search_desc', '{$pluginurl}/images/search.gif','mceSearch', true);
30
31             case "replace" :
a0109c 32                 return tinyMCE.getButtonHTML(cn, 'lang_searchreplace_replace_desc', '{$pluginurl}/images/replace.gif', 'mceSearchReplace', true);
S 33         }
f0ea59 34
a0109c 35         return "";
S 36     },
37
f0ea59 38     execCommand : function (editor_id, element, command, user_interface, value) {
S 39         var inst = tinyMCE.getInstanceById(editor_id), selectedText = inst.selection.getSelectedText(), rng;
a0109c 40
S 41         function defValue(key, default_value) {
42             value[key] = typeof(value[key]) == "undefined" ? default_value : value[key];
43         }
44
45         function replaceSel(search_str, str, back) {
f0ea59 46             inst.execCommand('mceInsertContent', false, str);
a0109c 47         }
S 48
49         if (!value)
f0ea59 50             value = [];
a0109c 51
S 52         defValue("editor_id", editor_id);
f0ea59 53         defValue("searchstring", selectedText);
a0109c 54         defValue("replacestring", null);
S 55         defValue("replacemode", "none");
56         defValue("casesensitive", false);
57         defValue("backwards", false);
58         defValue("wrap", false);
59         defValue("wholeword", false);
60         defValue("inline", "yes");
f0ea59 61         defValue("resizable", "no");
a0109c 62
S 63         switch (command) {
f0ea59 64             case "mceResetSearch" :
a0109c 65                 tinyMCE.lastSearchRng = null;
S 66                 return true;
67
f0ea59 68             case "mceSearch" :
a0109c 69                 if (user_interface) {
S 70                     var template = new Array();
71
f0ea59 72                     template['file'] = '../../plugins/searchreplace/searchreplace.htm';
S 73                     template['width'] = 380;
74                     template['height'] = 155 + (tinyMCE.isNS7 ? 20 : 0) + (tinyMCE.isMSIE ? 15 : 0);
75                     template['width'] += tinyMCE.getLang('lang_searchreplace_delta_width', 0);
76                     template['height'] += tinyMCE.getLang('lang_searchreplace_delta_height', 0);
a0109c 77
f0ea59 78                     inst.execCommand('SelectAll');
a0109c 79
S 80                     if (tinyMCE.isMSIE) {
f0ea59 81                         var r = inst.selection.getRng();
a0109c 82                         r.collapse(true);
S 83                         r.select();
84                     } else
f0ea59 85                         inst.selection.getSel().collapseToStart();
a0109c 86
S 87                     tinyMCE.openWindow(template, value);
88                 } else {
89                     var win = tinyMCE.getInstanceById(editor_id).contentWindow;
90                     var doc = tinyMCE.getInstanceById(editor_id).contentWindow.document;
91                     var body = tinyMCE.getInstanceById(editor_id).contentWindow.document.body;
92                     if (body.innerHTML == "") {
93                         alert(tinyMCE.getLang('lang_searchreplace_notfound'));
94                         return true;
95                     }
96
97                     if (value['replacemode'] == "current") {
98                         replaceSel(value['string'], value['replacestring'], value['backwards']);
99                         value['replacemode'] = "none";
100                         tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value, false);
101                         return true;
102                     }
103
104                     if (tinyMCE.isMSIE) {
105                         var rng = tinyMCE.lastSearchRng ? tinyMCE.lastSearchRng : doc.selection.createRange();
106                         var flags = 0;
107                         if (value['wholeword'])
108                             flags = flags | 2;
109
110                         if (value['casesensitive'])
111                             flags = flags | 4;
112
113                         if (!rng.findText) {
114                             alert('This operation is currently not supported by this browser.');
115                             return true;
116                         }
117
118                         if (value['replacemode'] == "all") {
119                             while (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) {
120                                 rng.scrollIntoView();
121                                 rng.select();
122                                 rng.collapse(false);
123                                 replaceSel(value['string'], value['replacestring'], value['backwards']);
124                             }
125
126                             alert(tinyMCE.getLang('lang_searchreplace_allreplaced'));
127                             return true;
128                         }
129
130                         if (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) {
131                             rng.scrollIntoView();
132                             rng.select();
133                             rng.collapse(value['backwards']);
134                             tinyMCE.lastSearchRng = rng;
135                         } else
136                             alert(tinyMCE.getLang('lang_searchreplace_notfound'));
f0ea59 137
a0109c 138                     } else {
S 139                         if (value['replacemode'] == "all") {
140                             while (win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false))
141                                 replaceSel(value['string'], value['replacestring'], value['backwards']);
142
143                             alert(tinyMCE.getLang('lang_searchreplace_allreplaced'));
144                             return true;
145                         }
146
147                         if (!win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false))
148                             alert(tinyMCE.getLang('lang_searchreplace_notfound'));
149                     }
150                 }
f0ea59 151
a0109c 152                 return true;
S 153
f0ea59 154             case "mceSearchReplace" :
a0109c 155                 value['replacestring'] = "";
S 156                 tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value, false);
157                 return true;
158         }
159
160         return false;
161     }
162 };
163
f0ea59 164 tinyMCE.addPlugin("searchreplace", TinyMCE_SearchReplacePlugin);