git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 969793e4372dd1060b4b8156ab198df532d93cf5

Files: 969793e4372dd1060b4b8156ab198df532d93cf5 / modules / avatar-edit.js

3822 bytesRaw
1var dataurl = require('dataurl')
2var hyperfile = require('hyperfile')
3var hypercrop = require('hypercrop')
4var hyperlightbox = require('hyperlightbox')
5var h = require('hyperscript')
6var pull = require('pull-stream')
7var getAvatar = require('ssb-avatar')
8var plugs = require('../plugs')
9var ref = require('ssb-ref')
10
11var self_id = require('../keys').id
12var default_avatar = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256'
13
14var confirm = plugs.first(exports.message_confirm = [])
15var sbot_blobs_add = plugs.first(exports.sbot_blobs_add = [])
16var blob_url = plugs.first(exports.blob_url = [])
17var sbot_links = plugs.first(exports.sbot_links = [])
18var avatar_name = plugs.first(exports.avatar_name = [])
19
20function crop (d, cb) {
21 var data
22 var canvas = hypercrop(h('img', {src: d}))
23
24 return h('div.column.avatar_pic',
25 canvas,
26 //canvas.selection,
27 h('div.row.avatar_pic__controls',
28 h('button', 'okay', {onclick: function () {
29 cb(null, canvas.selection.toDataURL())
30 }}),
31 h('button', 'cancel', {onclick: function () {
32 cb(new Error('canceled'))
33 }})
34 )
35 )
36}
37
38exports.avatar_edit = function (id) {
39
40 var img = h('img.avatar--large', {src: blob_url(default_avatar)})
41 var lb = hyperlightbox()
42 var name_input = h('input', {placeholder: 'rename'})
43 var name = avatar_name(id)
44 var selected = null, selected_data = null
45
46 getAvatar({links: sbot_links}, self_id, id, function (err, avatar) {
47 if (err) return console.error(err)
48 //don't show user has already selected an avatar.
49 if(selected) return
50 if(ref.isBlob(avatar.image))
51 img.src = blob_url(avatar.image)
52 })
53
54 var also_pictured = h('div.profile__alsopicturedas.wrap')
55
56 pull(
57 sbot_links({dest: id, rel: 'about', values: true}),
58 pull.map(function (e) {
59 return e.value.content.image
60 }),
61 pull.filter(function (e) {
62 return e && 'string' == typeof e.link
63 }),
64 pull.unique('link'),
65 pull.drain(function (image) {
66 also_pictured.appendChild(
67 h('a', {href:'#', onclick: function (ev) {
68 ev.stopPropagation()
69 ev.preventDefault()
70 selected = image
71 img.src = blob_url(image.link || image)
72 }},
73 h('img.avatar--thumbnail', {src: blob_url(image)})
74 )
75 )
76 })
77 )
78
79 return h('div.row.profile',
80 lb,
81 img,
82 h('div.column.profile__info',
83 h('strong', name),
84 name_input,
85
86 hyperfile.asDataURL(function (data) {
87 var el = crop(data, function (err, data) {
88 if(data) {
89 img.src = data
90 pull(
91 pull.once(dataurl.parse(data)),
92 sbot_blobs_add(function (err, hash) {
93 //TODO. Alerts are EVIL.
94 //I use them only in a moment of weakness.
95
96 if(err) return alert(err.stack)
97 selected = {
98 link: hash,
99 size: selected.data.length,
100 type: selected.mimetype,
101 width: 512,
102 height: 512
103 }
104
105 })
106 )
107 }
108 lb.close()
109 })
110 lb.show(el)
111 }),
112 h('button', 'update', {onclick: function () {
113 if(name_input.value)
114 name.textContent = name_input.value
115
116 if(selected)
117 confirm({
118 type: 'about',
119 about: id,
120 name: name_input.value || undefined,
121 image: selected
122 })
123 else if(name_input.value) //name only
124 confirm({
125 type: 'about',
126 about: id,
127 name: name_input.value || undefined,
128 })
129 else
130 //another moment of weakness
131 alert('must select a name or image')
132 }}),
133 also_pictured
134 )
135 )
136}
137
138
139
140
141
142
143
144
145
146
147
148

Built with git-ssb-web