web/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin.dev.js
author ymh
Fri, 12 Mar 2010 13:29:04 +0000
changeset 1 0d28b7c10758
permissions -rw-r--r--
First commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
0d28b7c10758 First commit
ymh
parents:
diff changeset
     1
0d28b7c10758 First commit
ymh
parents:
diff changeset
     2
(function() {
0d28b7c10758 First commit
ymh
parents:
diff changeset
     3
	tinymce.create('tinymce.plugins.wpEditImage', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
     4
0d28b7c10758 First commit
ymh
parents:
diff changeset
     5
		init : function(ed, url) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
     6
			var t = this;
0d28b7c10758 First commit
ymh
parents:
diff changeset
     7
0d28b7c10758 First commit
ymh
parents:
diff changeset
     8
			t.url = url;
0d28b7c10758 First commit
ymh
parents:
diff changeset
     9
			t._createButtons();
0d28b7c10758 First commit
ymh
parents:
diff changeset
    10
0d28b7c10758 First commit
ymh
parents:
diff changeset
    11
			// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    12
			ed.addCommand('WP_EditImage', function() {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    13
				var el = ed.selection.getNode(), vp = tinymce.DOM.getViewPort(), H = vp.h, W = ( 720 < vp.w ) ? 720 : vp.w, cls = ed.dom.getAttrib(el, 'class');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    14
0d28b7c10758 First commit
ymh
parents:
diff changeset
    15
				if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    16
					return;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    17
0d28b7c10758 First commit
ymh
parents:
diff changeset
    18
				tb_show('', url + '/editimage.html?ver=321&TB_iframe=true');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    19
				tinymce.DOM.setStyles('TB_window', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    20
					'width':( W - 50 )+'px',
0d28b7c10758 First commit
ymh
parents:
diff changeset
    21
					'height':( H - 45 )+'px',
0d28b7c10758 First commit
ymh
parents:
diff changeset
    22
					'margin-left':'-'+parseInt((( W - 50 ) / 2),10) + 'px'
0d28b7c10758 First commit
ymh
parents:
diff changeset
    23
				});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    24
0d28b7c10758 First commit
ymh
parents:
diff changeset
    25
				if ( ! tinymce.isIE6 ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    26
					tinymce.DOM.setStyles('TB_window', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    27
						'top':'20px',
0d28b7c10758 First commit
ymh
parents:
diff changeset
    28
						'marginTop':'0'
0d28b7c10758 First commit
ymh
parents:
diff changeset
    29
					});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    30
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    31
0d28b7c10758 First commit
ymh
parents:
diff changeset
    32
				tinymce.DOM.setStyles('TB_iframeContent', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    33
					'width':( W - 50 )+'px',
0d28b7c10758 First commit
ymh
parents:
diff changeset
    34
					'height':( H - 75 )+'px'
0d28b7c10758 First commit
ymh
parents:
diff changeset
    35
				});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    36
				tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    37
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    38
0d28b7c10758 First commit
ymh
parents:
diff changeset
    39
			ed.onInit.add(function(ed) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    40
				tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    41
					if ( !tinymce.isGecko && e.target.nodeName == 'IMG' && ed.dom.getParent(e.target, 'dl.wp-caption') )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    42
						return tinymce.dom.Event.cancel(e);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    43
				});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    44
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    45
0d28b7c10758 First commit
ymh
parents:
diff changeset
    46
			ed.onMouseUp.add(function(ed, e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    47
				if ( tinymce.isWebKit || tinymce.isOpera )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    48
					return;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    49
0d28b7c10758 First commit
ymh
parents:
diff changeset
    50
				if ( ed.dom.getParent(e.target, 'div.mceTemp') || ed.dom.is(e.target, 'div.mceTemp') ) {					
0d28b7c10758 First commit
ymh
parents:
diff changeset
    51
					window.setTimeout(function(){
0d28b7c10758 First commit
ymh
parents:
diff changeset
    52
						var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL = ed.dom.getParent(n, 'dl.wp-caption');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    53
0d28b7c10758 First commit
ymh
parents:
diff changeset
    54
						if ( DL && n.width != ( parseInt(ed.dom.getStyle(DL, 'width'), 10) - 10 ) ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    55
							ed.dom.setStyle(DL, 'width', parseInt(n.width, 10) + 10);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    56
							ed.execCommand('mceRepaint');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    57
						}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    58
					}, 100);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    59
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    60
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    61
0d28b7c10758 First commit
ymh
parents:
diff changeset
    62
			ed.onMouseDown.add(function(ed, e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    63
				var p;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    64
0d28b7c10758 First commit
ymh
parents:
diff changeset
    65
				if ( e.target.nodeName == 'IMG' && ed.dom.getAttrib(e.target, 'class').indexOf('mceItem') == -1 ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    66
					ed.plugins.wordpress._showButtons(e.target, 'wp_editbtns');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    67
					if ( tinymce.isGecko && (p = ed.dom.getParent(e.target, 'dl.wp-caption')) && ed.dom.hasClass(p.parentNode, 'mceTemp') )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    68
						ed.selection.select(p.parentNode);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    69
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    70
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    71
0d28b7c10758 First commit
ymh
parents:
diff changeset
    72
			ed.onKeyPress.add(function(ed, e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    73
				var DL, DIV, P;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    74
0d28b7c10758 First commit
ymh
parents:
diff changeset
    75
				if ( e.keyCode == 13 && (DL = ed.dom.getParent(ed.selection.getNode(), 'DL')) && ed.dom.hasClass(DL, 'wp-caption') ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    76
					P = ed.dom.create('p', {}, '&nbsp;');
0d28b7c10758 First commit
ymh
parents:
diff changeset
    77
					if ( (DIV = DL.parentNode) && DIV.nodeName == 'DIV' ) 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    78
						ed.dom.insertAfter( P, DIV );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    79
					else
0d28b7c10758 First commit
ymh
parents:
diff changeset
    80
						ed.dom.insertAfter( P, DL );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    81
0d28b7c10758 First commit
ymh
parents:
diff changeset
    82
					if ( P.firstChild )
0d28b7c10758 First commit
ymh
parents:
diff changeset
    83
						ed.selection.select(P.firstChild);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    84
					else
0d28b7c10758 First commit
ymh
parents:
diff changeset
    85
						ed.selection.select(P);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    86
0d28b7c10758 First commit
ymh
parents:
diff changeset
    87
					tinymce.dom.Event.cancel(e);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    88
					return false;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    89
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    90
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    91
0d28b7c10758 First commit
ymh
parents:
diff changeset
    92
			ed.onBeforeSetContent.add(function(ed, o) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    93
				o.content = t._do_shcode(o.content);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    94
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
    95
0d28b7c10758 First commit
ymh
parents:
diff changeset
    96
			ed.onPostProcess.add(function(ed, o) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    97
				if (o.get)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    98
					o.content = t._get_shcode(o.content);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    99
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   100
		},
0d28b7c10758 First commit
ymh
parents:
diff changeset
   101
0d28b7c10758 First commit
ymh
parents:
diff changeset
   102
		_do_shcode : function(co) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   103
			return co.replace(/\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\][\s\u00a0]*/g, function(a,b,c){
0d28b7c10758 First commit
ymh
parents:
diff changeset
   104
				var id, cls, w, cap, div_cls;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   105
				
0d28b7c10758 First commit
ymh
parents:
diff changeset
   106
				b = b.replace(/\\'|\\&#39;|\\&#039;/g, '&#39;').replace(/\\"|\\&quot;/g, '&quot;');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   107
				c = c.replace(/\\&#39;|\\&#039;/g, '&#39;').replace(/\\&quot;/g, '&quot;');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   108
				id = b.match(/id=['"]([^'"]+)/i);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   109
				cls = b.match(/align=['"]([^'"]+)/i);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   110
				w = b.match(/width=['"]([0-9]+)/);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   111
				cap = b.match(/caption=['"]([^'"]+)/i);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   112
0d28b7c10758 First commit
ymh
parents:
diff changeset
   113
				id = ( id && id[1] ) ? id[1] : '';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   114
				cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   115
				w = ( w && w[1] ) ? w[1] : '';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   116
				cap = ( cap && cap[1] ) ? cap[1] : '';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   117
				if ( ! w || ! cap ) return c;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   118
				
0d28b7c10758 First commit
ymh
parents:
diff changeset
   119
				div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   120
0d28b7c10758 First commit
ymh
parents:
diff changeset
   121
				return '<div class="'+div_cls+'" draggable><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
0d28b7c10758 First commit
ymh
parents:
diff changeset
   122
				'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   123
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   124
		},
0d28b7c10758 First commit
ymh
parents:
diff changeset
   125
0d28b7c10758 First commit
ymh
parents:
diff changeset
   126
		_get_shcode : function(co) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   127
			return co.replace(/<div class="mceTemp[^"]*">\s*<dl([^>]+)>\s*<dt[^>]+>([\s\S]+?)<\/dt>\s*<dd[^>]+>(.+?)<\/dd>\s*<\/dl>\s*<\/div>\s*/gi, function(a,b,c,cap){
0d28b7c10758 First commit
ymh
parents:
diff changeset
   128
				var id, cls, w;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   129
				
0d28b7c10758 First commit
ymh
parents:
diff changeset
   130
				id = b.match(/id=['"]([^'"]+)/i);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   131
				cls = b.match(/class=['"]([^'"]+)/i);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   132
				w = c.match(/width=['"]([0-9]+)/);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   133
0d28b7c10758 First commit
ymh
parents:
diff changeset
   134
				id = ( id && id[1] ) ? id[1] : '';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   135
				cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   136
				w = ( w && w[1] ) ? w[1] : '';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   137
0d28b7c10758 First commit
ymh
parents:
diff changeset
   138
				if ( ! w || ! cap ) return c;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   139
				cls = cls.match(/align[^ '"]+/) || 'alignnone';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   140
				cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   141
0d28b7c10758 First commit
ymh
parents:
diff changeset
   142
				return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
0d28b7c10758 First commit
ymh
parents:
diff changeset
   143
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   144
		},
0d28b7c10758 First commit
ymh
parents:
diff changeset
   145
0d28b7c10758 First commit
ymh
parents:
diff changeset
   146
		_createButtons : function() {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   147
			var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   148
0d28b7c10758 First commit
ymh
parents:
diff changeset
   149
			DOM.remove('wp_editbtns');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   150
0d28b7c10758 First commit
ymh
parents:
diff changeset
   151
			DOM.add(document.body, 'div', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   152
				id : 'wp_editbtns',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   153
				style : 'display:none;'
0d28b7c10758 First commit
ymh
parents:
diff changeset
   154
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   155
0d28b7c10758 First commit
ymh
parents:
diff changeset
   156
			editButton = DOM.add('wp_editbtns', 'img', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   157
				src : t.url+'/img/image.png',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   158
				id : 'wp_editimgbtn',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   159
				width : '24',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   160
				height : '24',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   161
				title : ed.getLang('wpeditimage.edit_img')
0d28b7c10758 First commit
ymh
parents:
diff changeset
   162
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   163
0d28b7c10758 First commit
ymh
parents:
diff changeset
   164
			tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   165
				var ed = tinyMCE.activeEditor;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   166
				ed.windowManager.bookmark = ed.selection.getBookmark('simple');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   167
				ed.execCommand("WP_EditImage");
0d28b7c10758 First commit
ymh
parents:
diff changeset
   168
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   169
0d28b7c10758 First commit
ymh
parents:
diff changeset
   170
			dellButton = DOM.add('wp_editbtns', 'img', {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   171
				src : t.url+'/img/delete.png',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   172
				id : 'wp_delimgbtn',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   173
				width : '24',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   174
				height : '24',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   175
				title : ed.getLang('wpeditimage.del_img')
0d28b7c10758 First commit
ymh
parents:
diff changeset
   176
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   177
0d28b7c10758 First commit
ymh
parents:
diff changeset
   178
			tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   179
				var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   180
0d28b7c10758 First commit
ymh
parents:
diff changeset
   181
				if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   182
					if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
0d28b7c10758 First commit
ymh
parents:
diff changeset
   183
						ed.dom.remove(p);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   184
					else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
0d28b7c10758 First commit
ymh
parents:
diff changeset
   185
						ed.dom.remove(p);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   186
					else
0d28b7c10758 First commit
ymh
parents:
diff changeset
   187
						ed.dom.remove(el);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   188
0d28b7c10758 First commit
ymh
parents:
diff changeset
   189
					ed.execCommand('mceRepaint');
0d28b7c10758 First commit
ymh
parents:
diff changeset
   190
					return false;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   191
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   192
			});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   193
		},
0d28b7c10758 First commit
ymh
parents:
diff changeset
   194
0d28b7c10758 First commit
ymh
parents:
diff changeset
   195
		getInfo : function() {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   196
			return {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   197
				longname : 'Edit Image',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   198
				author : 'WordPress',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   199
				authorurl : 'http://wordpress.org',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   200
				infourl : '',
0d28b7c10758 First commit
ymh
parents:
diff changeset
   201
				version : "1.0"
0d28b7c10758 First commit
ymh
parents:
diff changeset
   202
			};
0d28b7c10758 First commit
ymh
parents:
diff changeset
   203
		}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   204
	});
0d28b7c10758 First commit
ymh
parents:
diff changeset
   205
0d28b7c10758 First commit
ymh
parents:
diff changeset
   206
	tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   207
})();