Code coverage report for app/mixins/common/widgets/time_range_mixin.js

Statements: 48.72% (19 / 39)      Branches: 16.67% (3 / 18)      Functions: 44.44% (4 / 9)      Lines: 48.72% (19 / 39)      Ignored: none     

All files » app/mixins/common/widgets/ » time_range_mixin.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149    1                                     1   1 1   1                         12                                                                   13                           8                                     8   3 3             3 3 3 3 3 3   3                         5                        
'use strict';
 
;require.register("mixins/common/widgets/time_range_mixin", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  require('views/common/time_range_list');
  var timeRangePopup = require('views/common/custom_date_popup');
 
  App.TimeRangeMixin = Em.Mixin.create({
 
    timeRangeClassName: 'pull-right',
 
    /**
     * time range options for service metrics, a dropdown will list all options
     * value set in hours
     */
    timeRangeOptions: [{ index: 0, name: Em.I18n.t('graphs.timeRange.hour'), value: '1' }, { index: 1, name: Em.I18n.t('graphs.timeRange.twoHours'), value: '2' }, { index: 2, name: Em.I18n.t('graphs.timeRange.fourHours'), value: '4' }, { index: 3, name: Em.I18n.t('graphs.timeRange.twelveHours'), value: '12' }, { index: 4, name: Em.I18n.t('graphs.timeRange.day'), value: '24' }, { index: 5, name: Em.I18n.t('graphs.timeRange.week'), value: '168' }, { index: 6, name: Em.I18n.t('graphs.timeRange.month'), value: '720' }, { index: 7, name: Em.I18n.t('graphs.timeRange.year'), value: '8760' }, { index: 8, name: Em.I18n.t('common.custom'), value: '0' }],
 
    currentTimeRangeIndex: 0,
 
    currentTimeRange: function () {
      return this.get('timeRangeOptions').objectAt(this.get('currentTimeRangeIndex'));
    }.property('currentTimeRangeIndex'),
 
    isCustomTimeRange: function () {
      return !(Em.isNone(this.get('customStartTime')) || Em.isNone(this.get('customEndTime')));
    }.property('customStartTime', 'customEndTime'),
 
    customStartTime: null,
 
    customEndTime: null,
 
    customDurationFormatted: null,
 
    customStartTimeFormatted: function () {
      var time = this.get('customStartTime');
      return Em.isNone(time) ? '' : App.formatDateTimeWithTimeZone(time, 'M/D/YYYY hh:mmA');
    }.property('customStartTime'),
 
    customEndTimeFormatted: function () {
      var time = this.get('customEndTime');
      return Em.isNone(time) ? '' : App.formatDateTimeWithTimeZone(time, 'M/D/YYYY hh:mmA');
    }.property('customEndTime'),
 
    tooltipMessage: function () {
      var message;
      if (this.get('isCustomTimeRange')) {
        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');
      } else {
        message = '';
      }
      return message;
    }.property('customStartTimeFormatted', 'customEndTimeFormatted', 'customDurationFormatted'),
 
    didInsertElement: function didInsertElement() {
      App.tooltip(this.$(), {
        selector: '.dropdown-toggle[rel="tooltip"]',
        html: true,
        placement: 'left'
      });
    },
 
    /**
     * onclick handler for a time range option
     * @param {object} event
     * @param {function} callback
     * @param {object} context
     */
    setTimeRange: function setTimeRange(event, callback, context) {
      var prevCustomTimeRange = this.getProperties(['currentTimeRangeIndex', 'customStartTime', 'customEndTime', 'customDurationFormatted']),
          index = event.context.index,
          primary = function primary() {
        var timeRange = {
          customEndTime: timeRangePopup.endTime,
          customStartTime: timeRangePopup.startTime,
          customDurationFormatted: timeRangePopup.customDuration
        };
        if (callback) {
          callback();
        }
        this.set('currentTimeRangeIndex', index);
        this.setProperties(timeRange);
        if (context) {
          context.setProperties(timeRange);
        }
      },
          secondary = this.setProperties.bind(this, prevCustomTimeRange);
 
      if (index === 8) {
        // Custom start and end time is specified by user
        var defaultStartTime, defaultEndTime, duration;
        Iif (prevCustomTimeRange.currentTimeRangeIndex === 8) {
          // Custom time range is active
          defaultStartTime = new Date(this.get('customStartTime')).getTime();
          defaultEndTime = new Date(this.get('customEndTime')).getTime();
          duration = this.get('customDurationFormatted') || 0;
        } else {
          // Preset time range is active
          var minutes;
          duration = this.get('currentTimeRange.value') * 3600000;
          defaultEndTime = new Date();
          minutes = 5 * Math.ceil(defaultEndTime.getMinutes() / 5);
          defaultEndTime.setMinutes(minutes);
          defaultStartTime = defaultEndTime.getTime() - duration;
        }
        timeRangePopup.showCustomDatePopup(primary.bind(this), secondary, {
          startDate: App.formatDateTimeWithTimeZone(defaultStartTime, 'MM/DD/YYYY'),
          hoursForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'hh'),
          minutesForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'mm'),
          middayPeriodForStart: App.formatDateTimeWithTimeZone(defaultStartTime, 'A'),
          duration: duration,
          endDate: App.formatDateTimeWithTimeZone(defaultEndTime, 'MM/DD/YYYY'),
          hoursForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'hh'),
          minutesForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'mm'),
          middayPeriodForEnd: App.formatDateTimeWithTimeZone(defaultEndTime, 'A')
        });
      } else {
        // Preset time range is specified by user
        this.setProperties({
          currentTimeRangeIndex: index,
          customStartTime: null,
          customEndTime: null,
          customDurationFormatted: null
        });
      }
    },
 
    timeRangeListView: App.TimeRangeListView.extend()
 
  });
});