2018-10-03 19:27:40 -04:00
|
|
|
'use strict'
|
2018-08-28 00:34:17 -04:00
|
|
|
|
2019-01-08 22:51:57 -05:00
|
|
|
function Picker (dotgrid) {
|
2018-10-03 19:27:40 -04:00
|
|
|
this.memory = ''
|
|
|
|
this.el = document.createElement('div')
|
|
|
|
this.el.id = 'picker'
|
|
|
|
this.is_active = false
|
|
|
|
this.input = document.createElement('input')
|
|
|
|
this.input.id = 'picker_input'
|
2018-03-06 19:50:41 -05:00
|
|
|
|
2018-09-11 23:27:01 -04:00
|
|
|
this.el.appendChild(this.input)
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.start = function () {
|
|
|
|
if (this.is_active) { return }
|
2018-09-16 21:48:39 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.is_active = true
|
2018-09-16 21:48:39 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
this.input.setAttribute('placeholder', `${DOTGRID.tool.style().color.replace('#', '').trim()}`)
|
2018-10-03 19:27:40 -04:00
|
|
|
this.input.setAttribute('maxlength', 6)
|
2018-05-06 19:15:23 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.interface.el.className = 'picker'
|
2018-09-11 23:27:01 -04:00
|
|
|
this.input.focus()
|
2018-10-03 19:27:40 -04:00
|
|
|
this.input.value = ''
|
2018-08-17 16:04:54 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
try { DOTGRID.controller.set('picker') } catch (err) { }
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.update = function () {
|
|
|
|
if (!this.is_active) { return }
|
|
|
|
if (!is_color(this.input.value)) { return }
|
2018-09-11 23:27:01 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
const hex = `#${this.input.value}`
|
2018-03-21 03:10:09 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
document.getElementById('option_color').children[0].style.fill = hex
|
|
|
|
document.getElementById('option_color').children[0].style.stroke = hex
|
2018-09-16 21:48:39 -04:00
|
|
|
}
|
2018-05-06 19:05:25 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.stop = function () {
|
|
|
|
if (!this.is_active) { return }
|
2018-08-17 16:04:54 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.is_active = false
|
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.interface.el.className = ''
|
2018-09-11 23:27:01 -04:00
|
|
|
this.input.blur()
|
2018-10-03 19:27:40 -04:00
|
|
|
this.input.value = ''
|
2018-07-26 04:40:06 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
try { DOTGRID.controller.set() } catch (err) { console.log('No controller') }
|
2018-05-06 19:05:25 -04:00
|
|
|
|
2019-01-08 22:12:18 -05:00
|
|
|
setTimeout(() => { DOTGRID.interface.update(true); DOTGRID.renderer.update() }, 250)
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.validate = function () {
|
|
|
|
if (!is_color(this.input.value)) { return }
|
2018-03-06 19:50:41 -05:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
const hex = `#${this.input.value}`
|
2018-09-11 23:27:01 -04:00
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
DOTGRID.tool.style().color = hex
|
|
|
|
DOTGRID.tool.style().fill = DOTGRID.tool.style().fill != 'none' ? hex : 'none'
|
2018-09-16 21:48:39 -04:00
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.stop()
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.listen = function (e, is_down = false) {
|
2019-01-08 23:53:28 -05:00
|
|
|
if (is_down && !isColorChar(e.key)) {
|
2018-10-03 19:27:40 -04:00
|
|
|
e.preventDefault()
|
|
|
|
return
|
2018-09-13 23:03:12 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
if (e.key == 'Enter') {
|
|
|
|
this.validate()
|
|
|
|
e.preventDefault()
|
|
|
|
return
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
if (e.key == 'Escape') {
|
|
|
|
this.stop()
|
|
|
|
e.preventDefault()
|
|
|
|
return
|
2018-08-28 17:41:41 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
this.update()
|
2018-03-06 19:50:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
function is_color (val) {
|
|
|
|
if (val.length != 3 && val.length != 6) {
|
2018-05-06 19:05:25 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-03 19:27:40 -04:00
|
|
|
const re = /[0-9A-Fa-f]/g
|
2018-03-21 03:10:09 -04:00
|
|
|
return re.test(val)
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:53:28 -05:00
|
|
|
function isColorChar (val) {
|
2018-10-03 19:27:40 -04:00
|
|
|
const re = /[0-9A-Fa-f]/g
|
2018-09-13 23:03:12 -04:00
|
|
|
return re.test(val)
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:52:17 -04:00
|
|
|
this.input.onkeydown = function (event) { DOTGRID.picker.listen(event, true) }
|
|
|
|
this.input.onkeyup = function (event) { DOTGRID.picker.listen(event) }
|
2018-10-03 19:27:40 -04:00
|
|
|
}
|