git ssb

16+

Dominic / patchbay



Tree: dbe8a8578e6c6cdb350c1d250ad3d646610b339f

Files: dbe8a8578e6c6cdb350c1d250ad3d646610b339f / modules_basic / avatar-edit.js

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

Built with git-ssb-web