includes/clientside/tinymce/plugins/noneditable/editor_plugin_src.js
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 /**
       
     2  * $Id: editor_plugin_src.js 205 2007-02-12 18:58:29Z spocke $
       
     3  *
       
     4  * @author Moxiecode
       
     5  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
       
     6  */
       
     7 
       
     8 var TinyMCE_NonEditablePlugin = {
       
     9 	getInfo : function() {
       
    10 		return {
       
    11 			longname : 'Non editable elements',
       
    12 			author : 'Moxiecode Systems AB',
       
    13 			authorurl : 'http://tinymce.moxiecode.com',
       
    14 			infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/noneditable',
       
    15 			version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
       
    16 		};
       
    17 	},
       
    18 
       
    19 	initInstance : function(inst) {
       
    20 		tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/noneditable/css/noneditable.css");
       
    21 
       
    22 		// Ugly hack
       
    23 		if (tinyMCE.isMSIE5_0)
       
    24 			tinyMCE.settings['plugins'] = tinyMCE.settings['plugins'].replace(/noneditable/gi, 'Noneditable');
       
    25 	},
       
    26 
       
    27 	handleEvent : function(e) {
       
    28 		return this._moveSelection(e, tinyMCE.selectedInstance);
       
    29 	},
       
    30 
       
    31 	cleanup : function(type, content, inst) {
       
    32 		switch (type) {
       
    33 			case "insert_to_editor_dom":
       
    34 				var nodes, i, editClass, nonEditClass, editable, elm;
       
    35 
       
    36 				// Pass through Gecko
       
    37 				if (tinyMCE.isGecko)
       
    38 					return content;
       
    39 
       
    40 				nodes = tinyMCE.getNodeTree(content, [], 1);
       
    41 
       
    42 				editClass = tinyMCE.getParam("noneditable_editable_class", "mceEditable");
       
    43 				nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceNonEditable");
       
    44 
       
    45 				for (i=0; i<nodes.length; i++) {
       
    46 					elm = nodes[i];
       
    47 
       
    48 					// Convert contenteditable to classes
       
    49 					editable = tinyMCE.getAttrib(elm, "contenteditable");
       
    50 					if (new RegExp("true|false","gi").test(editable))
       
    51 						TinyMCE_NonEditablePlugin._setEditable(elm, editable == "true");
       
    52 
       
    53 					if (tinyMCE.isIE) {
       
    54 						if (tinyMCE.hasCSSClass(elm, editClass))
       
    55 							elm.contentEditable = true;
       
    56 
       
    57 						if (tinyMCE.hasCSSClass(elm, nonEditClass))
       
    58 							elm.contentEditable = false;
       
    59 					}
       
    60 				}
       
    61 
       
    62 				break;
       
    63 
       
    64 			case "insert_to_editor":
       
    65 				var editClass = tinyMCE.getParam("noneditable_editable_class", "mceEditable");
       
    66 				var nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceNonEditable");
       
    67 
       
    68 				// Replace mceItem to new school
       
    69 				content = content.replace(/mceItemEditable/g, editClass);
       
    70 				content = content.replace(/mceItemNonEditable/g, nonEditClass);
       
    71 
       
    72 				if (tinyMCE.isIE && (content.indexOf(editClass) != -1 || content.indexOf(nonEditClass) != -1)) {
       
    73 					content = content.replace(new RegExp("class=\"(.+)(" + editClass + ")\"", "gi"), 'class="$1$2" contenteditable="true"');
       
    74 					content = content.replace(new RegExp("class=\"(.+)(" + nonEditClass + ")\"", "gi"), 'class="$1$2" contenteditable="false"');
       
    75 					content = content.replace(new RegExp("class=\"(" + editClass + ")([^\"]*)\"", "gi"), 'class="$1$2" contenteditable="true"');
       
    76 					content = content.replace(new RegExp("class=\"(" + nonEditClass + ")([^\"]*)\"", "gi"), 'class="$1$2" contenteditable="false"');
       
    77 					content = content.replace(new RegExp("class=\"(.+)(" + editClass + ")([^\"]*)\"", "gi"), 'class="$1$2$3" contenteditable="true"');
       
    78 					content = content.replace(new RegExp("class=\"(.+)(" + nonEditClass + ")([^\"]*)\"", "gi"), 'class="$1$2$3" contenteditable="false"');
       
    79 				}
       
    80 
       
    81 				break;
       
    82 
       
    83 			case "get_from_editor_dom":
       
    84 				// Pass through Gecko
       
    85 				if (tinyMCE.isGecko)
       
    86 					return content;
       
    87 
       
    88 				if (tinyMCE.getParam("noneditable_leave_contenteditable", false)) {
       
    89 					var nodes = tinyMCE.getNodeTree(content, new Array(), 1);
       
    90 
       
    91 					for (var i=0; i<nodes.length; i++)
       
    92 						nodes[i].removeAttribute("contenteditable");
       
    93 				}
       
    94 
       
    95 				break;
       
    96 		}
       
    97 
       
    98 		return content;
       
    99 	},
       
   100 
       
   101 	_moveSelection : function(e, inst) {
       
   102 		var s, r, sc, ec, el, c = tinyMCE.getParam('noneditable_editable_class', 'mceNonEditable');
       
   103 
       
   104 		if (!inst)
       
   105 			return true;
       
   106 
       
   107 		// Always select whole element
       
   108 		if (tinyMCE.isGecko) {
       
   109 			s = inst.selection.getSel();
       
   110 			r = s.getRangeAt(0);
       
   111 			sc = tinyMCE.getParentNode(r.startContainer, function (n) {return tinyMCE.hasCSSClass(n, c);});
       
   112 			ec = tinyMCE.getParentNode(r.endContainer, function (n) {return tinyMCE.hasCSSClass(n, c);});
       
   113 
       
   114 			sc && r.setStartBefore(sc);
       
   115 			ec && r.setEndAfter(ec);
       
   116 
       
   117 			if (sc || ec) {
       
   118 				if (e.type == 'keypress' && e.keyCode == 39) {
       
   119 					el = sc || ec;
       
   120 
       
   121 					// Try!!
       
   122 				}
       
   123 
       
   124 				s.removeAllRanges();
       
   125 				s.addRange(r);
       
   126 
       
   127 				return tinyMCE.cancelEvent(e);
       
   128 			}
       
   129 		}
       
   130 
       
   131 		return true;
       
   132 	},
       
   133 
       
   134 	_setEditable : function(elm, state) {
       
   135 		var editClass = tinyMCE.getParam("noneditable_editable_class", "mceEditable");
       
   136 		var nonEditClass = tinyMCE.getParam("noneditable_noneditable_class", "mceNonEditable");
       
   137 
       
   138 		var className = elm.className ? elm.className : "";
       
   139 
       
   140 		if (className.indexOf(editClass) != -1 || className.indexOf(nonEditClass) != -1)
       
   141 			return;
       
   142 
       
   143 		if ((className = tinyMCE.getAttrib(elm, "class")) != "")
       
   144 			className += " ";
       
   145 
       
   146 		className += state ? editClass : nonEditClass;
       
   147 
       
   148 		elm.setAttribute("class", className);
       
   149 		elm.className = className;
       
   150 	}
       
   151 };
       
   152 
       
   153 tinyMCE.addPlugin("noneditable", TinyMCE_NonEditablePlugin);