Class: Daterange::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/daterange/controllers/main_controller.rb

Instance Method Summary collapse

Instance Method Details

#before_index_removeObject



94
95
96
97
# File 'app/daterange/controllers/main_controller.rb', line 94

def before_index_remove
  @start_comp.stop
  @end_comp.stop
end

#format_strObject



4
5
6
# File 'app/daterange/controllers/main_controller.rb', line 4

def format_str
  attrs.format_str || 'MM-DD-YYYY'
end

#index_readyObject



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
# File 'app/daterange/controllers/main_controller.rb', line 8

def index_ready
  callback = proc { |start_date, end_date| set_date(start_date, end_date) }
  `
  var opts = {
    ranges: {
      'Today': [moment(), moment()],
      'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
      'Last 7 Days': [moment().subtract(6, 'days'), moment()],
      'Last 30 Days': [moment().subtract(29, 'days'), moment()],
      'This Month': [moment().startOf('month'), moment().endOf('month')],
      'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    }
  };
  `
  # You can specify an integer to limit the number of days you can select
  # at once.
  if attrs.limit_days
    `opts.dateLimit = {
      days: #{attrs.limit_days.to_i}
    };`
  end

  @picker = `$(#{first_element}).daterangepicker(opts, #{callback})`

  puts "a"
  if attrs.start_date
    start_date = `moment(#{attrs.start_date}, #{format_str})`
  else
    start_date = `moment().subtract(29, 'days')`
  end

  puts "b"

  if attrs.end_date
    end_date = `moment(#{attrs.end_date}, #{format_str})`
  else
    end_date = `moment()`
  end

  puts "c"

  set_date(start_date, end_date)

  puts "d"

  @start_comp = proc do
    val = attrs.start_date
    picker = @picker
    if val
      `
      if (picker.setStartDate) {
        var date = moment(val);
        if (date.isValid()) {
          console.log('set start');
          picker.setStartDate(date);
        }
      }
      `
    end
  end.watch!

  @end_comp = proc do
    val = attrs.end_date
    picker = @picker
    if val
      puts "Try Update"
      `
      if (picker.setEndDate) {
        var date = moment(val);
        if (date.isValid()) {
          console.log('set end');
          picker.setEndDate(date);
        }
      }
      `
    end
  end.watch!


end

#pickerObject



89
90
91
92
# File 'app/daterange/controllers/main_controller.rb', line 89

def picker
  # Get the picker obj so we can set on it
  return `$(#{first_element}).data('daterangepicker')`
end

#set_date(start_date, end_date) ⇒ Object



99
100
101
102
103
104
# File 'app/daterange/controllers/main_controller.rb', line 99

def set_date(start_date, end_date)
  `$(#{first_element}).find('span').html(start_date.format(#{format_str}) + ' - ' + end_date.format(#{format_str})); `

  attrs.start_date = `start_date.format(#{format_str})`
  attrs.end_date = `end_date.format(#{format_str})`
end