summaryrefslogtreecommitdiffstats
path: root/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin.js
blob: 4ad1ba3f291346f483e0a79117f730f667c082ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

(function() {
	tinymce.create('tinymce.plugins.wpEditImage', {

		init : function(ed, url) {
			var t = this;

			t.url = url;
			t._createButtons();

			// Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
			ed.addCommand('WP_EditImage', function() {
				var el = ed.selection.getNode();

				if ( ed.dom.getAttrib(el, 'class').indexOf('mceItem') != -1 || el.nodeName != 'IMG' )
					return;

				tb_show('', url + '/editimage.html?TB_iframe=true');
				tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
			});

			ed.onInit.add(function(ed) {
				tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
	  				ed.plugins.wpeditimage.hideButtons();
				});
			});

			ed.onExecCommand.add(function(ed, cmd, ui, val) {
          		if ( 'mceFullScreen' == cmd )
					ed.plugins.wpeditimage.hideButtons();
      		});

			ed.onSaveContent.add(function(ed, o) {
   				ed.plugins.wpeditimage.hideButtons();
			});

			ed.onMouseUp.add(function(ed, e) {
				if ( tinymce.isOpera )
					ed.plugins.wpeditimage.showButtons(e);
			});

			ed.onMouseDown.add(function(ed, e) {
				if ( tinymce.isOpera ) return;
				ed.plugins.wpeditimage.showButtons(e);
			});
		},

		showButtons : function(e) {
			var t = this, ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y, el = e.target;

			t.hideButtons();
			if (el.nodeName == 'IMG') {
				if (ed.dom.getAttrib(el, 'class').indexOf('mceItem') != -1)
					return;

				vp = ed.dom.getViewPort(ed.getWin());
				p1 = DOM.getPos(ed.getContentAreaContainer());
				p2 = ed.dom.getPos(el);

				X = Math.max(p2.x - vp.x, 0) + p1.x;
				Y = Math.max(p2.y - vp.y, 0) + p1.y;

				DOM.setStyles('wp_editbtns', {
					'top' : Y+5+'px',
					'left' : X+5+'px',
					'display' : 'block'
				});

				t.btnsTout = window.setTimeout( function(){ed.plugins.wpeditimage.hideButtons();}, 5000 );
			}
		},
		
		hideButtons : function() {
			tinymce.DOM.hide('wp_editbtns');
			window.clearTimeout(this.btnsTout);
		},

		_createButtons : function() {
			var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM;

			DOM.remove('wp_editbtns');

			var wp_editbtns = DOM.add(document.body, 'div', {
				id : 'wp_editbtns',
				style : 'display:none;'
			});

			var wp_editimgbtn = DOM.add('wp_editbtns', 'img', {
				src : t.url+'/img/image.png',
				id : 'wp_editimgbtn',
				width : '24',
				height : '24',
				title : 'Edit'
			});

			wp_editimgbtn.onmousedown = function(e) {
				var ed = tinyMCE.activeEditor;
				ed.windowManager.bookmark = ed.selection.getBookmark('simple');
				ed.execCommand("WP_EditImage");
				this.parentNode.style.display = 'none';
			}

			var wp_delimgbtn = DOM.add('wp_editbtns', 'img', {
				src : t.url+'/img/delete.png',
				id : 'wp_delimgbtn',
				width : '24',
				height : '24',
				title : 'Delete'
			});

			wp_delimgbtn.onmousedown = function(e) {
				var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;

				if ( el.nodeName != 'IMG' || ed.dom.getAttrib(el, 'class').indexOf('mceItem') != -1 ) return;

				if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1)
					ed.dom.remove(p);
				else ed.dom.remove(el);

				this.parentNode.style.display = 'none';
				ed.execCommand('mceRepaint');
				return false;
			}
		},

		getInfo : function() {
			return {
				longname : 'Edit Image',
				author : 'WordPress',
				authorurl : 'http://wordpress.org',
				infourl : '',
				version : "1.0"
			};
		}
	});

	tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
})();