field_date.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2015 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Date input field.
  22. * @author pkendall64@gmail.com (Paul Kendall)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldDate');
  26. goog.require('Blockly.Field');
  27. goog.require('goog.date');
  28. goog.require('goog.dom');
  29. goog.require('goog.events');
  30. goog.require('goog.i18n.DateTimeSymbols');
  31. goog.require('goog.i18n.DateTimeSymbols_he');
  32. goog.require('goog.style');
  33. goog.require('goog.ui.DatePicker');
  34. /**
  35. * Class for a date input field.
  36. * @param {string} date The initial date.
  37. * @param {Function=} opt_validator A function that is executed when a new
  38. * date is selected. Its sole argument is the new date value. Its
  39. * return value becomes the selected date, unless it is undefined, in
  40. * which case the new date stands, or it is null, in which case the change
  41. * is aborted.
  42. * @extends {Blockly.Field}
  43. * @constructor
  44. */
  45. Blockly.FieldDate = function(date, opt_validator) {
  46. if (!date) {
  47. date = new goog.date.Date().toIsoString(true);
  48. }
  49. Blockly.FieldDate.superClass_.constructor.call(this, date, opt_validator);
  50. this.setValue(date);
  51. };
  52. goog.inherits(Blockly.FieldDate, Blockly.Field);
  53. /**
  54. * Mouse cursor style when over the hotspot that initiates the editor.
  55. */
  56. Blockly.FieldDate.prototype.CURSOR = 'text';
  57. /**
  58. * Close the colour picker if this input is being deleted.
  59. */
  60. Blockly.FieldDate.prototype.dispose = function() {
  61. Blockly.WidgetDiv.hideIfOwner(this);
  62. Blockly.FieldDate.superClass_.dispose.call(this);
  63. };
  64. /**
  65. * Return the current date.
  66. * @return {string} Current date.
  67. */
  68. Blockly.FieldDate.prototype.getValue = function() {
  69. return this.date_;
  70. };
  71. /**
  72. * Set the date.
  73. * @param {string} date The new date.
  74. */
  75. Blockly.FieldDate.prototype.setValue = function(date) {
  76. if (this.sourceBlock_) {
  77. var validated = this.callValidator(date);
  78. // If the new date is invalid, validation returns null.
  79. // In this case we still want to display the illegal result.
  80. if (validated !== null) {
  81. date = validated;
  82. }
  83. }
  84. this.date_ = date;
  85. Blockly.Field.prototype.setText.call(this, date);
  86. };
  87. /**
  88. * Create a date picker under the date field.
  89. * @private
  90. */
  91. Blockly.FieldDate.prototype.showEditor_ = function() {
  92. Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
  93. Blockly.FieldDate.widgetDispose_);
  94. // Create the date picker using Closure.
  95. Blockly.FieldDate.loadLanguage_();
  96. var picker = new goog.ui.DatePicker();
  97. picker.setAllowNone(false);
  98. picker.setShowWeekNum(false);
  99. // Position the picker to line up with the field.
  100. // Record windowSize and scrollOffset before adding the picker.
  101. var windowSize = goog.dom.getViewportSize();
  102. var scrollOffset = goog.style.getViewportPageOffset(document);
  103. var xy = this.getAbsoluteXY_();
  104. var borderBBox = this.getScaledBBox_();
  105. var div = Blockly.WidgetDiv.DIV;
  106. picker.render(div);
  107. picker.setDate(goog.date.fromIsoString(this.getValue()));
  108. // Record pickerSize after adding the date picker.
  109. var pickerSize = goog.style.getSize(picker.getElement());
  110. // Flip the picker vertically if off the bottom.
  111. if (xy.y + pickerSize.height + borderBBox.height >=
  112. windowSize.height + scrollOffset.y) {
  113. xy.y -= pickerSize.height - 1;
  114. } else {
  115. xy.y += borderBBox.height - 1;
  116. }
  117. if (this.sourceBlock_.RTL) {
  118. xy.x += borderBBox.width;
  119. xy.x -= pickerSize.width;
  120. // Don't go offscreen left.
  121. if (xy.x < scrollOffset.x) {
  122. xy.x = scrollOffset.x;
  123. }
  124. } else {
  125. // Don't go offscreen right.
  126. if (xy.x > windowSize.width + scrollOffset.x - pickerSize.width) {
  127. xy.x = windowSize.width + scrollOffset.x - pickerSize.width;
  128. }
  129. }
  130. Blockly.WidgetDiv.position(xy.x, xy.y, windowSize, scrollOffset,
  131. this.sourceBlock_.RTL);
  132. // Configure event handler.
  133. var thisField = this;
  134. Blockly.FieldDate.changeEventKey_ = goog.events.listen(picker,
  135. goog.ui.DatePicker.Events.CHANGE,
  136. function(event) {
  137. var date = event.date ? event.date.toIsoString(true) : '';
  138. Blockly.WidgetDiv.hide();
  139. if (thisField.sourceBlock_) {
  140. // Call any validation function, and allow it to override.
  141. date = thisField.callValidator(date);
  142. }
  143. thisField.setValue(date);
  144. });
  145. };
  146. /**
  147. * Hide the date picker.
  148. * @private
  149. */
  150. Blockly.FieldDate.widgetDispose_ = function() {
  151. if (Blockly.FieldDate.changeEventKey_) {
  152. goog.events.unlistenByKey(Blockly.FieldDate.changeEventKey_);
  153. }
  154. };
  155. /**
  156. * Load the best language pack by scanning the Blockly.Msg object for a
  157. * language that matches the available languages in Closure.
  158. * @private
  159. */
  160. Blockly.FieldDate.loadLanguage_ = function() {
  161. var reg = /^DateTimeSymbols_(.+)$/;
  162. for (var prop in goog.i18n) {
  163. var m = prop.match(reg);
  164. if (m) {
  165. var lang = m[1].toLowerCase().replace('_', '.'); // E.g. 'pt.br'
  166. if (goog.getObjectByName(lang, Blockly.Msg)) {
  167. goog.i18n.DateTimeSymbols = goog.i18n[prop];
  168. }
  169. }
  170. }
  171. };
  172. /**
  173. * CSS for date picker. See css.js for use.
  174. */
  175. Blockly.FieldDate.CSS = [
  176. /* Copied from: goog/css/datepicker.css */
  177. /**
  178. * Copyright 2009 The Closure Library Authors. All Rights Reserved.
  179. *
  180. * Use of this source code is governed by the Apache License, Version 2.0.
  181. * See the COPYING file for details.
  182. */
  183. /**
  184. * Standard styling for a goog.ui.DatePicker.
  185. *
  186. * @author arv@google.com (Erik Arvidsson)
  187. */
  188. '.blocklyWidgetDiv .goog-date-picker,',
  189. '.blocklyWidgetDiv .goog-date-picker th,',
  190. '.blocklyWidgetDiv .goog-date-picker td {',
  191. ' font: 13px Arial, sans-serif;',
  192. '}',
  193. '.blocklyWidgetDiv .goog-date-picker {',
  194. ' -moz-user-focus: normal;',
  195. ' -moz-user-select: none;',
  196. ' position: relative;',
  197. ' border: 1px solid #000;',
  198. ' float: left;',
  199. ' padding: 2px;',
  200. ' color: #000;',
  201. ' background: #c3d9ff;',
  202. ' cursor: default;',
  203. '}',
  204. '.blocklyWidgetDiv .goog-date-picker th {',
  205. ' text-align: center;',
  206. '}',
  207. '.blocklyWidgetDiv .goog-date-picker td {',
  208. ' text-align: center;',
  209. ' vertical-align: middle;',
  210. ' padding: 1px 3px;',
  211. '}',
  212. '.blocklyWidgetDiv .goog-date-picker-menu {',
  213. ' position: absolute;',
  214. ' background: threedface;',
  215. ' border: 1px solid gray;',
  216. ' -moz-user-focus: normal;',
  217. ' z-index: 1;',
  218. ' outline: none;',
  219. '}',
  220. '.blocklyWidgetDiv .goog-date-picker-menu ul {',
  221. ' list-style: none;',
  222. ' margin: 0px;',
  223. ' padding: 0px;',
  224. '}',
  225. '.blocklyWidgetDiv .goog-date-picker-menu ul li {',
  226. ' cursor: default;',
  227. '}',
  228. '.blocklyWidgetDiv .goog-date-picker-menu-selected {',
  229. ' background: #ccf;',
  230. '}',
  231. '.blocklyWidgetDiv .goog-date-picker th {',
  232. ' font-size: .9em;',
  233. '}',
  234. '.blocklyWidgetDiv .goog-date-picker td div {',
  235. ' float: left;',
  236. '}',
  237. '.blocklyWidgetDiv .goog-date-picker button {',
  238. ' padding: 0px;',
  239. ' margin: 1px 0;',
  240. ' border: 0;',
  241. ' color: #20c;',
  242. ' font-weight: bold;',
  243. ' background: transparent;',
  244. '}',
  245. '.blocklyWidgetDiv .goog-date-picker-date {',
  246. ' background: #fff;',
  247. '}',
  248. '.blocklyWidgetDiv .goog-date-picker-week,',
  249. '.blocklyWidgetDiv .goog-date-picker-wday {',
  250. ' padding: 1px 3px;',
  251. ' border: 0;',
  252. ' border-color: #a2bbdd;',
  253. ' border-style: solid;',
  254. '}',
  255. '.blocklyWidgetDiv .goog-date-picker-week {',
  256. ' border-right-width: 1px;',
  257. '}',
  258. '.blocklyWidgetDiv .goog-date-picker-wday {',
  259. ' border-bottom-width: 1px;',
  260. '}',
  261. '.blocklyWidgetDiv .goog-date-picker-head td {',
  262. ' text-align: center;',
  263. '}',
  264. /** Use td.className instead of !important */
  265. '.blocklyWidgetDiv td.goog-date-picker-today-cont {',
  266. ' text-align: center;',
  267. '}',
  268. /** Use td.className instead of !important */
  269. '.blocklyWidgetDiv td.goog-date-picker-none-cont {',
  270. ' text-align: center;',
  271. '}',
  272. '.blocklyWidgetDiv .goog-date-picker-month {',
  273. ' min-width: 11ex;',
  274. ' white-space: nowrap;',
  275. '}',
  276. '.blocklyWidgetDiv .goog-date-picker-year {',
  277. ' min-width: 6ex;',
  278. ' white-space: nowrap;',
  279. '}',
  280. '.blocklyWidgetDiv .goog-date-picker-monthyear {',
  281. ' white-space: nowrap;',
  282. '}',
  283. '.blocklyWidgetDiv .goog-date-picker table {',
  284. ' border-collapse: collapse;',
  285. '}',
  286. '.blocklyWidgetDiv .goog-date-picker-other-month {',
  287. ' color: #888;',
  288. '}',
  289. '.blocklyWidgetDiv .goog-date-picker-wkend-start,',
  290. '.blocklyWidgetDiv .goog-date-picker-wkend-end {',
  291. ' background: #eee;',
  292. '}',
  293. /** Use td.className instead of !important */
  294. '.blocklyWidgetDiv td.goog-date-picker-selected {',
  295. ' background: #c3d9ff;',
  296. '}',
  297. '.blocklyWidgetDiv .goog-date-picker-today {',
  298. ' background: #9ab;',
  299. ' font-weight: bold !important;',
  300. ' border-color: #246 #9bd #9bd #246;',
  301. ' color: #fff;',
  302. '}'
  303. ];