-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimageplugin.js
180 lines (156 loc) · 5.2 KB
/
imageplugin.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
*
* @fileoverview A simple plugin that inserts an image on command. This
* plugin is intended to be used only until Google releases their own
* image plugin.
*
*/
goog.provide('goog.editor.plugins.ImagePlugin');
goog.provide('goog.editor.plugins.ImagePlugin.COMMAND');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.editor.Plugin');
goog.require('goog.ui.Bubble');
goog.require('goog.style');
/**
* Plugin to insert the Image into an editable field.
* @constructor
* @extends {goog.editor.Plugin}
*/
goog.editor.plugins.ImagePlugin = function() {
goog.editor.Plugin.call(this);
};
goog.inherits(goog.editor.plugins.ImagePlugin, goog.editor.Plugin);
/** @inheritDoc */
goog.editor.plugins.ImagePlugin.prototype.getTrogClassId = function() {
return 'ImagePlugin';
};
/**
* Commands implemented by this plugin.
* @enum {string}
*/
goog.editor.plugins.ImagePlugin.COMMAND = {
IMAGE_PLUGIN: '+ImagePlugin'
};
/** @inheritDoc */
goog.editor.plugins.ImagePlugin.prototype.isSupportedCommand = function(command) {
return command == goog.editor.plugins.ImagePlugin.COMMAND.IMAGE_PLUGIN;
};
/**
* @type {string}
*/
goog.editor.plugins.ImagePlugin.CLASS_NAME = 'ImagePluginClass';
/**
* @param {string} command Command to execute.
* @override
* @protected
*/
goog.editor.plugins.ImagePlugin.prototype.execCommandInternal = function(
command) {
var domHelper = this.fieldObject.getEditableDomHelper();
var range = this.fieldObject.getRange();
range.removeContents();
var newNode =
domHelper.createDom(goog.dom.TagName.SPAN, goog.editor.plugins.ImagePlugin.CLASS_NAME, 'Hello World!');
range.insertNode(newNode, false);
};
var defaultTimeout = 10000;
var bubble = null;
function createBubble(anchor,domHelper) {
if (bubble) {
bubble.dispose();
bubble = null;
}
// Create any number of DOM elems you want here: This is an example of a div which resizes(in the bubble)
var buttonCreate = goog.dom.createDom('div', {
'id': 'btnResize',
'class': 'btnResize',
'style': 'float:right; cursor: pointer; color: #E89372;'
}, 'Resize');
var addChildToBubble = goog.dom.createDom(
goog.dom.TagName.SPAN, "image_bubble_span", "Options to modify this Image:",
goog.dom.createDom(goog.dom.TagName.HR, {
'style': 'color: #CCCCFF;'
}),
buttonCreate
);
var detectMargin = goog.style.getSize(anchor).width;
bubble = new goog.ui.Bubble(addChildToBubble, {
bubbleWidth: 147,
marginShift: detectMargin,
cssBubbleFont: goog.getCssName('goog-bubble-font'),
cssCloseButton: goog.getCssName('goog-bubble-close-button'),
cssBubbleTopRightAnchor: goog.getCssName('goog-bubble-top-no-anchor'),
cssBubbleTopLeftAnchor: goog.getCssName('goog-bubble-top-no-anchor'),
cssBubbleTopNoAnchor: goog.getCssName('goog-bubble-top-no-anchor'),
cssBubbleBottomRightAnchor: goog.getCssName('goog-bubble-bottom-no-anchor'),
cssBubbleBottomLeftAnchor: goog.getCssName('goog-bubble-bottom-no-anchor'),
cssBubbleBottomNoAnchor: goog.getCssName('goog-bubble-bottom-no-anchor'),
cssBubbleLeft: goog.getCssName('goog-bubble-left'),
cssBubbleRight: goog.getCssName('goog-bubble-right')
});
bubble.setAutoHide(true);
bubble.setPosition(new goog.positioning.AnchoredPosition(anchor, null));
bubble.render();
bubble.attach(anchor);
bubble.setVisible(true);
goog.events.listen(goog.dom.getElement('btnResize'), 'click', function() {
anchor.style.position = 'relative';
goog.style.setSize(anchor, '100px', '100px');
createBubble(anchor,domHelper);
});
try {
goog.events.listen(goog.dom.getElement('btnResize'), ['mouseover', 'mouseout'], function(e) {
if(e.type == 'mouseover') {
this.style.textDecoration = 'underline';
} else {
this.style.textDecoration = 'none';
}
})}
catch(err) {
// DO NOTHING!!
}
}
function removeButton() {
bubble.setVisible(false);
}
// This handles the selection Change inside the Editor. ( i.e. Detects which element is selected)
try {
goog.editor.plugins.ImagePlugin.prototype.handleSelectionChange = function(opt_e) {
var dom = this.fieldObject.doFieldSizingGecko();
var domHelper = this.fieldObject.getEditableDomHelper(), selectedElt, selectedEltCover;
var range = this.fieldObject.getRange();
if(opt_e) {
selectedElt = opt_e.target;
} else {
var range = this.fieldObject.getRange();
selectedElt = range && range.getContainerElement();
}
if(selectedElt.nodeName == goog.dom.TagName.IMG) {
createBubble(selectedElt,dom);
} else {
try {
removeButton();
} catch(e) {
//DO NOTHING!!
}
}
}
} catch(err) {
// DO NOTHING!!
}
// This needs to be called if the bubbles are not removed after calling makeUneditable() on the editor.
// This should be called before making the editor uneditable.
// ISSUE: The bubble is absolutely positioned even after the editor has been made uneditable.
// TODO: Come up with a cleaner solution. (move from using goog.ui.Bubble to the link bubble provided by Google in rev157).
try {
goog.editor.plugins.ImagePlugin.removeAllBubbles = function() {
try {
removeButton();
} catch(e) {
//DO NOTHING!!
}
}
} catch(err) {
// DO NOTHING!!
}