Class: Doing::JSONExport

Inherits:
Object show all
Includes:
Util
Defined in:
lib/doing/plugins/export/json_export.rb

Class Method Summary collapse

Methods included from Util

#args_for_editor, #deep_merge_hashes, #deep_merge_hashes!, #default_editor, #duplicable?, #duplicate_frozen_values, #editor_with_args, #exec_available, #find_default_editor, #first_available_exec, #mergable?, #merge_default_proc, #merge_values, #safe_load_file, #user_home, #write_to_file

Class Method Details

.render(wwid, items, variables: {}) ⇒ Object



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
# File 'lib/doing/plugins/export/json_export.rb', line 17

def self.render(wwid, items, variables: {})
  if items.nil? || items.empty?
    return case variables[:options][:output]
           when 'json'
             {
               'section' => '',
               'items' => [],
               'timers' => ""
             }.to_json
           when 'timeline'
            "<html></html>"
           end
  end

  opt = variables[:options]
  opt[:output] =  case opt[:output]
                  when /^t/
                    'timeline'
                  else
                    'json'
                  end
  items_out = []

  last_date = items[-1].date + (60 * 60 * 24)
  max = last_date.strftime('%F')
  min = items[0].date.strftime('%F')
  items.each_with_index do |i, index|
    title = i.title.utf8
    note = i.note.utf8

    end_date = i.end_date || ''
    interval = wwid.get_interval(i, formatted: false) || 0
    duration = i.duration || 0
    note ||= ''

    tags = []
    attributes = {}
    skip_tags = %w[meanwhile done cancelled flagged]
    i.title.scan(/@([^(\s]+)(?:\((.*?)\))?/).each do |tag|
      tags.push(tag[0]) unless skip_tags.include?(tag[0])
      attributes[tag[0]] = tag[1] if tag[1]
    end

    case opt[:output]
    when 'json'
      i = {
        date: i.date,
        end_date: end_date,
        title: title.strip, #+ " #{note}"
        section: i.section,
        note: note.to_s(prefix: ''),
        time: interval.time_string(format: :clock),
        duration: duration.time_string(format: :clock),
        tags: tags,
        id: i.id
      }

      attributes.each { |attr, val| i[attr.to_sym] = val }

      items_out << i

    when 'timeline'
      new_item = {
        'id' => index + 1,
        'content' => title.strip, #+ " #{note}"
        'title' => title.strip + " (#{interval.time_string(format: :clock)})",
        'start' => i.date.strftime('%F %T'),
        'type' => 'box',
        'style' => 'color:#4c566b;background-color:#d8dee9;'
      }

      if interval.to_i&.positive?
        new_item['end'] = end_date.strftime('%F %T')
        if interval.to_i > 3600
          new_item['type'] = 'range'
          new_item['style'] = 'color:white;background-color:#a2bf8a;'
        end
      end
      new_item['style'] = 'color:white;background-color:#f7921e;' if i.tags?(Doing.setting('marker_tag'))
      items_out.push(new_item)
    end
  end
  case opt[:output]
  when 'json'
    Doing.logger.debug('JSON Export:', "#{items_out.count} items output to JSON")
    JSON.pretty_generate({
                           'section' => variables[:page_title],
                           'items' => items_out,
                           'timers' => wwid.tag_times(format: :json,
                                                      sort_by: opt[:sort_tags],
                                                      sort_order: opt[:tag_order])
                         })
  when 'timeline'
    template = <<~EOTEMPLATE
                <!doctype html>
                <html>
                <head>
                  <link href="https://unpkg.com/[email protected]/dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
                  <script src="https://unpkg.com/[email protected]/dist/vis-timeline-graph2d.min.js"></script>
                </head>
                <body>
                  <div id="mytimeline"></div>
      #{'          '}
                  <script type="text/javascript">
                    // DOM element where the Timeline will be attached
                    var container = document.getElementById('mytimeline');
      #{'          '}
                    // Create a DataSet with data (enables two way data binding)
                    var data = new vis.DataSet(#{items_out.to_json});
      #{'          '}
                    // Configuration for the Timeline
                    var options = {
                      width: '100%',
                      height: '800px',
                      margin: {
                        item: 20
                      },
                      stack: true,
                      min: '#{min}',
                      max: '#{max}'
                    };
      #{'          '}
                    // Create a Timeline
                    var timeline = new vis.Timeline(container, data, options);
                  </script>
                </body>
                </html>
    EOTEMPLATE
    Doing.logger.debug('Timeline Export:', "#{items_out.count} items output to Timeline")
    template
  end
end

.settingsObject



11
12
13
14
15
# File 'lib/doing/plugins/export/json_export.rb', line 11

def self.settings
  {
    trigger: 'json|time(?:line)?'
  }
end