aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/editor/abstractdialog.js
blob: 4b73802152514126bcc058a98246d05396c5e4d4 (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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Wrapper around {@link goog.ui.Dialog}, to provide
 * dialogs that are smarter about interacting with a rich text editor.
 *
 */

goog.provide('goog.ui.editor.AbstractDialog');
goog.provide('goog.ui.editor.AbstractDialog.Builder');
goog.provide('goog.ui.editor.AbstractDialog.EventType');

goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.events.EventTarget');
goog.require('goog.ui.Dialog');
goog.require('goog.ui.Dialog.ButtonSet');
goog.require('goog.ui.Dialog.DefaultButtonKeys');
goog.require('goog.ui.Dialog.Event');
goog.require('goog.ui.Dialog.EventType');


// *** Public interface ***************************************************** //



/**
 * Creates an object that represents a dialog box.
 * @param {goog.dom.DomHelper} domHelper DomHelper to be used to create the
 * dialog's dom structure.
 * @constructor
 * @extends {goog.events.EventTarget}
 */
goog.ui.editor.AbstractDialog = function(domHelper) {
  goog.events.EventTarget.call(this);
  this.dom = domHelper;
};
goog.inherits(goog.ui.editor.AbstractDialog, goog.events.EventTarget);


/**
 * Causes the dialog box to appear, centered on the screen. Lazily creates the
 * dialog if needed.
 */
goog.ui.editor.AbstractDialog.prototype.show = function() {
  // Lazily create the wrapped dialog to be shown.
  if (!this.dialogInternal_) {
    this.dialogInternal_ = this.createDialogControl();
    this.dialogInternal_.addEventListener(goog.ui.Dialog.EventType.AFTER_HIDE,
        this.handleAfterHide_, false, this);
  }

  this.dialogInternal_.setVisible(true);
};


/**
 * Hides the dialog, causing AFTER_HIDE to fire.
 */
goog.ui.editor.AbstractDialog.prototype.hide = function() {
  if (this.dialogInternal_) {
    // This eventually fires the wrapped dialog's AFTER_HIDE event, calling our
    // handleAfterHide_().
    this.dialogInternal_.setVisible(false);
  }
};


/**
 * @return {boolean} Whether the dialog is open.
 */
goog.ui.editor.AbstractDialog.prototype.isOpen = function() {
  return !!this.dialogInternal_ && this.dialogInternal_.isVisible();
};


/**
 * Runs the handler registered on the OK button event and closes the dialog if
 * that handler succeeds.
 * This is useful in cases such as double-clicking an item in the dialog is
 * equivalent to selecting it and clicking the default button.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.processOkAndClose = function() {
  // Fake an OK event from the wrapped dialog control.
  var evt = new goog.ui.Dialog.Event(goog.ui.Dialog.DefaultButtonKeys.OK, null);
  if (this.handleOk(evt)) {
    // handleOk calls dispatchEvent, so if any listener calls preventDefault it
    // will return false and we won't hide the dialog.
    this.hide();
  }
};


// *** Dialog events ******************************************************** //


/**
 * Event type constants for events the dialog fires.
 * @enum {string}
 */
goog.ui.editor.AbstractDialog.EventType = {
  // This event is fired after the dialog is hidden, no matter if it was closed
  // via OK or Cancel or is being disposed without being hidden first.
  AFTER_HIDE: 'afterhide',
  // Either the cancel or OK events can be canceled via preventDefault or by
  // returning false from their handlers to stop the dialog from closing.
  CANCEL: 'cancel',
  OK: 'ok'
};


// *** Inner helper class *************************************************** //



/**
 * A builder class for the dialog control. All methods except build return this.
 * @param {goog.ui.editor.AbstractDialog} editorDialog Editor dialog object
 *     that will wrap the wrapped dialog object this builder will create.
 * @constructor
 */
goog.ui.editor.AbstractDialog.Builder = function(editorDialog) {
  // We require the editor dialog to be passed in so that the builder can set up
  // ok/cancel listeners by default, making it easier for most dialogs.
  this.editorDialog_ = editorDialog;
  this.wrappedDialog_ = new goog.ui.Dialog('', true, this.editorDialog_.dom);
  this.buttonSet_ = new goog.ui.Dialog.ButtonSet(this.editorDialog_.dom);
  this.buttonHandlers_ = {};
  this.addClassName(goog.getCssName('tr-dialog'));
};


/**
 * Sets the title of the dialog.
 * @param {string} title Title HTML (escaped).
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.setTitle = function(title) {
  this.wrappedDialog_.setTitle(title);
  return this;
};


/**
 * Adds an OK button to the dialog. Clicking this button will cause {@link
 * handleOk} to run, subsequently dispatching an OK event.
 * @param {string=} opt_label The caption for the button, if not "OK".
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.addOkButton =
    function(opt_label) {
  var key = goog.ui.Dialog.DefaultButtonKeys.OK;
  /** @desc Label for an OK button in an editor dialog. */
  var MSG_TR_DIALOG_OK = goog.getMsg('OK');
  // True means this is the default/OK button.
  this.buttonSet_.set(key, opt_label || MSG_TR_DIALOG_OK, true);
  this.buttonHandlers_[key] = goog.bind(this.editorDialog_.handleOk,
                                        this.editorDialog_);
  return this;
};


/**
 * Adds a Cancel button to the dialog. Clicking this button will cause {@link
 * handleCancel} to run, subsequently dispatching a CANCEL event.
 * @param {string=} opt_label The caption for the button, if not "Cancel".
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.addCancelButton =
    function(opt_label) {
  var key = goog.ui.Dialog.DefaultButtonKeys.CANCEL;
  /** @desc Label for a cancel button in an editor dialog. */
  var MSG_TR_DIALOG_CANCEL = goog.getMsg('Cancel');
  // False means it's not the OK button, true means it's the Cancel button.
  this.buttonSet_.set(key, opt_label || MSG_TR_DIALOG_CANCEL, false, true);
  this.buttonHandlers_[key] = goog.bind(this.editorDialog_.handleCancel,
                                        this.editorDialog_);
  return this;
};


/**
 * Adds a custom button to the dialog.
 * @param {string} label The caption for the button.
 * @param {function(goog.ui.Dialog.EventType):*} handler Function called when
 *     the button is clicked. It is recommended that this function be a method
 *     in the concrete subclass of AbstractDialog using this Builder, and that
 *     it dispatch an event (see {@link handleOk}).
 * @param {string=} opt_buttonId Identifier to be used to access the button when
 *     calling AbstractDialog.getButtonElement().
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.addButton =
    function(label, handler, opt_buttonId) {
  // We don't care what the key is, just that we can match the button with the
  // handler function later.
  var key = opt_buttonId || goog.string.createUniqueString();
  this.buttonSet_.set(key, label);
  this.buttonHandlers_[key] = handler;
  return this;
};


/**
 * Puts a CSS class on the dialog's main element.
 * @param {string} className The class to add.
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.addClassName =
    function(className) {
  goog.dom.classes.add(this.wrappedDialog_.getDialogElement(), className);
  return this;
};


/**
 * Sets the content element of the dialog.
 * @param {Element} contentElem An element for the main body.
 * @return {goog.ui.editor.AbstractDialog.Builder} This.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.setContent =
    function(contentElem) {
  goog.dom.appendChild(this.wrappedDialog_.getContentElement(), contentElem);
  return this;
};


/**
 * Builds the wrapped dialog control. May only be called once, after which
 * no more methods may be called on this builder.
 * @return {goog.ui.Dialog} The wrapped dialog control.
 */
goog.ui.editor.AbstractDialog.Builder.prototype.build = function() {
  if (this.buttonSet_.isEmpty()) {
    // If caller didn't set any buttons, add an OK and Cancel button by default.
    this.addOkButton();
    this.addCancelButton();
  }
  this.wrappedDialog_.setButtonSet(this.buttonSet_);

  var handlers = this.buttonHandlers_;
  this.buttonHandlers_ = null;
  this.wrappedDialog_.addEventListener(goog.ui.Dialog.EventType.SELECT,
      // Listen for the SELECT event, which means a button was clicked, and
      // call the handler associated with that button via the key property.
      function(e) {
        if (handlers[e.key]) {
          return handlers[e.key](e);
        }
      });

  // All editor dialogs are modal.
  this.wrappedDialog_.setModal(true);

  var dialog = this.wrappedDialog_;
  this.wrappedDialog_ = null;
  return dialog;
};


/**
 * Editor dialog that will wrap the wrapped dialog this builder will create.
 * @type {goog.ui.editor.AbstractDialog}
 * @private
 */
goog.ui.editor.AbstractDialog.Builder.prototype.editorDialog_;


/**
 * wrapped dialog control being built by this builder.
 * @type {goog.ui.Dialog}
 * @private
 */
goog.ui.editor.AbstractDialog.Builder.prototype.wrappedDialog_;


/**
 * Set of buttons to be added to the wrapped dialog control.
 * @type {goog.ui.Dialog.ButtonSet}
 * @private
 */
goog.ui.editor.AbstractDialog.Builder.prototype.buttonSet_;


/**
 * Map from keys that will be returned in the wrapped dialog SELECT events to
 * handler functions to be called to handle those events.
 * @type {Object}
 * @private
 */
goog.ui.editor.AbstractDialog.Builder.prototype.buttonHandlers_;


// *** Protected interface ************************************************** //


/**
 * The DOM helper for the parent document.
 * @type {goog.dom.DomHelper}
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.dom;


/**
 * Creates and returns the goog.ui.Dialog control that is being wrapped
 * by this object.
 * @return {goog.ui.Dialog} Created Dialog control.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.createDialogControl =
    goog.abstractMethod;


/**
 * Returns the HTML Button element for the OK button in this dialog.
 * @return {Element} The button element if found, else null.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.getOkButtonElement = function() {
  return this.getButtonElement(goog.ui.Dialog.DefaultButtonKeys.OK);
};


/**
 * Returns the HTML Button element for the Cancel button in this dialog.
 * @return {Element} The button element if found, else null.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.getCancelButtonElement = function() {
  return this.getButtonElement(goog.ui.Dialog.DefaultButtonKeys.CANCEL);
};


/**
 * Returns the HTML Button element for the button added to this dialog with
 * the given button id.
 * @param {string} buttonId The id of the button to get.
 * @return {Element} The button element if found, else null.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.getButtonElement = function(buttonId) {
  return this.dialogInternal_.getButtonSet().getButton(buttonId);
};


/**
 * Creates and returns the event object to be used when dispatching the OK
 * event to listeners, or returns null to prevent the dialog from closing.
 * Subclasses should override this to return their own subclass of
 * goog.events.Event that includes all data a plugin would need from the dialog.
 * @param {goog.events.Event} e The event object dispatched by the wrapped
 *     dialog.
 * @return {goog.events.Event} The event object to be used when dispatching the
 *     OK event to listeners.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.createOkEvent = goog.abstractMethod;


/**
 * Handles the event dispatched by the wrapped dialog control when the user
 * clicks the OK button. Attempts to create the OK event object and dispatches
 * it if successful.
 * @param {goog.ui.Dialog.Event} e wrapped dialog OK event object.
 * @return {boolean} Whether the default action (closing the dialog) should
 *     still be executed. This will be false if the OK event could not be
 *     created to be dispatched, or if any listener to that event returs false
 *     or calls preventDefault.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.handleOk = function(e) {
  var eventObj = this.createOkEvent(e);
  if (eventObj) {
    return this.dispatchEvent(eventObj);
  } else {
    return false;
  }
};


/**
 * Handles the event dispatched by the wrapped dialog control when the user
 * clicks the Cancel button. Simply dispatches a CANCEL event.
 * @return {boolean} Returns false if any of the handlers called prefentDefault
 *     on the event or returned false themselves.
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.handleCancel = function() {
  return this.dispatchEvent(goog.ui.editor.AbstractDialog.EventType.CANCEL);
};


/**
 * Disposes of the dialog. If the dialog is open, it will be hidden and
 * AFTER_HIDE will be dispatched.
 * @override
 * @protected
 */
goog.ui.editor.AbstractDialog.prototype.disposeInternal = function() {
  if (this.dialogInternal_) {
    this.hide();

    this.dialogInternal_.dispose();
    this.dialogInternal_ = null;
  }

  goog.ui.editor.AbstractDialog.superClass_.disposeInternal.call(this);
};


// *** Private implementation *********************************************** //


/**
 * The wrapped dialog widget.
 * @type {goog.ui.Dialog}
 * @private
 */
goog.ui.editor.AbstractDialog.prototype.dialogInternal_;


/**
 * Cleans up after the dialog is hidden and fires the AFTER_HIDE event. Should
 * be a listener for the wrapped dialog's AFTER_HIDE event.
 * @private
 */
goog.ui.editor.AbstractDialog.prototype.handleAfterHide_ = function() {
  this.dispatchEvent(goog.ui.editor.AbstractDialog.EventType.AFTER_HIDE);
};