Files: 6200cdd535433e85ea74c0c9b081b40ecfadc318 / modules / avatar-edit.js
2912 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 | |
19 | function crop (d, cb) { |
20 | var data |
21 | var canvas = hypercrop(h('img', {src: d})) |
22 | |
23 | return h('div.avatar_pic', |
24 | canvas, |
25 | //canvas.selection, |
26 | h('div.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 | |
37 | exports.avatar_edit = function (id) { |
38 | |
39 | var img = h('img', {src: blob_url(default_avatar)}) |
40 | var lb = hyperlightbox() |
41 | var name_input = h('input', {placeholder: 'rename'}) |
42 | var name = h('h2.avatar__name') |
43 | var selected = null |
44 | |
45 | getAvatar({links: sbot_links}, self_id, id, function (err, avatar) { |
46 | if (err) return console.error(err) |
47 | //don't show user has already selected an avatar. |
48 | if(selected) return |
49 | if(ref.isBlob(avatar.image)) |
50 | img.src = blob_url(avatar.image) |
51 | name.textContent = avatar.name |
52 | }) |
53 | |
54 | |
55 | return h('div', |
56 | lb, |
57 | name, |
58 | img, |
59 | name_input, |
60 | hyperfile.asDataURL(function (data) { |
61 | var el = crop(data, function (err, data) { |
62 | if(data) { |
63 | img.src = data |
64 | selected = dataurl.parse(data) |
65 | } |
66 | lb.close() |
67 | }) |
68 | lb.show(el) |
69 | }), |
70 | h('button', 'update', {onclick: function () { |
71 | if(name_input.value) |
72 | name.textContent = name_input.value |
73 | |
74 | if(selected) { |
75 | pull( |
76 | pull.once(selected.data), |
77 | sbot_blobs_add(function (err, hash) { |
78 | //TODO. Alerts are EVIL. |
79 | //I use them only in a moment of weakness. |
80 | if(err) return alert(err.stack) |
81 | confirm({ |
82 | type: 'about', |
83 | about: id, |
84 | name: name_input.value || undefined, |
85 | image: { |
86 | link: hash, |
87 | size: selected.data.length, |
88 | type: selected.mimetype, |
89 | width: 512, |
90 | height: 512 |
91 | } |
92 | }) |
93 | }) |
94 | ) |
95 | } |
96 | else if(input.value) //name only |
97 | confirm({ |
98 | type: 'about', |
99 | about: id, |
100 | name: name_input.value || undefined, |
101 | }) |
102 | else |
103 | //another moment of weakness |
104 | alert('must select a name or image') |
105 | }}) |
106 | ) |
107 | } |
108 | |
109 |
Built with git-ssb-web