-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
89 lines (80 loc) · 2.82 KB
/
client.js
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
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var vdom = require('virtual-dom')
, h = vdom.h
const SET_COLOR = 'AUTHORCOLORS_SET_COLOR'
module.exports = setup
module.exports.consumes = ['ui', 'api', 'presence']
module.exports.provides = ['authorColors']
function setup(plugin, imports, register) {
var ui = imports.ui
, api = imports.api
, presence = imports.presence
ui.reduxRootReducers.push((state, action) => {
if(SET_COLOR === action.type) {
return {...state, session: {
...state.session
, user: {
...state.session.user
, attributes: {
...state.session.user.attributes
, color: action.payload
}
}
}}
}
return state
})
var authorColors = {
action_setColor: function*(color) {
var state = ui.store.getState()
yield {type: SET_COLOR, payload: color, id: state.session.user.id}
yield api.action_user_update(state.session.user.id, {color})
yield presence.action_loadUser(state.session.user.id)
}
}
presence.onRenderUser(function(store, user, props, children) {
var state = store.getState()
// Border color
var style = props.style || (props.style = {})
, color = user.attributes.color || '#777'
style['border-color'] = color
// Color picker if user === this user
if(user.id == state.session.user.id) {
// invisible input
var input = new Widget(h('input', {
attributes: {type: 'color', value: color}
, 'ev-change': evt => {
store.dispatch(authorColors.action_setColor(evt.currentTarget.value))
}
}))
children.push(input)
// visible button that triggers the color input :/ hACk alarm
var button = h('button.btn.btn-default.btn-xs', {
'ev-click': evt => input.node.click()
, style: {color: color}
}, h('i.glyphicon.glyphicon-tint'))
children.push(button)
}
})
register(null, {authorColors: authorColors})
}
const Widget = function (vnode){this.node = vdom.create(vnode)}
Widget.prototype.type = "Widget"
Widget.prototype.init = function(){return this.node}
Widget.prototype.update = function(previous, domNode){this.node = domNode; return null}
Widget.prototype.destroy = function(domNode){}