PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/media/qooxdoo-1.4-sdk/framework/source/class/qx/ui/form/DateField.js

https://github.com/vuuvv/vshop1
JavaScript | 396 lines | 175 code | 72 blank | 149 comment | 25 complexity | c76e8aa148b221f929e20c92d9f3b333 MD5 | raw file
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Martin Wittemann (martinwittemann)
  12. ************************************************************************ */
  13. /**
  14. * A *date field* is like a combo box with the date as popup. As button to
  15. * open the calendar a calendar icon is shown at the right to the textfield.
  16. *
  17. * To be conform with all form widgets, the {@link qx.ui.form.IForm} interface
  18. * is implemented.
  19. *
  20. * The following example creates a date field and sets the current
  21. * date as selected.
  22. *
  23. * <pre class='javascript'>
  24. * var dateField = new qx.ui.form.DateField();
  25. * this.getRoot().add(dateField, {top: 20, left: 20});
  26. * dateField.setValue(new Date());
  27. * </pre>
  28. *
  29. * @childControl list {qx.ui.control.DateChooser} date chooser component
  30. * @childControl popup {qx.ui.popup.Popup} popup which shows the list control
  31. */
  32. qx.Class.define("qx.ui.form.DateField",
  33. {
  34. extend : qx.ui.form.ComboBox,
  35. implement : [qx.ui.form.IDateForm],
  36. /*
  37. *****************************************************************************
  38. CONSTRUCTOR
  39. *****************************************************************************
  40. */
  41. construct : function()
  42. {
  43. this.base(arguments);
  44. // create a default date format
  45. this.setDateFormat(qx.ui.form.DateField.getDefaultDateFormatter());
  46. // listen for locale changes
  47. if (qx.core.Environment.get("qx.dynlocale"))
  48. {
  49. this.__localeListenerId =
  50. qx.locale.Manager.getInstance().addListener("changeLocale", function() {
  51. this.setDateFormat(qx.ui.form.DateField.getDefaultDateFormatter());
  52. }, this);
  53. }
  54. },
  55. /*
  56. *****************************************************************************
  57. PROPERTIES
  58. *****************************************************************************
  59. */
  60. properties :
  61. {
  62. // overridden
  63. appearance :
  64. {
  65. refine : true,
  66. init : "datefield"
  67. },
  68. /** The formatter, which converts the selected date to a string. **/
  69. dateFormat :
  70. {
  71. check : "qx.util.format.DateFormat",
  72. apply : "_applyDateFormat"
  73. }
  74. },
  75. /*
  76. *****************************************************************************
  77. MEMBERS
  78. *****************************************************************************
  79. */
  80. statics :
  81. {
  82. __dateFormat : null,
  83. __formatter : null,
  84. /**
  85. * Get the shared default date formatter
  86. *
  87. * @return {qx.util.format.DateFormat} The shared date formatter
  88. */
  89. getDefaultDateFormatter : function()
  90. {
  91. var format = qx.locale.Date.getDateFormat("medium").toString();
  92. if (format == this.__dateFormat) {
  93. return this.__formatter;
  94. }
  95. if (this.__formatter) {
  96. this.__formatter.dispose();
  97. }
  98. this.__formatter = new qx.util.format.DateFormat(format, qx.locale.Manager.getInstance().getLocale());
  99. this.__dateFormat = format;
  100. return this.__formatter;
  101. }
  102. },
  103. /*
  104. *****************************************************************************
  105. MEMBERS
  106. *****************************************************************************
  107. */
  108. members :
  109. {
  110. __localeListenerId : null,
  111. /*
  112. ---------------------------------------------------------------------------
  113. PUBLIC METHODS
  114. ---------------------------------------------------------------------------
  115. */
  116. /**
  117. * This method sets the date corresponding to the {@link #dateFormat} to the
  118. * date field. It will also select the date in the calendar popup.
  119. *
  120. * @param value {Date} The date to set.
  121. */
  122. setValue : function(value)
  123. {
  124. // set the date to the textfield
  125. var textField = this.getChildControl("textfield");
  126. textField.setValue(this.getDateFormat().format(value));
  127. // set the date in the datechooser
  128. var dateChooser = this.getChildControl("list");
  129. dateChooser.setValue(value);
  130. },
  131. /**
  132. * Returns the current set date corresponding to the {@link #dateFormat}.
  133. * If the given text could not be parsed, <code>null</code> will be returned.
  134. *
  135. * @return {Date} The currently set date.
  136. */
  137. getValue : function()
  138. {
  139. // get the value of the textfield
  140. var textfieldValue = this.getChildControl("textfield").getValue();
  141. // return the parsed date
  142. try {
  143. return this.getDateFormat().parse(textfieldValue);
  144. } catch (ex) {
  145. return null;
  146. }
  147. },
  148. /**
  149. * Resets the DateField. The textfield will be empty and the datechooser
  150. * will also have no selection.
  151. */
  152. resetValue: function()
  153. {
  154. // set the date to the textfield
  155. var textField = this.getChildControl("textfield");
  156. textField.setValue("");
  157. // set the date in the datechooser
  158. var dateChooser = this.getChildControl("list");
  159. dateChooser.setValue(null);
  160. },
  161. /*
  162. ---------------------------------------------------------------------------
  163. PROPERTY APPLY METHODS
  164. ---------------------------------------------------------------------------
  165. */
  166. // property apply routine
  167. _applyDateFormat : function(value, old)
  168. {
  169. // if old is undefined or null do nothing
  170. if (!old) {
  171. return;
  172. }
  173. // get the date with the old date format
  174. try
  175. {
  176. var textfield = this.getChildControl("textfield");
  177. var dateStr = textfield.getValue();
  178. var currentDate = old.parse(dateStr);
  179. textfield.setValue(value.format(currentDate));
  180. }
  181. catch (ex) {
  182. // do nothing if the former date could not be parsed
  183. }
  184. },
  185. /*
  186. ---------------------------------------------------------------------------
  187. WIDGET API
  188. ---------------------------------------------------------------------------
  189. */
  190. // overridden
  191. _createChildControlImpl : function(id, hash)
  192. {
  193. var control;
  194. switch(id)
  195. {
  196. case "list":
  197. control = new qx.ui.control.DateChooser();
  198. control.setFocusable(false);
  199. control.setKeepFocus(true);
  200. control.addListener("execute", this._onChangeDate, this);
  201. break;
  202. case "popup":
  203. control = new qx.ui.popup.Popup(new qx.ui.layout.VBox);
  204. control.setAutoHide(false);
  205. control.add(this.getChildControl("list"));
  206. control.addListener("mouseup", this._onChangeDate, this);
  207. control.addListener("changeVisibility", this._onPopupChangeVisibility, this);
  208. break;
  209. }
  210. return control || this.base(arguments, id);
  211. },
  212. /*
  213. ---------------------------------------------------------------------------
  214. EVENT LISTENERS
  215. ---------------------------------------------------------------------------
  216. */
  217. /**
  218. * Handler method which handles the click on the calender popup.
  219. *
  220. * @param e {qx.event.type.Mouse} The mouse event of the click.
  221. */
  222. _onChangeDate : function(e)
  223. {
  224. var textField = this.getChildControl("textfield");
  225. var selectedDate = this.getChildControl("list").getValue();
  226. textField.setValue(this.getDateFormat().format(selectedDate));
  227. this.close();
  228. },
  229. /**
  230. * Handler method which handles the key press. It forwards all key event
  231. * to the opened date chooser except the escape key event. Escape closes
  232. * the popup.
  233. * If the list is cloned, all key events will not be processed further.
  234. *
  235. * @param e {qx.event.type.KeySequence} Keypress event
  236. */
  237. _onKeyPress : function(e)
  238. {
  239. // get the key identifier
  240. var iden = e.getKeyIdentifier();
  241. if (iden == "Down" && e.isAltPressed())
  242. {
  243. this.toggle();
  244. e.stopPropagation();
  245. return;
  246. }
  247. // if the popup is closed, ignore all
  248. var popup = this.getChildControl("popup");
  249. if (popup.getVisibility() == "hidden") {
  250. return;
  251. }
  252. // hide the list always on escape
  253. if (iden == "Escape")
  254. {
  255. this.close();
  256. e.stopPropagation();
  257. return;
  258. }
  259. // Stop navigation keys when popup is open
  260. if (iden === "Left" || iden === "Right" || iden === "Down" || iden === "Up") {
  261. e.preventDefault();
  262. }
  263. // forward the rest of the events to the date chooser
  264. this.getChildControl("list").handleKeyPress(e);
  265. },
  266. // overridden
  267. _onPopupChangeVisibility : function(e)
  268. {
  269. // Synchronize the chooser with the current value on every
  270. // opening of the popup. This is needed when the value has been
  271. // modified and not saved yet (e.g. no blur)
  272. var popup = this.getChildControl("popup");
  273. if (popup.isVisible())
  274. {
  275. var chooser = this.getChildControl("list");
  276. var date = this.getValue();
  277. chooser.setValue(date);
  278. }
  279. },
  280. /**
  281. * Reacts on value changes of the text field and syncs the
  282. * value to the combobox.
  283. *
  284. * @param e {qx.event.type.Data} Change event
  285. */
  286. _onTextFieldChangeValue : function(e)
  287. {
  288. // Apply to popup
  289. var date = this.getValue();
  290. if (date != null)
  291. {
  292. var list = this.getChildControl("list");
  293. list.setValue(date);
  294. }
  295. // Fire event
  296. this.fireDataEvent("changeValue", this.getValue());
  297. },
  298. /**
  299. * Checks if the textfield of the DateField is empty.
  300. *
  301. * @return {Boolean} True, if the textfield of the DateField is empty.
  302. */
  303. isEmpty: function()
  304. {
  305. var value = this.getChildControl("textfield").getValue();
  306. return value == null || value == "";
  307. }
  308. },
  309. destruct : function() {
  310. // listen for locale changes
  311. if (qx.core.Environment.get("qx.dynlocale"))
  312. {
  313. if (this.__localeListenerId) {
  314. this.removeListenerById(this.__localeListenerId);
  315. }
  316. }
  317. }
  318. });