| 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 plugin. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.demos.editor.HelloWorldDialogPlugin'); |
| 21 |
goog.provide('goog.demos.editor.HelloWorldDialogPlugin.Command'); |
| 22 |
|
| 23 |
goog.require('goog.demos.editor.HelloWorldDialog'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.editor.plugins.AbstractDialogPlugin'); |
| 26 |
goog.require('goog.editor.range'); |
| 27 |
goog.require('goog.functions'); |
| 28 |
goog.require('goog.ui.editor.AbstractDialog'); |
| 29 |
|
| 30 |
|
| 31 |
// *** Public interface ***************************************************** // |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* A plugin that opens the hello world dialog. |
| 37 |
* @constructor |
| 38 |
* @extends {goog.editor.plugins.AbstractDialogPlugin} |
| 39 |
* @final |
| 40 |
*/ |
| 41 |
goog.demos.editor.HelloWorldDialogPlugin = function() { |
| 42 |
goog.editor.plugins.AbstractDialogPlugin.call( |
| 43 |
this, |
| 44 |
goog.demos.editor.HelloWorldDialogPlugin.Command.HELLO_WORLD_DIALOG); |
| 45 |
}; |
| 46 |
goog.inherits( |
| 47 |
goog.demos.editor.HelloWorldDialogPlugin, |
| 48 |
goog.editor.plugins.AbstractDialogPlugin); |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Commands implemented by this plugin. |
| 53 |
* @enum {string} |
| 54 |
*/ |
| 55 |
goog.demos.editor.HelloWorldDialogPlugin.Command = { |
| 56 |
HELLO_WORLD_DIALOG: 'helloWorldDialog' |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
/** @override */ |
| 61 |
goog.demos.editor.HelloWorldDialogPlugin.prototype.getTrogClassId = |
| 62 |
goog.functions.constant('HelloWorldDialog'); |
| 63 |
|
| 64 |
|
| 65 |
// *** Protected interface ************************************************** // |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Creates a new instance of the dialog and registers for the relevant events. |
| 70 |
* @param {goog.dom.DomHelper} dialogDomHelper The dom helper to be used to |
| 71 |
* create the dialog. |
| 72 |
* @return {goog.demos.editor.HelloWorldDialog} The dialog. |
| 73 |
* @override |
| 74 |
* @protected |
| 75 |
*/ |
| 76 |
goog.demos.editor.HelloWorldDialogPlugin.prototype.createDialog = function( |
| 77 |
dialogDomHelper) { |
| 78 |
var dialog = new goog.demos.editor.HelloWorldDialog(dialogDomHelper); |
| 79 |
dialog.addEventListener( |
| 80 |
goog.ui.editor.AbstractDialog.EventType.OK, this.handleOk_, false, this); |
| 81 |
return dialog; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
// *** Private implementation *********************************************** // |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Handles the OK event from the dialog by inserting the hello world message |
| 90 |
* into the field. |
| 91 |
* @param {goog.demos.editor.HelloWorldDialog.OkEvent} e OK event object. |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
goog.demos.editor.HelloWorldDialogPlugin.prototype.handleOk_ = function(e) { |
| 95 |
// First restore the selection so we can manipulate the field's content |
| 96 |
// according to what was selected. |
| 97 |
this.restoreOriginalSelection(); |
| 98 |
|
| 99 |
// Notify listeners that the field's contents are about to change. |
| 100 |
this.getFieldObject().dispatchBeforeChange(); |
| 101 |
|
| 102 |
// Now we can clear out what was previously selected (if anything). |
| 103 |
var range = this.getFieldObject().getRange(); |
| 104 |
range.removeContents(); |
| 105 |
// And replace it with a span containing our hello world message. |
| 106 |
var createdNode = this.getFieldDomHelper().createDom( |
| 107 |
goog.dom.TagName.SPAN, null, e.message); |
| 108 |
createdNode = range.insertNode(createdNode, false); |
| 109 |
// Place the cursor at the end of the new text node (false == to the right). |
| 110 |
goog.editor.range.placeCursorNextTo(createdNode, false); |
| 111 |
|
| 112 |
// Notify listeners that the field's selection has changed. |
| 113 |
this.getFieldObject().dispatchSelectionChangeEvent(); |
| 114 |
// Notify listeners that the field's contents have changed. |
| 115 |
this.getFieldObject().dispatchChange(); |
| 116 |
}; |