File size: 4,307 Bytes
271613e 24a842e 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 |
import emitter from '../lib/emitter'
import Settings from '../Settings/Settings'
import Emitter from 'licia/Emitter'
import $ from 'licia/$'
import nextTick from 'licia/nextTick'
import orientation from 'licia/orientation'
import pointerEvent from 'licia/pointerEvent'
import { pxToNum, classPrefix as c, eventClient } from '../lib/util'
import evalCss from '../lib/evalCss'
const $document = $(document)
export default class EntryBtn extends Emitter {
constructor($container) {
super()
this._style = evalCss(require('./EntryBtn.scss'))
this._$container = $container
this._initTpl()
this._bindEvent()
this._registerListener()
}
hide() {
this._$el.hide()
}
show() {
this._$el.show()
}
setPos(pos) {
if (this._isOutOfRange(pos)) {
pos = this._getDefPos()
}
this._$el.css({
left: pos.x,
top: pos.y,
})
this.config.set('pos', pos)
}
getPos() {
return this.config.get('pos')
}
destroy() {
evalCss.remove(this._style)
this._unregisterListener()
this._$el.remove()
}
_isOutOfRange(pos) {
pos = pos || this.config.get('pos')
const defPos = this._getDefPos()
return (
pos.x > defPos.x + 10 || pos.x < 0 || pos.y < 0 || pos.y > defPos.y + 10
)
}
_registerListener() {
this._scaleListener = () =>
nextTick(() => {
if (this._isOutOfRange()) this._resetPos()
})
emitter.on(emitter.SCALE, this._scaleListener)
}
_unregisterListener() {
emitter.off(emitter.SCALE, this._scaleListener)
}
_initTpl() {
const $container = this._$container
$container.append(
c('<div class="entry-btn"><span class="icon-tool"></span></div>')
)
this._$el = $container.find('.eruda-entry-btn')
}
_resetPos(orientationChanged) {
const cfg = this.config
let pos = cfg.get('pos')
const defPos = this._getDefPos()
if (!cfg.get('rememberPos') || orientationChanged) {
pos = defPos
}
this.setPos(pos)
}
_onDragStart = (e) => {
const $el = this._$el
$el.addClass(c('active'))
this._isClick = true
e = e.origEvent
this._startX = eventClient('x', e)
this._oldX = pxToNum($el.css('left'))
this._oldY = pxToNum($el.css('top'))
this._startY = eventClient('y', e)
$document.on(pointerEvent('move'), this._onDragMove)
$document.on(pointerEvent('up'), this._onDragEnd)
}
_onDragMove = (e) => {
const btnSize = this._$el.get(0).offsetWidth
const maxWidth = this._$container.get(0).offsetWidth
const maxHeight = this._$container.get(0).offsetHeight
e = e.origEvent
const deltaX = eventClient('x', e) - this._startX
const deltaY = eventClient('y', e) - this._startY
if (Math.abs(deltaX) > 3 || Math.abs(deltaY) > 3) {
this._isClick = false
}
let newX = this._oldX + deltaX
let newY = this._oldY + deltaY
if (newX < 0) {
newX = 0
} else if (newX > maxWidth - btnSize) {
newX = maxWidth - btnSize
}
if (newY < 0) {
newY = 0
} else if (newY > maxHeight - btnSize) {
newY = maxHeight - btnSize
}
this._$el.css({
left: newX,
top: newY,
})
}
_onDragEnd = (e) => {
const $el = this._$el
if (this._isClick) {
this.emit('click')
}
this._onDragMove(e)
$document.off(pointerEvent('move'), this._onDragMove)
$document.off(pointerEvent('up'), this._onDragEnd)
const cfg = this.config
if (cfg.get('rememberPos')) {
cfg.set('pos', {
x: pxToNum($el.css('left')),
y: pxToNum($el.css('top')),
})
}
$el.rmClass('eruda-active')
}
_bindEvent() {
const $el = this._$el
$el.on(pointerEvent('down'), this._onDragStart)
orientation.on('change', () => this._resetPos(true))
window.addEventListener('resize', () => this._resetPos())
}
initCfg(settings) {
const cfg = (this.config = Settings.createCfg('entry-button', {
rememberPos: true,
pos: this._getDefPos(),
}))
settings.switch(cfg, 'rememberPos', '入力ボタンの位置を記憶する')
this._resetPos()
}
_getDefPos() {
const minWidth = this._$el.get(0).offsetWidth + 10
return {
x: window.innerWidth - minWidth,
y: window.innerHeight - minWidth,
}
}
}
|