git ssb

16+

Dominic / patchbay



Tree: c17210ec353dee6e28ecd08ebdc1c50a65d99dec

Files: c17210ec353dee6e28ecd08ebdc1c50a65d99dec / modules / avatar-edit.js

3842 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 var _data = dataurl.parse(data)
91 pull(
92 pull.once(_data.data),
93 sbot_blobs_add(function (err, hash) {
94 //TODO. Alerts are EVIL.
95 //I use them only in a moment of weakness.
96
97 if(err) return alert(err.stack)
98 selected = {
99 link: hash,
100 size: _data.data.length,
101 type: _data.mimetype,
102 width: 512,
103 height: 512
104 }
105
106 })
107 )
108 }
109 lb.close()
110 })
111 lb.show(el)
112 }),
113 h('button', 'update', {onclick: function () {
114 if(name_input.value)
115 name.textContent = name_input.value
116
117 if(selected)
118 confirm({
119 type: 'about',
120 about: id,
121 name: name_input.value || undefined,
122 image: selected
123 })
124 else if(name_input.value) //name only
125 confirm({
126 type: 'about',
127 about: id,
128 name: name_input.value || undefined,
129 })
130 else
131 //another moment of weakness
132 alert('must select a name or image')
133 }}),
134 also_pictured
135 )
136 )
137}
138
139

Built with git-ssb-web