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