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