time_range.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * use: {{view App.TimeRangeWidget controllerBinding="App.router.mainChartsController"}}
  21. * set controller.preset field with preset value
  22. * widget assign itself to controller like presetWidget (controller.get('presetWidget'))
  23. * @type {*}
  24. */
  25. App.TimeRangeWidget = Em.View.extend({
  26. classNames:['time-range-widget'],
  27. templateName:require('templates/common/time_range'),
  28. dateFrom: null,
  29. dateTo: null,
  30. /**
  31. * presets
  32. */
  33. presets:[
  34. Em.Object.create({ label:Em.I18n.t('timeRange.presets.1hour'), value:'1h', period: 3600000, step: 300000}),
  35. Em.Object.create({ label:Em.I18n.t('timeRange.presets.12hour'), value:'12h', period: 43200000, step: 3600000}),
  36. Em.Object.create({ label:Em.I18n.t('timeRange.presets.1day'), value:'1d', period: 86400000, step: 3600000}),
  37. Em.Object.create({ label:Em.I18n.t('timeRange.presets.1week'), value:'1wk', period: 604800000, step: 86400000}),
  38. Em.Object.create({ label:Em.I18n.t('timeRange.presets.1month'), value:'1mo', period: 2592000000, step: 86400000}),
  39. Em.Object.create({ label:Em.I18n.t('timeRange.presets.1year'), value:'1yr', period: 31536000000, step: 2592000000})
  40. ],
  41. /**
  42. * chosen preset value
  43. */
  44. chosenPreset: null,
  45. /**
  46. * return array of chosen presets
  47. */
  48. chosenPresets:function () {
  49. return this.get('chosenPreset') ? [this.get('chosenPreset')] : this.get('defaultPresets');
  50. }.property('chosenPreset'),
  51. /**
  52. * preset item view
  53. */
  54. presetView:Em.View.extend({
  55. tagName:'li',
  56. classNameBindings:['disabled'],
  57. disabled: Em.computed.ifThenElse('isActive', 'disabled', false),
  58. isActive: Em.computed.equalProperties('preset.value', 'widget.chosenPreset.value'),
  59. template:Em.Handlebars.compile('<a {{action activate view.preset target="view.widget" href="true" }}>{{unbound view.preset.label}}</a>')
  60. }),
  61. /**
  62. * return default selected presets (currently - all)
  63. */
  64. defaultPresets:function () {
  65. var values = [];
  66. $.each(this.get('presets'), function () {
  67. if (this.value) {
  68. values.push(this.value);
  69. }
  70. });
  71. return values;
  72. }.property(),
  73. bindToController:function () {
  74. var thisW = this;
  75. var controller = this.get('controller');
  76. controller.set('presetWidget', thisW);
  77. },
  78. /**
  79. * assign this widget to controller, prepare items by presetsConfig
  80. */
  81. init:function () {
  82. this._super();
  83. this.bindToController();
  84. },
  85. /**
  86. * write active preset to widget
  87. * @param event
  88. */
  89. activate:function (event) {
  90. if (event.context == this.get('chosenPreset')) {
  91. this.set('chosenPreset', null);
  92. } else {
  93. this.set('chosenPreset', event.context);
  94. }
  95. },
  96. dateFromView: Ember.TextField.extend({
  97. elementId: 'timeRangeFrom',
  98. classNames: 'timeRangeFrom',
  99. attributeBindings:['readonly'],
  100. readonly: true,
  101. didInsertElement: function() {
  102. var self = this;
  103. this.$().datetimepicker({
  104. dateFormat: 'dd/mm/yy',
  105. timeFormat: 'hh:mm',
  106. maxDate: new Date(),
  107. onClose:function (dateText, inst) {
  108. var endDateTextBox = $('#timeRangeTo');
  109. if (endDateTextBox.val() != '') {
  110. var testStartDate = new Date(dateText);
  111. var testEndDate = new Date(endDateTextBox.val());
  112. if (testStartDate > testEndDate)
  113. endDateTextBox.val(dateText);
  114. } else {
  115. endDateTextBox.val(dateText);
  116. }
  117. self.set('dateFrom', dateText);
  118. },
  119. onSelect:function (selectedDateTime) {
  120. var start = $(this).datetimepicker('getDate');
  121. $('#timeRangeTo').datetimepicker('option', 'minDate', new Date(start.getTime()));
  122. }
  123. });
  124. self.set('dateFrom', this.get('value'));
  125. }
  126. }),
  127. dateToView: Ember.TextField.extend({
  128. elementId: 'timeRangeTo',
  129. classNames: 'timeRangeTo',
  130. attributeBindings:['readonly'],
  131. readonly: true,
  132. didInsertElement: function() {
  133. var self = this;
  134. this.$().datetimepicker({
  135. dateFormat: 'dd/mm/yy',
  136. timeFormat: 'hh:mm',
  137. maxDate: new Date(),
  138. onClose:function (dateText, inst) {
  139. var startDateTextBox = $('#timeRangeFrom');
  140. if (startDateTextBox.val() != '') {
  141. var testStartDate = new Date(startDateTextBox.val());
  142. var testEndDate = new Date(dateText);
  143. if (testStartDate > testEndDate)
  144. startDateTextBox.val(dateText);
  145. } else {
  146. startDateTextBox.val(dateText);
  147. }
  148. self.set('dateTo', dateText);
  149. },
  150. onSelect:function (selectedDateTime) {
  151. var end = $(this).datetimepicker('getDate');
  152. $('#timeRangeFrom').datetimepicker('option', 'maxDate', new Date(end.getTime()));
  153. }
  154. });
  155. self.set('dateTo', this.get('value'));
  156. }
  157. }),
  158. sliderOptions: Ember.Object.extend({
  159. end: null,
  160. period: null,
  161. start: function() {
  162. return this.get('end') - this.get('period');
  163. }.property('end', 'period')
  164. }),
  165. nowLabel: null,
  166. rangeLabel: null,
  167. buildSlider: function() {
  168. if (this.get('chosenPreset')) {
  169. var sliderOptions = this.sliderOptions.create({
  170. end: function() {
  171. var endDate = new Date();
  172. return endDate.getTime();
  173. }.property(),
  174. period: this.get('chosenPreset.period'),
  175. step: this.get('chosenPreset.step'),
  176. countTimeAgo: function(stepValue) {
  177. var msAgo = this.get('end') - stepValue;
  178. return msAgo.toDaysHoursMinutes();
  179. }
  180. });
  181. this.set('nowLabel', 'Now');
  182. this.set('rangeLabel', new Date(sliderOptions.get('start')));
  183. var self = this;
  184. $('#slider').slider({
  185. range: "max",
  186. min: sliderOptions.get('start'),
  187. max: sliderOptions.get('end'),
  188. value: sliderOptions.get('start'),
  189. step: sliderOptions.get('step'),
  190. stop: function(event, ui) {
  191. self.set('rangeLabel', new Date(ui.value));
  192. // self.set('rangeLabel', sliderOptions.countTimeAgo(ui.value).h);
  193. },
  194. slide: function(event, ui){
  195. self.set('rangeLabel', new Date(ui.value));
  196. // self.set('rangeLabel', sliderOptions.countTimeAgo(ui.value).h);
  197. }
  198. });
  199. } else {
  200. $("#slider").slider("destroy");
  201. }
  202. }.observes('chosenPreset')
  203. });