File size: 5,034 Bytes
271613e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
866d205
271613e
 
 
 
866d205
271613e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311e809
271613e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311e809
271613e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311e809
271613e
 
61f2db0
271613e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import map from 'licia/map'
import trim from 'licia/trim'
import isNull from 'licia/isNull'
import each from 'licia/each'
import copy from 'licia/copy'
import LunaModal from 'luna-modal'
import LunaDataGrid from 'luna-data-grid'
import { setState, getState } from './util'
import chobitsu from '../lib/chobitsu'
import { classPrefix as c } from '../lib/util'

export default class Cookie {
  constructor($container, devtools) {
    this._$container = $container
    this._devtools = devtools
    this._selectedItem = null

    this._initTpl()
    this._dataGrid = new LunaDataGrid(this._$dataGrid.get(0), {
      columns: [
        {
          id: 'key',
          title: 'γ‚­γƒΌ',
          weight: 30,
        },
        {
          id: 'value',
          title: 'ε€€',
          weight: 90,
        },
      ],
      minHeight: 60,
      maxHeight: 223,
    })

    this._bindEvent()
  }
  refresh() {
    const $container = this._$container
    const dataGrid = this._dataGrid

    const { cookies } = chobitsu.domain('Network').getCookies()
    const cookieData = map(cookies, ({ name, value }) => ({
      key: name,
      val: value,
    }))

    dataGrid.clear()
    each(cookieData, ({ key, val }) => {
      dataGrid.append(
        {
          key,
          value: val,
        },
        {
          selectable: true,
        }
      )
    })

    const cookieState = getState('cookie', cookieData.length)
    setState($container, cookieState)
  }
  _initTpl() {
    const $container = this._$container

    $container.html(
      c(`<h2 class="title">
      クッキー
      <div class="btn refresh-cookie">
        <span class="icon-refresh"></span>
      </div>
      <div class="btn show-detail btn-disabled">
        <span class="icon icon-eye"></span>
      </div>
      <div class="btn copy-cookie btn-disabled">
        <span class="icon icon-copy"></span>
      </div>
      <div class="btn delete-cookie btn-disabled">
        <span class="icon icon-delete"></span>
      </div>
      <div class="btn clear-cookie">
        <span class="icon-clear"></span>
      </div>
      <div class="btn filter" data-type="cookie">
        <span class="icon-filter"></span>
      </div>
      <div class="btn filter-text"></div>
    </h2>
    <div class="data-grid"></div>`)
    )

    this._$dataGrid = $container.find(c('.data-grid'))
    this._$filterText = $container.find(c('.filter-text'))
  }
  _updateButtons() {
    const $container = this._$container
    const $showDetail = $container.find(c('.show-detail'))
    const $deleteCookie = $container.find(c('.delete-cookie'))
    const $copyCookie = $container.find(c('.copy-cookie'))
    const btnDisabled = c('btn-disabled')

    $showDetail.addClass(btnDisabled)
    $deleteCookie.addClass(btnDisabled)
    $copyCookie.addClass(btnDisabled)

    if (this._selectedItem) {
      $showDetail.rmClass(btnDisabled)
      $deleteCookie.rmClass(btnDisabled)
      $copyCookie.rmClass(btnDisabled)
    }
  }
  _getVal(key) {
    const { cookies } = chobitsu.domain('Network').getCookies()

    for (let i = 0, len = cookies.length; i < len; i++) {
      if (cookies[i].name === key) {
        return cookies[i].value
      }
    }

    return ''
  }
  _bindEvent() {
    const devtools = this._devtools

    this._$container
      .on('click', c('.refresh-cookie'), () => {
        devtools.notify('ζ›΄ζ–°γ—γΎγ—γŸ', { icon: 'success' })
        this.refresh()
      })
      .on('click', c('.clear-cookie'), () => {
        chobitsu.domain('Storage').clearDataForOrigin({
          storageTypes: 'cookies',
        })
        this.refresh()
      })
      .on('click', c('.delete-cookie'), () => {
        const key = this._selectedItem

        chobitsu.domain('Network').deleteCookies({ name: key })
        this.refresh()
      })
      .on('click', c('.show-detail'), () => {
        const key = this._selectedItem
        const val = this._getVal(key)

        try {
          showSources('object', JSON.parse(val))
        } catch {
          showSources('raw', val)
        }
      })
      .on('click', c('.copy-cookie'), () => {
        const key = this._selectedItem
        copy(this._getVal(key))
        devtools.notify('γ‚³γƒ”γƒΌγ—γΎγ—γŸ', { icon: 'success' })
      })
      .on('click', c('.filter'), () => {
        LunaModal.prompt('フィルターをε…₯εŠ›').then((filter) => {
          if (isNull(filter)) return
          filter = trim(filter)
          this._filter = filter
          this._$filterText.text(filter)
          this._dataGrid.setOption('filter', filter)
        })
      })

    function showSources(type, data) {
      const sources = devtools.get('sources')
      if (!sources) return

      sources.set(type, data)

      devtools.showTool('sources')

      return true
    }

    this._dataGrid
      .on('select', (node) => {
        this._selectedItem = node.data.key
        this._updateButtons()
      })
      .on('deselect', () => {
        this._selectedItem = null
        this._updateButtons()
      })
  }
}