-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
160 lines (146 loc) · 4.64 KB
/
Copy pathclient.js
File metadata and controls
160 lines (146 loc) · 4.64 KB
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
(function () {
'use strict';
/**
* Create a new auto complete.
*
* @param {Pagelet} pagelet
* @api private
*/
function autocomplete(pagelet) {
var container = $(pagelet.placeholders)
, target = pagelet.pagelet('target');
//
// Add support for adding new services
//
var select = container.find('header select').selectize({
create: false, // Do not allow creation of new values.
maxOptions: 15, // Maximum items in the dropdown.
onItemAdd: function added(item) {
var data;
pagelet.data.services.some(function some(service) {
if (item === service.name) data = service;
return !!data;
});
target.render(data);
$(target.placeholders).show();
},
render: {
/**
* Return an empty string so we can remove the `do you want to create bla
* bla bla` from the UI.
*
* @returns {String}
* @param {Function} escape Custom HTML escaper.
* @api private
*/
option_create: function create(data, escape) {
return '';
},
/**
* Custom layout renderer for items.
*
* @param {Object} item The thing returned from the server.
* @param {Function} escape Custom HTML escaper.
* @returns {String}
* @api private
*/
option: function option(item, escape) {
var pretty = item.text.slice(0, 1).toUpperCase() + item.text.slice(1);
return [
'<div class="completed">',
'<s class="icon '+ escape(item.text) +'"></s>',
'<strong class="name">'+ escape(pretty) +'</strong>',
'</div>'
].join('');
}
}
})[0];
//
// Hide form fields when canceled.
//
container.on('click', 'button[name="cancel"]', function click() {
if (select) {
select.selectize.clear(); // Reset the auto select/dropdown.
}
target.render(''); // Nuke the HTML
$(target.placeholders).hide(); // Hide the parent element.
});
}
pipe.on('notifications:target:render', function (pagelet) {
var notifications = pagelet.pipe.get('notifications');
//
// We're not extended with an autocomplete, bail out.
//
if (!('autocomplete' in notifications)) return;
/**
* Custom layout renderer for items.
*
* @param {Object} item The thing returned from the server.
* @param {Function} escape Custom HTML escaper.
* @returns {String}
* @api private
*/
$('input[name="package"]', pagelet.placeholders).selectize({
valueField: 'name',
labelField: 'name',
searchField: 'name',
maxOptions: 5, // Maximum items in the dropdown.
openOnFocus: false, // Open dropdown on focus.
createOnBlur: true, // Blur input, create item.
maxItems: 1, // Only allow one module.
create: true,
load: function load(query, callback) {
if (!query.length) return callback();
notifications.autocomplete(query, function autocomplete(err, results) {
if (err) return callback();
callback(results);
});
},
render: {
/**
* Return an empty string so we can remove the `do you want to create bla
* bla bla` from the UI.
*
* @returns {String}
* @param {Function} escape Custom HTML escaper.
* @api private
*/
option_create: function create(data, escape) {
return '';
},
/**
* Custom layout renderer for items.
*
* @param {Object} item The thing returned from the server.
* @param {Function} escape Custom HTML escaper.
* @returns {String}
* @api private
*/
option: function option(item, escape) {
return [
'<div class="completed">',
'<strong class="name">'+ escape(item.name) +'</strong>',
'string' === typeof item.desc ? '<span class="description">'+ escape(item.desc) +'</span>' : '',
'</div>'
].join('');
}
}
});
});
/**
* Hacky as fuck but works like charm. When the targets are updated, update
* the notifcations.
*
* @api private
*/
function submission(pagelet) {
pagelet.once('render', function loading() {
pagelet.once('render', function update() {
pipe.get('notifications').get();
});
});
}
pipe.on('notifications:render', autocomplete)
.on('targets:submit', submission)
.on('targets:render', autocomplete);
}());