PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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