time_range.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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:function () {
  58. return this.get('isActive') ? "disabled" : false;
  59. }.property('isActive'),
  60. isActive:function () {
  61. return this.get('preset.value') == this.get('widget.chosenPreset.value');
  62. }.property('widget.chosenPreset'),
  63. template:Em.Handlebars.compile('<a {{action activate view.preset target="view.widget" href="true" }}>{{unbound view.preset.label}}</a>')
  64. }),
  65. /**
  66. * return default selected presets (currently - all)
  67. */
  68. defaultPresets:function () {
  69. var values = [];
  70. $.each(this.get('presets'), function () {
  71. if (this.value) {
  72. values.push(this.value);
  73. }
  74. });
  75. return values;
  76. }.property(),
  77. bindToController:function () {
  78. var thisW = this;
  79. var controller = this.get('controller');
  80. controller.set('presetWidget', thisW);
  81. },
  82. /**
  83. * assign this widget to controller, prepare items by presetsConfig
  84. */
  85. init:function () {
  86. this._super();
  87. this.bindToController();
  88. },
  89. /**
  90. * write active preset to widget
  91. * @param event
  92. */
  93. activate:function (event) {
  94. if (event.context == this.get('chosenPreset')) {
  95. this.set('chosenPreset', null);
  96. } else {
  97. this.set('chosenPreset', event.context);
  98. }
  99. },
  100. dateFromView: Ember.TextField.extend({
  101. elementId: 'timeRangeFrom',
  102. classNames: 'timeRangeFrom',
  103. attributeBindings:['readonly'],
  104. readonly: true,
  105. didInsertElement: function() {
  106. var self = this;
  107. this.$().datetimepicker({
  108. dateFormat: 'dd/mm/yy',
  109. timeFormat: 'hh:mm',
  110. maxDate: new Date(),
  111. onClose:function (dateText, inst) {
  112. var endDateTextBox = $('#timeRangeTo');
  113. if (endDateTextBox.val() != '') {
  114. var testStartDate = new Date(dateText);
  115. var testEndDate = new Date(endDateTextBox.val());
  116. if (testStartDate > testEndDate)
  117. endDateTextBox.val(dateText);
  118. } else {
  119. endDateTextBox.val(dateText);
  120. }
  121. self.set('dateFrom', dateText);
  122. },
  123. onSelect:function (selectedDateTime) {
  124. var start = $(this).datetimepicker('getDate');
  125. $('#timeRangeTo').datetimepicker('option', 'minDate', new Date(start.getTime()));
  126. }
  127. });
  128. self.set('dateFrom', this.get('value'));
  129. }
  130. }),
  131. dateToView: Ember.TextField.extend({
  132. elementId: 'timeRangeTo',
  133. classNames: 'timeRangeTo',
  134. attributeBindings:['readonly'],
  135. readonly: true,
  136. didInsertElement: function() {
  137. var self = this;
  138. this.$().datetimepicker({
  139. dateFormat: 'dd/mm/yy',
  140. timeFormat: 'hh:mm',
  141. maxDate: new Date(),
  142. onClose:function (dateText, inst) {
  143. var startDateTextBox = $('#timeRangeFrom');
  144. if (startDateTextBox.val() != '') {
  145. var testStartDate = new Date(startDateTextBox.val());
  146. var testEndDate = new Date(dateText);
  147. if (testStartDate > testEndDate)
  148. startDateTextBox.val(dateText);
  149. } else {
  150. startDateTextBox.val(dateText);
  151. }
  152. self.set('dateTo', dateText);
  153. },
  154. onSelect:function (selectedDateTime) {
  155. var end = $(this).datetimepicker('getDate');
  156. $('#timeRangeFrom').datetimepicker('option', 'maxDate', new Date(end.getTime()));
  157. }
  158. });
  159. self.set('dateTo', this.get('value'));
  160. }
  161. }),
  162. sliderOptions: Ember.Object.extend({
  163. end: null,
  164. period: null,
  165. start: function() {
  166. return this.get('end') - this.get('period');
  167. }.property('end', 'period')
  168. }),
  169. nowLabel: null,
  170. rangeLabel: null,
  171. buildSlider: function() {
  172. if (this.get('chosenPreset')) {
  173. var sliderOptions = this.sliderOptions.create({
  174. end: function() {
  175. var endDate = new Date();
  176. return endDate.getTime();
  177. }.property(),
  178. period: this.get('chosenPreset.period'),
  179. step: this.get('chosenPreset.step'),
  180. countTimeAgo: function(stepValue) {
  181. var msAgo = this.get('end') - stepValue;
  182. return msAgo.toDaysHoursMinutes();
  183. }
  184. });
  185. this.set('nowLabel', 'Now');
  186. this.set('rangeLabel', new Date(sliderOptions.get('start')));
  187. var self = this;
  188. $('#slider').slider({
  189. range: "max",
  190. min: sliderOptions.get('start'),
  191. max: sliderOptions.get('end'),
  192. value: sliderOptions.get('start'),
  193. step: sliderOptions.get('step'),
  194. stop: function(event, ui) {
  195. self.set('rangeLabel', new Date(ui.value));
  196. // self.set('rangeLabel', sliderOptions.countTimeAgo(ui.value).h);
  197. },
  198. slide: function(event, ui){
  199. self.set('rangeLabel', new Date(ui.value));
  200. // self.set('rangeLabel', sliderOptions.countTimeAgo(ui.value).h);
  201. }
  202. });
  203. } else {
  204. console.log(this.get('chosenPreset'));
  205. $("#slider").slider("destroy");
  206. }
  207. }.observes('chosenPreset')
  208. })