Files: d513ed5cfa8254cfdde42c0391ea107d3ef9edd8 / modules / profile / sheet / edit.js
4845 bytesRaw
1 | var nest = require('depnest') |
2 | var extend = require('xtend') |
3 | var {Value, h, computed, when} = require('mutant') |
4 | var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' |
5 | |
6 | exports.gives = nest('profile.sheet.edit') |
7 | |
8 | exports.needs = nest({ |
9 | 'sheet.display': 'first', |
10 | 'keys.sync.id': 'first', |
11 | 'message.async.publish': 'first', |
12 | 'about.obs': { |
13 | name: 'first', |
14 | description: 'first', |
15 | image: 'first', |
16 | color: 'first' |
17 | }, |
18 | 'blob.html.input': 'first', |
19 | 'blob.sync.url': 'first', |
20 | 'intl.sync.i18n': 'first', |
21 | 'about.sync.shortFeedId': 'first' |
22 | }) |
23 | |
24 | exports.create = function (api) { |
25 | const i18n = api.intl.sync.i18n |
26 | return nest('profile.sheet.edit', function () { |
27 | var id = api.keys.sync.id() |
28 | api.sheet.display(close => { |
29 | var currentName = api.about.obs.name(id) |
30 | var currentImage = api.about.obs.image(id) |
31 | var currentDescription = api.about.obs.description(id) |
32 | |
33 | var publishing = Value(false) |
34 | var chosenImage = Value(currentImage()) |
35 | |
36 | // don't display if name is default |
37 | var chosenName = Value(currentName() === api.about.sync.shortFeedId(id) ? '' : currentName()) |
38 | var chosenDescription = Value(currentDescription()) |
39 | |
40 | return { |
41 | content: h('div', { |
42 | style: { |
43 | padding: '20px', |
44 | 'text-align': 'center' |
45 | } |
46 | }, [ |
47 | h('h2', { |
48 | style: { |
49 | 'font-weight': 'normal' |
50 | } |
51 | }, [i18n('Your Profile')]), |
52 | h('ProfileEditor', [ |
53 | h('div.side', [ |
54 | h('ImageInput', [ |
55 | h('img', { |
56 | style: { 'background-color': api.about.obs.color(id) }, |
57 | src: computed(chosenImage, (id) => id ? api.blob.sync.url(id) : fallbackImageUrl) |
58 | }), |
59 | h('span', ['🖼 ', i18n('Choose Profile Image...')]), |
60 | api.blob.html.input(file => { |
61 | chosenImage.set(file.link) |
62 | }, { |
63 | accept: 'image/*', |
64 | resize: { width: 500, height: 500 } |
65 | }) |
66 | ]) |
67 | ]), |
68 | h('div.main', [ |
69 | h('input.name', { |
70 | 'disabled': publishing, |
71 | placeholder: i18n('Choose a name'), |
72 | hooks: [ValueHook(chosenName), FocusHook()] |
73 | }), |
74 | h('textarea.description', { |
75 | 'disabled': publishing, |
76 | placeholder: i18n('Describe yourself (if you want)'), |
77 | hooks: [ValueHook(chosenDescription)] |
78 | }) |
79 | ]) |
80 | ]) |
81 | ]), |
82 | footer: [ |
83 | h('button -save', { |
84 | 'ev-click': save, |
85 | 'disabled': publishing |
86 | }, when(publishing, i18n('Publishing...'), i18n('Preview & Publish'))), |
87 | h('button -cancel', { |
88 | 'disabled': publishing, |
89 | 'ev-click': close |
90 | }, i18n('Cancel')) |
91 | ] |
92 | } |
93 | |
94 | function save () { |
95 | // no confirm |
96 | var update = {} |
97 | var newName = chosenName().trim() || currentName() |
98 | |
99 | if (chosenImage() !== currentImage()) update.image = chosenImage() |
100 | if (newName !== currentName()) update.name = newName |
101 | if (chosenDescription() !== currentDescription()) update.description = chosenDescription() |
102 | |
103 | if (Object.keys(update).length) { |
104 | publishing.set(true) |
105 | |
106 | // confirm publish |
107 | api.message.async.publish(extend({ |
108 | type: 'about', |
109 | about: id |
110 | }, update), (err, msg) => { |
111 | publishing.set(false) |
112 | if (err) { |
113 | showDialog({ |
114 | type: 'error', |
115 | title: i18n('Error'), |
116 | buttons: [i18n('OK')], |
117 | message: i18n('An error occurred while attempting to publish about message.'), |
118 | detail: err.message |
119 | }) |
120 | } else if (msg) { |
121 | // user confirmed publish, so close the dialog |
122 | close() |
123 | } |
124 | }) |
125 | } else { |
126 | showDialog({ |
127 | type: 'info', |
128 | title: i18n('Update Profile'), |
129 | buttons: [i18n('OK')], |
130 | message: i18n('Nothing to publish'), |
131 | detail: i18n('You have not made any changes.') |
132 | }) |
133 | close() |
134 | } |
135 | } |
136 | }) |
137 | }) |
138 | } |
139 | |
140 | function FocusHook () { |
141 | return function (element) { |
142 | setTimeout(() => { |
143 | element.focus() |
144 | element.select() |
145 | }, 5) |
146 | } |
147 | } |
148 | |
149 | function ValueHook (obs) { |
150 | return function (element) { |
151 | element.value = obs() |
152 | element.oninput = function () { |
153 | obs.set(element.value.trim()) |
154 | } |
155 | } |
156 | } |
157 | |
158 | function showDialog (opts) { |
159 | var electron = require('electron') |
160 | electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), opts) |
161 | } |
162 |
Built with git-ssb-web