Files: 53cf077425b3a9a22877b44e927c7bcc36fb08e8 / modules / avatar-edit.js
3817 bytesRaw
1 | var dataurl = require('dataurl') |
2 | var hyperfile = require('hyperfile') |
3 | var hypercrop = require('hypercrop') |
4 | var hyperlightbox = require('hyperlightbox') |
5 | var h = require('hyperscript') |
6 | var pull = require('pull-stream') |
7 | var getAvatar = require('ssb-avatar') |
8 | var plugs = require('../plugs') |
9 | var ref = require('ssb-ref') |
10 | |
11 | var self_id = require('../keys').id |
12 | var default_avatar = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256' |
13 | |
14 | var confirm = plugs.first(exports.message_confirm = []) |
15 | var sbot_blobs_add = plugs.first(exports.sbot_blobs_add = []) |
16 | var blob_url = plugs.first(exports.blob_url = []) |
17 | var sbot_links = plugs.first(exports.sbot_links = []) |
18 | var avatar_name = plugs.first(exports.avatar_name = []) |
19 | |
20 | function 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 | |
38 | exports.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(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