Debian Codegraph @

closure-library/closure/goog/demos/editor/helloworlddialog.js

raw
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 An example of how to write a dialog to be opened by a plugin.
17 *
18 */
19
20 goog.provide('goog.demos.editor.HelloWorldDialog');
21 goog.provide('goog.demos.editor.HelloWorldDialog.OkEvent');
22
23 goog.require('goog.dom.TagName');
24 goog.require('goog.events.Event');
25 goog.require('goog.string');
26 goog.require('goog.ui.editor.AbstractDialog');
27
28
29 // *** Public interface ***************************************************** //
30
31
32
33 /**
34 * Creates a dialog to let the user enter a customized hello world message.
35 * @param {goog.dom.DomHelper} domHelper DomHelper to be used to create the
36 * dialog's dom structure.
37 * @constructor
38 * @extends {goog.ui.editor.AbstractDialog}
39 * @final
40 */
41 goog.demos.editor.HelloWorldDialog = function(domHelper) {
42 goog.ui.editor.AbstractDialog.call(this, domHelper);
43 };
44 goog.inherits(
45 goog.demos.editor.HelloWorldDialog, goog.ui.editor.AbstractDialog);
46
47
48 // *** Event **************************************************************** //
49
50
51
52 /**
53 * OK event object for the hello world dialog.
54 * @param {string} message Customized hello world message chosen by the user.
55 * @constructor
56 * @extends {goog.events.Event}
57 * @final
58 */
59 goog.demos.editor.HelloWorldDialog.OkEvent = function(message) {
60 this.message = message;
61 };
62 goog.inherits(goog.demos.editor.HelloWorldDialog.OkEvent, goog.events.Event);
63
64
65 /**
66 * Event type.
67 * @type {goog.ui.editor.AbstractDialog.EventType}
68 * @override
69 */
70 goog.demos.editor.HelloWorldDialog.OkEvent.prototype.type =
71 goog.ui.editor.AbstractDialog.EventType.OK;
72
73
74 /**
75 * Customized hello world message chosen by the user.
76 * @type {string}
77 */
78 goog.demos.editor.HelloWorldDialog.OkEvent.prototype.message;
79
80
81 // *** Protected interface ************************************************** //
82
83
84 /** @override */
85 goog.demos.editor.HelloWorldDialog.prototype.createDialogControl = function() {
86 var builder = new goog.ui.editor.AbstractDialog.Builder(this);
87 /** @desc Title of the hello world dialog. */
88 var MSG_HELLO_WORLD_DIALOG_TITLE = goog.getMsg('Add a Hello World message');
89 builder.setTitle(MSG_HELLO_WORLD_DIALOG_TITLE)
90 .setContent(this.createContent_());
91 return builder.build();
92 };
93
94
95 /**
96 * Creates and returns the event object to be used when dispatching the OK
97 * event to listeners, or returns null to prevent the dialog from closing.
98 * @param {goog.events.Event} e The event object dispatched by the wrapped
99 * dialog.
100 * @return {goog.demos.editor.HelloWorldDialog.OkEvent} The event object to be
101 * used when dispatching the OK event to listeners.
102 * @protected
103 * @override
104 */
105 goog.demos.editor.HelloWorldDialog.prototype.createOkEvent = function(e) {
106 var message = this.getMessage_();
107 if (message &&
108 goog.demos.editor.HelloWorldDialog.isValidHelloWorld_(message)) {
109 return new goog.demos.editor.HelloWorldDialog.OkEvent(message);
110 } else {
111 /** @desc Error message telling the user why their message was rejected. */
112 var MSG_HELLO_WORLD_DIALOG_ERROR =
113 goog.getMsg('Your message must contain the words "hello" and "world".');
114 this.dom.getWindow().alert(MSG_HELLO_WORLD_DIALOG_ERROR);
115 return null; // Prevents the dialog from closing.
116 }
117 };
118
119
120 // *** Private implementation *********************************************** //
121
122
123 /**
124 * Input element where the user will type their hello world message.
125 * @type {Element}
126 * @private
127 */
128 goog.demos.editor.HelloWorldDialog.prototype.input_;
129
130
131 /**
132 * Creates the DOM structure that makes up the dialog's content area.
133 * @return {Element} The DOM structure that makes up the dialog's content area.
134 * @private
135 */
136 goog.demos.editor.HelloWorldDialog.prototype.createContent_ = function() {
137 /** @desc Sample hello world message to prepopulate the dialog with. */
138 var MSG_HELLO_WORLD_DIALOG_SAMPLE = goog.getMsg('Hello, world!');
139 this.input_ = this.dom.createDom(
140 goog.dom.TagName.INPUT, {size: 25, value: MSG_HELLO_WORLD_DIALOG_SAMPLE});
141 /** @desc Prompt telling the user to enter a hello world message. */
142 var MSG_HELLO_WORLD_DIALOG_PROMPT =
143 goog.getMsg('Enter your Hello World message');
144 return this.dom.createDom(
145 goog.dom.TagName.DIV, null, [MSG_HELLO_WORLD_DIALOG_PROMPT, this.input_]);
146 };
147
148
149 /**
150 * Returns the hello world message currently typed into the dialog's input.
151 * @return {?string} The hello world message currently typed into the dialog's
152 * input, or null if called before the input is created.
153 * @private
154 */
155 goog.demos.editor.HelloWorldDialog.prototype.getMessage_ = function() {
156 return this.input_ && this.input_.value;
157 };
158
159
160 /**
161 * Returns whether or not the given message contains the strings "hello" and
162 * "world". Case-insensitive and order doesn't matter.
163 * @param {string} message The message to be checked.
164 * @return {boolean} Whether or not the given message contains the strings
165 * "hello" and "world".
166 * @private
167 */
168 goog.demos.editor.HelloWorldDialog.isValidHelloWorld_ = function(message) {
169 message = message.toLowerCase();
170 return goog.string.contains(message, 'hello') &&
171 goog.string.contains(message, 'world');
172 };