-
Notifications
You must be signed in to change notification settings - Fork 300
/
dialog.js
244 lines (207 loc) · 7.41 KB
/
dialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function()
{
const api = require("ko/share/slack/api");
const ss = require("ko/simple-storage").get("slack");
const log = require("ko/logging").getLogger("slack/dialog");
const w = require("ko/windows").getMain();
var panel;
var elem = {};
/**
* Trigger a dialog for a user to fill in fields for posting to slack
* Asks for (* means madatory):
* Title (Defaults to ` ` of `filename-snippet`, if changed gets saved to prefs)
* Channels* (Saves last select)
* Message (No default, not saved)
*
* @returns {Object} postData object holding the about values
* {
* "title":"Posted from Komodo IDE",
* "channels":"a,b,c",
* "message":"Look what I made!"
* }
*/
this.create = function(params, callback)
{
this.close();
// Have to open the panel before appending anything
// XXX this should check for a panel and not recreate it if it already
// exists.
panel = require("ko/ui/panel").create({
anchor: require("ko/windows").getMostRecent().document.documentElement,
attributes: {
backdrag: true,
noautohide: true,
width: "450px",
class: "dialog"
}
});
panel.open();
panel.addRow(require("ko/ui/label").create("Share "+(title || "")+" on Slack"),
{attributes: {flex: 1, align: "center", pack: "center"}});
var title = params.title || ss.storage.title || "";
elem.titleField = createTitleField(title);
panel.add(elem.titleField);
elem.titleField.focus();
elem.availableChannels = createChannelField();
panel.add(elem.availableChannels);
elem.msgField = createMsgField();
panel.add(elem.msgField);
// Force placeholder, why timeout? XUL. The answer is XUL. And facepalms.
// Honestly I don't know why this doesn't work without a timeout, but
// this is XUL and I don't have time for its nonsense.
setTimeout(function() {
elem.msgField.element.placeholder = "Message (optional)";
}, 100);
elem.postButton = createPostBtn();
elem.postButton.onCommand(onPost.bind(this, false, params, callback));
elem.postAndOpenButton = createPostAndOpenBtn();
elem.postAndOpenButton.onCommand(onPost.bind(this, true, params, callback));
elem.closeButton = createCloseBtn();
elem.closeButton.onCommand(this.close);
panel.addRow([elem.postButton, elem.postAndOpenButton, elem.closeButton],
{attributes: {flex: 1, align: "center", pack: "center"}});
// Must retrieve the channels async as they might need to be retrieved
// through an API call.
var populateChannels = function (channels)
{
var selectedChannel = ss.storage.selectedChannel;
//Populate the list and create a list of items to select
var select = [];
for ( let channel of channels ) {
let opts = {attributes:{label:"# " + channel, value:channel}};
let listitem = require("ko/ui/menuitem").create(opts);
if(channel == selectedChannel)
{
listitem.$element.attr("selected", "true");
}
elem.availableChannels.addMenuItem(listitem);
}
elem.availableChannels.enable();
elem.postButton.enable();
elem.postAndOpenButton.enable();
};
api.getChannels(populateChannels);
};
this.close = function()
{
if ( ! panel)
return;
panel.close();
panel.element.remove();
panel = null;
};
function onPost(openInBrowser, params, callback)
{
params.title = elem.titleField.value();
params.initial_comment = elem.msgField.value();
params.openInBrowser = openInBrowser;
var channel = elem.availableChannels.value();
if ( ! channel)
{
require("notify/notify").interact("You must choose at least one channel", "slack", {priority: "warn"});
return;
}
ss.storage.selectedChannel = channel;
params.channels = channel;
var disabled = [];
for (let k in elem)
{
if ("disable" in elem[k])
{
elem[k].disable();
disabled.push(elem[k]);
}
}
api.post(params, function(code, respText)
{
for (let elem of disabled)
elem.enable();
onPostComplete(params, code, respText, callback);
}.bind(this));
}
function onPostComplete(params, code, respText, callback)
{
/** Write a proper callback that handles results from the API using
* notify. See errors section for API function:
* https://api.slack.com/methods/files.upload
*/
var respJSON;
try
{
respJSON = JSON.parse(respText);
} catch(e)
{
log.error("Could not parse response from server: " + e);
callback(false);
return;
}
if( respJSON.ok )
{
var file = respJSON.file;
var url = file.permalink;
if (params.openInBrowser)
{
ko.browse.openUrlInDefaultBrowser(url);
}
log.debug("code: "+code+"\nResponse: "+respText);
callback(true, url);
require("ko/share/slack/dialog").close();
}
else
{
log.warn("Slack share failed: \n" + respText);
callback(false, null, code, respText);
}
}
// Title text field
function createTitleField(title)
{
var options = { attributes:
{
value: title,
placeholder: "Title",
col: 120,
flex: 1
}};
return require("ko/ui/textbox").create(options);
}
// Channel selection list
function createChannelField()
{
var options = { attributes:
{
label: "Select a Channel",
disabled: "true"
}
};
var availableChannels = require("ko/ui/menulist").create(options);
availableChannels.disable();
return availableChannels;
}
// Message Text field
function createMsgField() {
var options = {
attributes:
{
multiline: true,
rows: 3
}
};
return require("ko/ui/textbox").create(options);
}
// Post button
function createPostBtn()
{
return require("ko/ui/button").create({attributes:{label:"Share on Slack",disabled:true}});
}
// Post button
function createPostAndOpenBtn()
{
return require("ko/ui/button").create({attributes:{label:"Share & Open Browser",disabled:true}});
}
// Close Button
function createCloseBtn()
{
return require("ko/ui/button").create({attributes:{label:"Cancel"}});
}
}).apply(module.exports);