time_range_mixin.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. require('views/common/time_range_list');
  20. var timeRangePopup = require('views/common/custom_date_popup');
  21. App.TimeRangeMixin = Em.Mixin.create({
  22. timeRangeClassName: 'pull-right',
  23. /**
  24. * time range options for service metrics, a dropdown will list all options
  25. * value set in hours
  26. */
  27. timeRangeOptions: [
  28. {index: 0, name: Em.I18n.t('graphs.timeRange.hour'), value: '1'},
  29. {index: 1, name: Em.I18n.t('graphs.timeRange.twoHours'), value: '2'},
  30. {index: 2, name: Em.I18n.t('graphs.timeRange.fourHours'), value: '4'},
  31. {index: 3, name: Em.I18n.t('graphs.timeRange.twelveHours'), value: '12'},
  32. {index: 4, name: Em.I18n.t('graphs.timeRange.day'), value: '24'},
  33. {index: 5, name: Em.I18n.t('graphs.timeRange.week'), value: '168'},
  34. {index: 6, name: Em.I18n.t('graphs.timeRange.month'), value: '720'},
  35. {index: 7, name: Em.I18n.t('graphs.timeRange.year'), value: '8760'},
  36. {index: 8, name: Em.I18n.t('common.custom'), value: '0'}
  37. ],
  38. currentTimeRangeIndex: 0,
  39. currentTimeRange: function() {
  40. return this.get('timeRangeOptions').objectAt(this.get('currentTimeRangeIndex'));
  41. }.property('currentTimeRangeIndex'),
  42. isCustomTimeRange: function () {
  43. return !(Em.isNone(this.get('customStartTime')) || Em.isNone(this.get('customEndTime')));
  44. }.property('customStartTime', 'customEndTime'),
  45. customStartTime: null,
  46. customEndTime: null,
  47. customDurationFormatted: null,
  48. customStartTimeFormatted: function () {
  49. var time = this.get('customStartTime');
  50. return Em.isNone(time) ? '' : App.formatDateTimeWithTimeZone(time, 'M/D/YYYY hh:mmA');
  51. }.property('customStartTime'),
  52. customEndTimeFormatted: function () {
  53. var time = this.get('customEndTime');
  54. return Em.isNone(time) ? '' : App.formatDateTimeWithTimeZone(time, 'M/D/YYYY hh:mmA');
  55. }.property('customEndTime'),
  56. tooltipMessage: function () {
  57. var message;
  58. if (this.get('isCustomTimeRange')) {
  59. message = Em.I18n.t('common.start') + ': ' + this.get('customStartTimeFormatted') + '<br>' + Em.I18n.t('common.duration') + ': ' + this.get('customDurationFormatted') + '<br>' + Em.I18n.t('common.end') + ': ' + this.get('customEndTimeFormatted');
  60. } else {
  61. message = '';
  62. }
  63. return message;
  64. }.property('customStartTimeFormatted', 'customEndTimeFormatted', 'customDurationFormatted'),
  65. didInsertElement: function () {
  66. App.tooltip(this.$(), {
  67. selector: '.dropdown-toggle[rel="tooltip"]'
  68. });
  69. },
  70. /**
  71. * onclick handler for a time range option
  72. * @param {object} event
  73. * @param {function} callback
  74. * @param {object} context
  75. */
  76. setTimeRange: function (event, callback, context) {
  77. var prevCustomTimeRange = this.getProperties(['currentTimeRangeIndex', 'customStartTime', 'customEndTime', 'customDurationFormatted']),
  78. index = event.context.index,
  79. primary = function () {
  80. var timeRange = {
  81. customEndTime: timeRangePopup.endTime,
  82. customStartTime: timeRangePopup.startTime,
  83. customDurationFormatted: timeRangePopup.customDuration
  84. };
  85. if (callback) {
  86. callback();
  87. }
  88. this.set('currentTimeRangeIndex', index);
  89. this.setProperties(timeRange);
  90. if (context) {
  91. context.setProperties(timeRange);
  92. }
  93. },
  94. secondary = this.setProperties.bind(this, prevCustomTimeRange);
  95. if (index === 8) {
  96. // Custom start and end time is specified by user
  97. var defaultStartTime,
  98. defaultEndTime,
  99. duration;
  100. if (prevCustomTimeRange.currentTimeRangeIndex === 8) {
  101. // Custom time range is active
  102. defaultStartTime = new Date(this.get('customStartTime')).getTime();
  103. defaultEndTime = new Date(this.get('customEndTime')).getTime();
  104. duration = this.get('customDurationFormatted') || 0;
  105. } else {
  106. // Preset time range is active
  107. var minutes;
  108. duration = this.get('currentTimeRange.value') * 3600000;
  109. defaultEndTime = new Date();
  110. minutes = 5 * Math.ceil(defaultEndTime.getMinutes() / 5);
  111. defaultEndTime.setMinutes(minutes);
  112. defaultStartTime = defaultEndTime.getTime() - duration;
  113. }
  114. timeRangePopup.showCustomDatePopup(primary.bind(this), secondary, {
  115. startDate: App.formatDateTimeWithTimeZone(defaultStartTime, 'MM/DD/YYYY'),
  116. hoursForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'hh'),
  117. minutesForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'mm'),
  118. middayPeriodForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'A'),
  119. duration: duration,
  120. endDate: App.formatDateTimeWithTimeZone(defaultEndTime, 'MM/DD/YYYY'),
  121. hoursForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'hh'),
  122. minutesForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'mm'),
  123. middayPeriodForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'A')
  124. });
  125. } else {
  126. // Preset time range is specified by user
  127. this.setProperties({
  128. currentTimeRangeIndex: index,
  129. customStartTime: null,
  130. customEndTime: null,
  131. customDurationFormatted: null
  132. });
  133. }
  134. },
  135. timeRangeListView: App.TimeRangeListView.extend()
  136. });