aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/editor/equationeditordialog.js
blob: 579234f9a225ef9e74d94d8c5fcefa6585ed000d (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
// Copyright 2011 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.

goog.provide('goog.ui.editor.EquationEditorDialog');

goog.require('goog.editor.Command');
goog.require('goog.ui.editor.AbstractDialog');
goog.require('goog.ui.editor.EquationEditorOkEvent');
goog.require('goog.ui.equation.ChangeEvent');
goog.require('goog.ui.equation.TexEditor');



/**
 * Equation editor dialog (based on goog.ui.editor.AbstractDialog).
 * @param {Object} context The context that this dialog runs in.
 * @param {goog.dom.DomHelper} domHelper DomHelper to be used to create the
 *     dialog's dom structure.
 * @param {string} equation Initial equation.
 * @param {string} helpUrl URL pointing to help documentation.
 * @constructor
 * @extends {goog.ui.editor.AbstractDialog}
 */
goog.ui.editor.EquationEditorDialog = function(context, domHelper,
    equation, helpUrl) {
  goog.ui.editor.AbstractDialog.call(this, domHelper);
  this.equationEditor_ =
      new goog.ui.equation.TexEditor(context, helpUrl);
  this.equationEditor_.render();
  this.equationEditor_.setEquation(equation);
  this.equationEditor_.addEventListener(goog.editor.Command.EQUATION,
      this.onChange_, false, this);
};
goog.inherits(goog.ui.editor.EquationEditorDialog,
    goog.ui.editor.AbstractDialog);


/**
 * The equation editor actual UI.
 * @type {goog.ui.equation.TexEditor}
 * @private
 */
goog.ui.editor.EquationEditorDialog.prototype.equationEditor_;


/**
 * The dialog's OK button element.
 * @type {Element?}
 * @private
 */
goog.ui.editor.EquationEditorDialog.prototype.okButton_;


/** @override */
goog.ui.editor.EquationEditorDialog.prototype.createDialogControl =
    function() {
  var builder = new goog.ui.editor.AbstractDialog.Builder(this);

  /**
   * @desc The title of the equation editor dialog.
   */
  var MSG_EE_DIALOG_TITLE = goog.getMsg('Equation Editor');

  /**
   * @desc Button label for the equation editor dialog for adding
   * a new equation.
   */
  var MSG_EE_BUTTON_SAVE_NEW = goog.getMsg('Insert equation');

  /**
   * @desc Button label for the equation editor dialog for saving
   * a modified equation.
   */
  var MSG_EE_BUTTON_SAVE_MODIFY = goog.getMsg('Save changes');

  var okButtonText = this.equationEditor_.getEquation() ?
      MSG_EE_BUTTON_SAVE_MODIFY : MSG_EE_BUTTON_SAVE_NEW;

  builder.setTitle(MSG_EE_DIALOG_TITLE)
    .setContent(this.equationEditor_.getElement())
    .addOkButton(okButtonText)
    .addCancelButton();

  return builder.build();
};


/**
 * @override
 */
goog.ui.editor.EquationEditorDialog.prototype.createOkEvent = function(e) {
  if (this.equationEditor_.isValid()) {
    // Equation is not valid, don't close the dialog.
    return null;
  }
  var equationHtml = this.equationEditor_.getHtml();
  return new goog.ui.editor.EquationEditorOkEvent(equationHtml);
};


/**
 * Handles CHANGE event fired when user changes equation.
 * @param {goog.ui.equation.ChangeEvent} e The event object.
 * @private
 */
goog.ui.editor.EquationEditorDialog.prototype.onChange_ = function(e) {
  if (!this.okButton_) {
    this.okButton_ = this.getButtonElement(
        goog.ui.Dialog.DefaultButtonKeys.OK);
  }
  this.okButton_.disabled = !e.isValid;
};