defaultdatepickerrenderer.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2013 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 The default renderer for {@link goog.ui.DatePicker}.
  16. *
  17. * @see ../demos/datepicker.html
  18. */
  19. goog.provide('goog.ui.DefaultDatePickerRenderer');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. /** @suppress {extraRequire} Interface. */
  23. goog.require('goog.ui.DatePickerRenderer');
  24. /**
  25. * Default renderer for {@link goog.ui.DatePicker}. Renders the date picker's
  26. * navigation header and footer.
  27. *
  28. * @param {string} baseCssClass Name of base CSS class of the date picker.
  29. * @param {goog.dom.DomHelper=} opt_domHelper DOM helper.
  30. * @constructor
  31. * @implements {goog.ui.DatePickerRenderer}
  32. */
  33. goog.ui.DefaultDatePickerRenderer = function(baseCssClass, opt_domHelper) {
  34. /**
  35. * Name of base CSS class of datepicker
  36. * @type {string}
  37. * @private
  38. */
  39. this.baseCssClass_ = baseCssClass;
  40. /**
  41. * @type {!goog.dom.DomHelper}
  42. * @private
  43. */
  44. this.dom_ = opt_domHelper || goog.dom.getDomHelper();
  45. };
  46. /**
  47. * Returns the dom helper that is being used on this component.
  48. * @return {!goog.dom.DomHelper} The dom helper used on this component.
  49. */
  50. goog.ui.DefaultDatePickerRenderer.prototype.getDomHelper = function() {
  51. return this.dom_;
  52. };
  53. /**
  54. * Returns base CSS class. This getter is used to get base CSS class part.
  55. * All CSS class names in component are created as:
  56. * goog.getCssName(this.getBaseCssClass(), 'CLASS_NAME')
  57. * @return {string} Base CSS class.
  58. */
  59. goog.ui.DefaultDatePickerRenderer.prototype.getBaseCssClass = function() {
  60. return this.baseCssClass_;
  61. };
  62. /**
  63. * Render the navigation row (navigating months and maybe years).
  64. *
  65. * @param {!Element} row The parent element to render the component into.
  66. * @param {boolean} simpleNavigation Whether the picker should render a simple
  67. * navigation menu that only contains controls for navigating to the next
  68. * and previous month. The default navigation menu contains controls for
  69. * navigating to the next/previous month, next/previous year, and menus for
  70. * jumping to specific months and years.
  71. * @param {boolean} showWeekNum Whether week numbers should be shown.
  72. * @param {string} fullDateFormat The full date format.
  73. * {@see goog.i18n.DateTimeSymbols}.
  74. * @override
  75. */
  76. goog.ui.DefaultDatePickerRenderer.prototype.renderNavigationRow = function(
  77. row, simpleNavigation, showWeekNum, fullDateFormat) {
  78. // Populate the navigation row according to the configured navigation mode.
  79. var cell, monthCell, yearCell;
  80. if (simpleNavigation) {
  81. cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  82. cell.colSpan = showWeekNum ? 1 : 2;
  83. this.createButton_(
  84. cell, '\u00AB',
  85. goog.getCssName(this.getBaseCssClass(), 'previousMonth')); // <<
  86. row.appendChild(cell);
  87. cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  88. cell.colSpan = showWeekNum ? 6 : 5;
  89. cell.className = goog.getCssName(this.getBaseCssClass(), 'monthyear');
  90. row.appendChild(cell);
  91. cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  92. this.createButton_(
  93. cell, '\u00BB',
  94. goog.getCssName(this.getBaseCssClass(), 'nextMonth')); // >>
  95. row.appendChild(cell);
  96. } else {
  97. monthCell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  98. monthCell.colSpan = 5;
  99. this.createButton_(
  100. monthCell, '\u00AB',
  101. goog.getCssName(this.getBaseCssClass(), 'previousMonth')); // <<
  102. this.createButton_(
  103. monthCell, '', goog.getCssName(this.getBaseCssClass(), 'month'));
  104. this.createButton_(
  105. monthCell, '\u00BB',
  106. goog.getCssName(this.getBaseCssClass(), 'nextMonth')); // >>
  107. yearCell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  108. yearCell.colSpan = 3;
  109. this.createButton_(
  110. yearCell, '\u00AB',
  111. goog.getCssName(this.getBaseCssClass(), 'previousYear')); // <<
  112. this.createButton_(
  113. yearCell, '', goog.getCssName(this.getBaseCssClass(), 'year'));
  114. this.createButton_(
  115. yearCell, '\u00BB',
  116. goog.getCssName(this.getBaseCssClass(), 'nextYear')); // <<
  117. // If the date format has year ('y') appearing first before month ('m'),
  118. // show the year on the left hand side of the datepicker popup. Otherwise,
  119. // show the month on the left side. This check assumes the data to be
  120. // valid, and that all date formats contain month and year.
  121. if (fullDateFormat.indexOf('y') < fullDateFormat.indexOf('m')) {
  122. row.appendChild(yearCell);
  123. row.appendChild(monthCell);
  124. } else {
  125. row.appendChild(monthCell);
  126. row.appendChild(yearCell);
  127. }
  128. }
  129. };
  130. /**
  131. * Render the footer row (with select buttons).
  132. *
  133. * @param {!Element} row The parent element to render the component into.
  134. * @param {boolean} showWeekNum Whether week numbers should be shown.
  135. * @override
  136. */
  137. goog.ui.DefaultDatePickerRenderer.prototype.renderFooterRow = function(
  138. row, showWeekNum) {
  139. // Populate the footer row with buttons for Today and None.
  140. var cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  141. cell.colSpan = showWeekNum ? 2 : 3;
  142. cell.className = goog.getCssName(this.getBaseCssClass(), 'today-cont');
  143. /** @desc Label for button that selects the current date. */
  144. var MSG_DATEPICKER_TODAY_BUTTON_LABEL = goog.getMsg('Today');
  145. this.createButton_(
  146. cell, MSG_DATEPICKER_TODAY_BUTTON_LABEL,
  147. goog.getCssName(this.getBaseCssClass(), 'today-btn'));
  148. row.appendChild(cell);
  149. cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  150. cell.colSpan = showWeekNum ? 4 : 3;
  151. row.appendChild(cell);
  152. cell = this.getDomHelper().createElement(goog.dom.TagName.TD);
  153. cell.colSpan = 2;
  154. cell.className = goog.getCssName(this.getBaseCssClass(), 'none-cont');
  155. /** @desc Label for button that clears the selection. */
  156. var MSG_DATEPICKER_NONE = goog.getMsg('None');
  157. this.createButton_(
  158. cell, MSG_DATEPICKER_NONE,
  159. goog.getCssName(this.getBaseCssClass(), 'none-btn'));
  160. row.appendChild(cell);
  161. };
  162. /**
  163. * Support function for button creation.
  164. *
  165. * @param {Element} parentNode Container the button should be added to.
  166. * @param {string} label Button label.
  167. * @param {string=} opt_className Class name for button, which will be used
  168. * in addition to "goog-date-picker-btn".
  169. * @private
  170. * @return {!Element} The created button element.
  171. */
  172. goog.ui.DefaultDatePickerRenderer.prototype.createButton_ = function(
  173. parentNode, label, opt_className) {
  174. var classes = [goog.getCssName(this.getBaseCssClass(), 'btn')];
  175. if (opt_className) {
  176. classes.push(opt_className);
  177. }
  178. var el = this.getDomHelper().createElement(goog.dom.TagName.BUTTON);
  179. el.className = classes.join(' ');
  180. el.appendChild(this.getDomHelper().createTextNode(label));
  181. parentNode.appendChild(el);
  182. return el;
  183. };