prompt.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright 2006 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 DHTML prompt to replace javascript's prompt().
  16. *
  17. * @see ../demos/prompt.html
  18. */
  19. goog.provide('goog.ui.Prompt');
  20. goog.require('goog.Timer');
  21. goog.require('goog.dom');
  22. goog.require('goog.dom.InputType');
  23. goog.require('goog.dom.TagName');
  24. goog.require('goog.events');
  25. goog.require('goog.events.EventType');
  26. goog.require('goog.functions');
  27. goog.require('goog.html.SafeHtml');
  28. goog.require('goog.ui.Component');
  29. goog.require('goog.ui.Dialog');
  30. goog.require('goog.userAgent');
  31. /**
  32. * Creates an object that represents a prompt (used in place of javascript's
  33. * prompt). The html structure of the prompt is the same as the layout for
  34. * dialog.js except for the addition of a text box which is placed inside the
  35. * "Content area" and has the default class-name 'modal-dialog-userInput'
  36. *
  37. * @param {string} promptTitle The title of the prompt.
  38. * @param {string|!goog.html.SafeHtml} promptBody The body of the prompt.
  39. * String is treated as plain text and it will be HTML-escaped.
  40. * @param {Function} callback The function to call when the user selects Ok or
  41. * Cancel. The function should expect a single argument which represents
  42. * what the user entered into the prompt. If the user presses cancel, the
  43. * value of the argument will be null.
  44. * @param {string=} opt_defaultValue Optional default value that should be in
  45. * the text box when the prompt appears.
  46. * @param {string=} opt_class Optional prefix for the classes.
  47. * @param {boolean=} opt_useIframeForIE For IE, workaround windowed controls
  48. * z-index issue by using a an iframe instead of a div for bg element.
  49. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper; see {@link
  50. * goog.ui.Component} for semantics.
  51. * @constructor
  52. * @extends {goog.ui.Dialog}
  53. */
  54. goog.ui.Prompt = function(
  55. promptTitle, promptBody, callback, opt_defaultValue, opt_class,
  56. opt_useIframeForIE, opt_domHelper) {
  57. goog.ui.Prompt.base(
  58. this, 'constructor', opt_class, opt_useIframeForIE, opt_domHelper);
  59. /**
  60. * The id of the input element.
  61. * @type {string}
  62. * @private
  63. */
  64. this.inputElementId_ = this.makeId('ie');
  65. this.setTitle(promptTitle);
  66. var label = goog.html.SafeHtml.create(
  67. 'label', {'for': this.inputElementId_},
  68. goog.html.SafeHtml.htmlEscapePreservingNewlines(promptBody));
  69. var br = goog.html.SafeHtml.BR;
  70. this.setSafeHtmlContent(goog.html.SafeHtml.concat(label, br, br));
  71. this.callback_ = callback;
  72. this.defaultValue_ = goog.isDef(opt_defaultValue) ? opt_defaultValue : '';
  73. /** @desc label for a dialog button. */
  74. var MSG_PROMPT_OK = goog.getMsg('OK');
  75. /** @desc label for a dialog button. */
  76. var MSG_PROMPT_CANCEL = goog.getMsg('Cancel');
  77. var buttonSet = new goog.ui.Dialog.ButtonSet(opt_domHelper);
  78. buttonSet.set(goog.ui.Dialog.DefaultButtonKeys.OK, MSG_PROMPT_OK, true);
  79. buttonSet.set(
  80. goog.ui.Dialog.DefaultButtonKeys.CANCEL, MSG_PROMPT_CANCEL, false, true);
  81. this.setButtonSet(buttonSet);
  82. };
  83. goog.inherits(goog.ui.Prompt, goog.ui.Dialog);
  84. goog.tagUnsealableClass(goog.ui.Prompt);
  85. /**
  86. * Callback function which is invoked with the response to the prompt
  87. * @type {Function}
  88. * @private
  89. */
  90. goog.ui.Prompt.prototype.callback_ = goog.nullFunction;
  91. /**
  92. * Default value to display in prompt window
  93. * @type {string}
  94. * @private
  95. */
  96. goog.ui.Prompt.prototype.defaultValue_ = '';
  97. /**
  98. * Element in which user enters response (HTML <input> text box)
  99. * @type {?HTMLInputElement|?HTMLTextAreaElement}
  100. * @private
  101. */
  102. goog.ui.Prompt.prototype.userInputEl_ = null;
  103. /**
  104. * Tracks whether the prompt is in the process of closing to prevent multiple
  105. * calls to the callback when the user presses enter.
  106. * @type {boolean}
  107. * @private
  108. */
  109. goog.ui.Prompt.prototype.isClosing_ = false;
  110. /**
  111. * Number of rows in the user input element.
  112. * The default is 1 which means use an <input> element.
  113. * @type {number}
  114. * @private
  115. */
  116. goog.ui.Prompt.prototype.rows_ = 1;
  117. /**
  118. * Number of cols in the user input element.
  119. * The default is 0 which means use browser default.
  120. * @type {number}
  121. * @private
  122. */
  123. goog.ui.Prompt.prototype.cols_ = 0;
  124. /**
  125. * The input decorator function.
  126. * @type {function(Element)?}
  127. * @private
  128. */
  129. goog.ui.Prompt.prototype.inputDecoratorFn_ = null;
  130. /**
  131. * A validation function that takes a string and returns true if the string is
  132. * accepted, false otherwise.
  133. * @type {function(string):boolean}
  134. * @private
  135. */
  136. goog.ui.Prompt.prototype.validationFn_ = goog.functions.TRUE;
  137. /**
  138. * Sets the validation function that takes a string and returns true if the
  139. * string is accepted, false otherwise.
  140. * @param {function(string): boolean} fn The validation function to use on user
  141. * input.
  142. */
  143. goog.ui.Prompt.prototype.setValidationFunction = function(fn) {
  144. this.validationFn_ = fn;
  145. };
  146. /** @override */
  147. goog.ui.Prompt.prototype.enterDocument = function() {
  148. if (this.inputDecoratorFn_) {
  149. this.inputDecoratorFn_(this.userInputEl_);
  150. }
  151. goog.ui.Prompt.superClass_.enterDocument.call(this);
  152. this.getHandler().listen(
  153. this, goog.ui.Dialog.EventType.SELECT, this.onPromptExit_);
  154. this.getHandler().listen(
  155. this.userInputEl_,
  156. [goog.events.EventType.KEYUP, goog.events.EventType.CHANGE],
  157. this.handleInputChanged_);
  158. };
  159. /**
  160. * @return {?HTMLInputElement|?HTMLTextAreaElement} The user input element. May
  161. * be null if the Prompt has not been rendered.
  162. */
  163. goog.ui.Prompt.prototype.getInputElement = function() {
  164. return this.userInputEl_;
  165. };
  166. /**
  167. * Sets an input decorator function. This function will be called in
  168. * #enterDocument and will be passed the input element. This is useful for
  169. * attaching handlers to the input element for specific change events,
  170. * for example.
  171. * @param {function(Element)} inputDecoratorFn A function to call on the input
  172. * element on #enterDocument.
  173. */
  174. goog.ui.Prompt.prototype.setInputDecoratorFn = function(inputDecoratorFn) {
  175. this.inputDecoratorFn_ = inputDecoratorFn;
  176. };
  177. /**
  178. * Set the number of rows in the user input element.
  179. * A values of 1 means use an `<input>` element. If the prompt is already
  180. * rendered then you cannot change from `<input>` to `<textarea>` or vice versa.
  181. * @param {number} rows Number of rows for user input element.
  182. * @throws {goog.ui.Component.Error.ALREADY_RENDERED} If the component is
  183. * already rendered and an attempt to change between `<input>` and
  184. * `<textarea>` is made.
  185. */
  186. goog.ui.Prompt.prototype.setRows = function(rows) {
  187. if (this.isInDocument()) {
  188. if (this.userInputEl_.tagName == goog.dom.TagName.INPUT) {
  189. if (rows > 1) {
  190. throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
  191. }
  192. } else {
  193. if (rows <= 1) {
  194. throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
  195. }
  196. this.userInputEl_.rows = rows;
  197. }
  198. }
  199. this.rows_ = rows;
  200. };
  201. /**
  202. * @return {number} The number of rows in the user input element.
  203. */
  204. goog.ui.Prompt.prototype.getRows = function() {
  205. return this.rows_;
  206. };
  207. /**
  208. * Set the number of cols in the user input element.
  209. * @param {number} cols Number of cols for user input element.
  210. */
  211. goog.ui.Prompt.prototype.setCols = function(cols) {
  212. this.cols_ = cols;
  213. if (this.userInputEl_) {
  214. if (this.userInputEl_.tagName == goog.dom.TagName.INPUT) {
  215. this.userInputEl_.size = cols;
  216. } else {
  217. this.userInputEl_.cols = cols;
  218. }
  219. }
  220. };
  221. /**
  222. * @return {number} The number of cols in the user input element.
  223. */
  224. goog.ui.Prompt.prototype.getCols = function() {
  225. return this.cols_;
  226. };
  227. /**
  228. * Create the initial DOM representation for the prompt.
  229. * @override
  230. */
  231. goog.ui.Prompt.prototype.createDom = function() {
  232. goog.ui.Prompt.superClass_.createDom.call(this);
  233. var cls = this.getClass();
  234. // add input box to the content
  235. if (this.rows_ == 1) {
  236. // If rows == 1 then use an input element.
  237. this.userInputEl_ = this.getDomHelper().createDom(goog.dom.TagName.INPUT, {
  238. 'className': goog.getCssName(cls, 'userInput'),
  239. 'value': this.defaultValue_
  240. });
  241. this.userInputEl_.type = goog.dom.InputType.TEXT;
  242. if (this.cols_) {
  243. this.userInputEl_.size = this.cols_;
  244. }
  245. } else {
  246. // If rows > 1 then use a textarea.
  247. this.userInputEl_ =
  248. this.getDomHelper().createDom(goog.dom.TagName.TEXTAREA, {
  249. 'className': goog.getCssName(cls, 'userInput'),
  250. 'value': this.defaultValue_
  251. });
  252. this.userInputEl_.rows = this.rows_;
  253. if (this.cols_) {
  254. this.userInputEl_.cols = this.cols_;
  255. }
  256. }
  257. this.userInputEl_.id = this.inputElementId_;
  258. var contentEl = this.getContentElement();
  259. contentEl.appendChild(
  260. this.getDomHelper().createDom(
  261. goog.dom.TagName.DIV, {'style': 'overflow: auto'},
  262. this.userInputEl_));
  263. };
  264. /**
  265. * Handles input change events on the input field. Disables the OK button if
  266. * validation fails on the new input value.
  267. * @private
  268. */
  269. goog.ui.Prompt.prototype.handleInputChanged_ = function() {
  270. this.updateOkButtonState_();
  271. };
  272. /**
  273. * Set OK button enabled/disabled state based on input.
  274. * @private
  275. */
  276. goog.ui.Prompt.prototype.updateOkButtonState_ = function() {
  277. var enableOkButton = this.validationFn_(this.userInputEl_.value);
  278. var buttonSet = this.getButtonSet();
  279. buttonSet.setButtonEnabled(
  280. goog.ui.Dialog.DefaultButtonKeys.OK, enableOkButton);
  281. };
  282. /**
  283. * Causes the prompt to appear, centered on the screen, gives focus
  284. * to the text box, and selects the text
  285. * @param {boolean} visible Whether the dialog should be visible.
  286. * @override
  287. */
  288. goog.ui.Prompt.prototype.setVisible = function(visible) {
  289. goog.ui.Prompt.base(this, 'setVisible', visible);
  290. if (visible) {
  291. this.isClosing_ = false;
  292. this.userInputEl_.value = this.defaultValue_;
  293. this.focus();
  294. this.updateOkButtonState_();
  295. }
  296. };
  297. /**
  298. * Overrides setFocus to put focus on the input element.
  299. * @override
  300. */
  301. goog.ui.Prompt.prototype.focus = function() {
  302. goog.ui.Prompt.base(this, 'focus');
  303. if (goog.userAgent.OPERA) {
  304. // select() doesn't focus <input> elements in Opera.
  305. this.userInputEl_.focus();
  306. }
  307. this.userInputEl_.select();
  308. };
  309. /**
  310. * Sets the default value of the prompt when it is displayed.
  311. * @param {string} defaultValue The default value to display.
  312. */
  313. goog.ui.Prompt.prototype.setDefaultValue = function(defaultValue) {
  314. this.defaultValue_ = defaultValue;
  315. };
  316. /**
  317. * Handles the closing of the prompt, invoking the callback function that was
  318. * registered to handle the value returned by the prompt.
  319. * @param {goog.ui.Dialog.Event} e The dialog's selection event.
  320. * @private
  321. */
  322. goog.ui.Prompt.prototype.onPromptExit_ = function(e) {
  323. /*
  324. * The timeouts below are required for one edge case. If after the dialog
  325. * hides, suppose validation of the input fails which displays an alert. If
  326. * the user pressed the Enter key to dismiss the alert that was displayed it
  327. * can trigger the event handler a second time. This timeout ensures that the
  328. * alert is displayed only after the prompt is able to clean itself up.
  329. */
  330. if (!this.isClosing_) {
  331. this.isClosing_ = true;
  332. if (e.key == 'ok') {
  333. goog.Timer.callOnce(
  334. goog.bind(this.callback_, this, this.userInputEl_.value), 1);
  335. } else {
  336. goog.Timer.callOnce(goog.bind(this.callback_, this, null), 1);
  337. }
  338. }
  339. };
  340. /** @override */
  341. goog.ui.Prompt.prototype.disposeInternal = function() {
  342. goog.dom.removeNode(this.userInputEl_);
  343. goog.events.unlisten(
  344. this, goog.ui.Dialog.EventType.SELECT, this.onPromptExit_, true, this);
  345. goog.ui.Prompt.superClass_.disposeInternal.call(this);
  346. this.userInputEl_ = null;
  347. };