Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/editor/plugins/loremipsum.js
2868 views
1
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview A plugin that fills the field with lorem ipsum text when it's
17
* empty and does not have the focus. Applies to both editable and uneditable
18
* fields.
19
*
20
* @author [email protected] (Nick Santos)
21
*/
22
23
goog.provide('goog.editor.plugins.LoremIpsum');
24
25
goog.require('goog.asserts');
26
goog.require('goog.dom');
27
goog.require('goog.editor.Command');
28
goog.require('goog.editor.Field');
29
goog.require('goog.editor.Plugin');
30
goog.require('goog.editor.node');
31
goog.require('goog.functions');
32
goog.require('goog.html.SafeHtml');
33
goog.require('goog.userAgent');
34
35
36
37
/**
38
* A plugin that manages lorem ipsum state of editable fields.
39
* @param {string} message The lorem ipsum message.
40
* @constructor
41
* @extends {goog.editor.Plugin}
42
* @final
43
*/
44
goog.editor.plugins.LoremIpsum = function(message) {
45
goog.editor.Plugin.call(this);
46
47
/**
48
* The lorem ipsum message.
49
* @type {string}
50
* @private
51
*/
52
this.message_ = message;
53
};
54
goog.inherits(goog.editor.plugins.LoremIpsum, goog.editor.Plugin);
55
56
57
/** @override */
58
goog.editor.plugins.LoremIpsum.prototype.getTrogClassId =
59
goog.functions.constant('LoremIpsum');
60
61
62
/** @override */
63
goog.editor.plugins.LoremIpsum.prototype.activeOnUneditableFields =
64
goog.functions.TRUE;
65
66
67
/**
68
* Whether the field is currently filled with lorem ipsum text.
69
* @type {boolean}
70
* @private
71
*/
72
goog.editor.plugins.LoremIpsum.prototype.usingLorem_ = false;
73
74
75
/**
76
* Handles queryCommandValue.
77
* @param {string} command The command to query.
78
* @return {boolean} The result.
79
* @override
80
*/
81
goog.editor.plugins.LoremIpsum.prototype.queryCommandValue = function(command) {
82
return command == goog.editor.Command.USING_LOREM && this.usingLorem_;
83
};
84
85
86
/**
87
* Handles execCommand.
88
* @param {string} command The command to execute.
89
* Should be CLEAR_LOREM or UPDATE_LOREM.
90
* @param {*=} opt_placeCursor Whether to place the cursor in the field
91
* after clearing lorem. Should be a boolean.
92
* @override
93
*/
94
goog.editor.plugins.LoremIpsum.prototype.execCommand = function(
95
command, opt_placeCursor) {
96
if (command == goog.editor.Command.CLEAR_LOREM) {
97
this.clearLorem_(!!opt_placeCursor);
98
} else if (command == goog.editor.Command.UPDATE_LOREM) {
99
this.updateLorem_();
100
}
101
};
102
103
104
/** @override */
105
goog.editor.plugins.LoremIpsum.prototype.isSupportedCommand = function(
106
command) {
107
return command == goog.editor.Command.CLEAR_LOREM ||
108
command == goog.editor.Command.UPDATE_LOREM ||
109
command == goog.editor.Command.USING_LOREM;
110
};
111
112
113
/**
114
* Set the lorem ipsum text in a goog.editor.Field if needed.
115
* @private
116
*/
117
goog.editor.plugins.LoremIpsum.prototype.updateLorem_ = function() {
118
// Try to apply lorem ipsum if:
119
// 1) We have lorem ipsum text
120
// 2) There's not a dialog open, as that screws
121
// with the dialog's ability to properly restore the selection
122
// on dialog close (since the DOM nodes would get clobbered in FF)
123
// 3) We're not using lorem already
124
// 4) The field is not currently active (doesn't have focus).
125
var fieldObj = this.getFieldObject();
126
if (!this.usingLorem_ && !fieldObj.inModalMode() &&
127
goog.editor.Field.getActiveFieldId() != fieldObj.id) {
128
var field = fieldObj.getElement();
129
if (!field) {
130
// Fallback on the original element. This is needed by
131
// fields managed by click-to-edit.
132
field = fieldObj.getOriginalElement();
133
}
134
135
goog.asserts.assert(field);
136
if (goog.editor.node.isEmpty(field)) {
137
this.usingLorem_ = true;
138
139
// Save the old font style so it can be restored when we
140
// clear the lorem ipsum style.
141
this.oldFontStyle_ = field.style.fontStyle;
142
field.style.fontStyle = 'italic';
143
fieldObj.setSafeHtml(
144
true, goog.html.SafeHtml.htmlEscapePreservingNewlines(this.message_),
145
true);
146
}
147
}
148
};
149
150
151
/**
152
* Clear an EditableField's lorem ipsum and put in initial text if needed.
153
*
154
* If using click-to-edit mode (where Trogedit manages whether the field
155
* is editable), this works for both editable and uneditable fields.
156
*
157
* TODO(user): Is this really necessary? See TODO below.
158
* @param {boolean=} opt_placeCursor Whether to place the cursor in the field
159
* after clearing lorem.
160
* @private
161
*/
162
goog.editor.plugins.LoremIpsum.prototype.clearLorem_ = function(
163
opt_placeCursor) {
164
// Don't mess with lorem state when a dialog is open as that screws
165
// with the dialog's ability to properly restore the selection
166
// on dialog close (since the DOM nodes would get clobbered)
167
var fieldObj = this.getFieldObject();
168
if (this.usingLorem_ && !fieldObj.inModalMode()) {
169
var field = fieldObj.getElement();
170
if (!field) {
171
// Fallback on the original element. This is needed by
172
// fields managed by click-to-edit.
173
field = fieldObj.getOriginalElement();
174
}
175
176
goog.asserts.assert(field);
177
this.usingLorem_ = false;
178
field.style.fontStyle = this.oldFontStyle_;
179
fieldObj.setSafeHtml(true, null, true);
180
181
// TODO(nicksantos): I'm pretty sure that this is a hack, but talk to
182
// Julie about why this is necessary and what to do with it. Really,
183
// we need to figure out where it's necessary and remove it where it's
184
// not. Safari never places the cursor on its own willpower.
185
if (opt_placeCursor && fieldObj.isLoaded()) {
186
if (goog.userAgent.WEBKIT) {
187
goog.dom.getOwnerDocument(fieldObj.getElement()).body.focus();
188
fieldObj.focusAndPlaceCursorAtStart();
189
} else if (goog.userAgent.OPERA) {
190
fieldObj.placeCursorAtStart();
191
}
192
}
193
}
194
};
195
196