123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- goog.provide('goog.ui.Prompt');
- goog.require('goog.Timer');
- goog.require('goog.dom');
- goog.require('goog.dom.InputType');
- goog.require('goog.dom.TagName');
- goog.require('goog.events');
- goog.require('goog.events.EventType');
- goog.require('goog.functions');
- goog.require('goog.html.SafeHtml');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.Dialog');
- goog.require('goog.userAgent');
- goog.ui.Prompt = function(
- promptTitle, promptBody, callback, opt_defaultValue, opt_class,
- opt_useIframeForIE, opt_domHelper) {
- goog.ui.Prompt.base(
- this, 'constructor', opt_class, opt_useIframeForIE, opt_domHelper);
-
- this.inputElementId_ = this.makeId('ie');
- this.setTitle(promptTitle);
- var label = goog.html.SafeHtml.create(
- 'label', {'for': this.inputElementId_},
- goog.html.SafeHtml.htmlEscapePreservingNewlines(promptBody));
- var br = goog.html.SafeHtml.BR;
- this.setSafeHtmlContent(goog.html.SafeHtml.concat(label, br, br));
- this.callback_ = callback;
- this.defaultValue_ = goog.isDef(opt_defaultValue) ? opt_defaultValue : '';
-
- var MSG_PROMPT_OK = goog.getMsg('OK');
-
- var MSG_PROMPT_CANCEL = goog.getMsg('Cancel');
- var buttonSet = new goog.ui.Dialog.ButtonSet(opt_domHelper);
- buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.OK, MSG_PROMPT_OK, true);
- buttonSet.set(
- goog.ui.Dialog.DefaultButtonKeys.CANCEL, MSG_PROMPT_CANCEL, false, true);
- this.setButtonSet(buttonSet);
- };
- goog.inherits(goog.ui.Prompt, goog.ui.Dialog);
- goog.tagUnsealableClass(goog.ui.Prompt);
- goog.ui.Prompt.prototype.callback_ = goog.nullFunction;
- goog.ui.Prompt.prototype.defaultValue_ = '';
- goog.ui.Prompt.prototype.userInputEl_ = null;
- goog.ui.Prompt.prototype.isClosing_ = false;
- goog.ui.Prompt.prototype.rows_ = 1;
- goog.ui.Prompt.prototype.cols_ = 0;
- goog.ui.Prompt.prototype.inputDecoratorFn_ = null;
- goog.ui.Prompt.prototype.validationFn_ = goog.functions.TRUE;
- goog.ui.Prompt.prototype.setValidationFunction = function(fn) {
- this.validationFn_ = fn;
- };
- goog.ui.Prompt.prototype.enterDocument = function() {
- if (this.inputDecoratorFn_) {
- this.inputDecoratorFn_(this.userInputEl_);
- }
- goog.ui.Prompt.superClass_.enterDocument.call(this);
- this.getHandler().listen(
- this, goog.ui.Dialog.EventType.SELECT, this.onPromptExit_);
- this.getHandler().listen(
- this.userInputEl_,
- [goog.events.EventType.KEYUP, goog.events.EventType.CHANGE],
- this.handleInputChanged_);
- };
- goog.ui.Prompt.prototype.getInputElement = function() {
- return this.userInputEl_;
- };
- goog.ui.Prompt.prototype.setInputDecoratorFn = function(inputDecoratorFn) {
- this.inputDecoratorFn_ = inputDecoratorFn;
- };
- goog.ui.Prompt.prototype.setRows = function(rows) {
- if (this.isInDocument()) {
- if (this.userInputEl_.tagName == goog.dom.TagName.INPUT) {
- if (rows > 1) {
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- }
- } else {
- if (rows <= 1) {
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- }
- this.userInputEl_.rows = rows;
- }
- }
- this.rows_ = rows;
- };
- goog.ui.Prompt.prototype.getRows = function() {
- return this.rows_;
- };
- goog.ui.Prompt.prototype.setCols = function(cols) {
- this.cols_ = cols;
- if (this.userInputEl_) {
- if (this.userInputEl_.tagName == goog.dom.TagName.INPUT) {
- this.userInputEl_.size = cols;
- } else {
- this.userInputEl_.cols = cols;
- }
- }
- };
- goog.ui.Prompt.prototype.getCols = function() {
- return this.cols_;
- };
- goog.ui.Prompt.prototype.createDom = function() {
- goog.ui.Prompt.superClass_.createDom.call(this);
- var cls = this.getClass();
-
- if (this.rows_ == 1) {
-
- this.userInputEl_ = this.getDomHelper().createDom(goog.dom.TagName.INPUT, {
- 'className': goog.getCssName(cls, 'userInput'),
- 'value': this.defaultValue_
- });
- this.userInputEl_.type = goog.dom.InputType.TEXT;
- if (this.cols_) {
- this.userInputEl_.size = this.cols_;
- }
- } else {
-
- this.userInputEl_ =
- this.getDomHelper().createDom(goog.dom.TagName.TEXTAREA, {
- 'className': goog.getCssName(cls, 'userInput'),
- 'value': this.defaultValue_
- });
- this.userInputEl_.rows = this.rows_;
- if (this.cols_) {
- this.userInputEl_.cols = this.cols_;
- }
- }
- this.userInputEl_.id = this.inputElementId_;
- var contentEl = this.getContentElement();
- contentEl.appendChild(
- this.getDomHelper().createDom(
- goog.dom.TagName.DIV, {'style': 'overflow: auto'},
- this.userInputEl_));
- };
- goog.ui.Prompt.prototype.handleInputChanged_ = function() {
- this.updateOkButtonState_();
- };
- goog.ui.Prompt.prototype.updateOkButtonState_ = function() {
- var enableOkButton = this.validationFn_(this.userInputEl_.value);
- var buttonSet = this.getButtonSet();
- buttonSet.setButtonEnabled(
- goog.ui.Dialog.DefaultButtonKeys.OK, enableOkButton);
- };
- goog.ui.Prompt.prototype.setVisible = function(visible) {
- goog.ui.Prompt.base(this, 'setVisible', visible);
- if (visible) {
- this.isClosing_ = false;
- this.userInputEl_.value = this.defaultValue_;
- this.focus();
- this.updateOkButtonState_();
- }
- };
- goog.ui.Prompt.prototype.focus = function() {
- goog.ui.Prompt.base(this, 'focus');
- if (goog.userAgent.OPERA) {
-
- this.userInputEl_.focus();
- }
- this.userInputEl_.select();
- };
- goog.ui.Prompt.prototype.setDefaultValue = function(defaultValue) {
- this.defaultValue_ = defaultValue;
- };
- goog.ui.Prompt.prototype.onPromptExit_ = function(e) {
-
- if (!this.isClosing_) {
- this.isClosing_ = true;
- if (e.key == 'ok') {
- goog.Timer.callOnce(
- goog.bind(this.callback_, this, this.userInputEl_.value), 1);
- } else {
- goog.Timer.callOnce(goog.bind(this.callback_, this, null), 1);
- }
- }
- };
- goog.ui.Prompt.prototype.disposeInternal = function() {
- goog.dom.removeNode(this.userInputEl_);
- goog.events.unlisten(
- this, goog.ui.Dialog.EventType.SELECT, this.onPromptExit_, true, this);
- goog.ui.Prompt.superClass_.disposeInternal.call(this);
- this.userInputEl_ = null;
- };
|