forked from xanderv87/meteor-editable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editable.js
282 lines (256 loc) · 9.24 KB
/
editable.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
mEditable = {
_types: new Meteor.Collection(null),
getTemplate: function (type) {
var t = this._types.findOne({_id: type });
if (!t)
throw new Meteor.Error(500, 'Editable type ' + type + ' is not defined.');
return Template[t.template] || null;
},
getVal: function (type) {
return this._types.findOne({_id: type }).getVal;
},
addType: function (type) {
type._id = type.type;
delete type.type;
// allow users to override types
this._types.remove({_id: type._id });
check(type, {
_id: Match.Where(function (t) {
check(t, String);
return t !== '';
}),
getVal: Function,
template: Match.Where(function (t) {
return typeof t === 'object';
})
});
// store only the template name
if (type.template.viewName) {
type.template = type.template.viewName.replace(/Template\./, '');
} else if (!type.template.kind) {
type.template = type.template.__templateName;
} else {
type.template = type.template.kind.replace(/^Template_/, '');
}
return this._types.insert(type);
}
};
var m_editable = Template['m_editable_main'];
var POSSIBLE_POSITIONS = ['left', 'right', 'top', 'bottom'];
Template.m_editable.helpers({ 'settings': function () { return generateSettings(this); } });
m_editable.helpers({
'm_editable_handle': function () {
if (this.handle === 'span')
return Template.m_editable_handle_span;
return Template.m_editable_handle_atag;
},
'displayVal': function () {
value = Session.get('displayValue');
if (!value) {
Session.set('displayValue', true);
}
return valueToDisplay(this);
},
'value': function () { return valueToText(this.value, this.source) || this.emptyText; },
'editableEmpty': function () {
var v = valueToText(this.value, this.source);
if (typeof this.display === 'function') {
v = this.display(v, this.value);
}
return !v.trim() ? 'editable-empty' : '';
},
'inputTemplate': function () { return mEditable.getTemplate(this.type); }
// can't get tmpl in this context else I'd do this:
// 'loading': function (a,b) {
// return tmpl.Session.get('loading');
// }
});
m_editable.events({
'resize .editable-container': function (e, tmpl) {
resizePopover(tmpl.$('.m_editable-popup'), this.position);
},
'submit': function (e, tmpl) {
var self = this;
var val = mEditable.getVal(self.type)(tmpl.$('.editable-input'));
if (typeof self.onsubmit === 'function') {
if (self.async) {
tmpl.Session.set('loading', true);
self.onsubmit.call(self, val, function () {
tmpl.$('.m_editable-popup').trigger('hide');
doSavedTransition(tmpl);
});
return;
}
self.onsubmit.call(self, val);
}
self.value = val;
Session.set('displayValue', true);
tmpl.$('.m_editable-popup').trigger('hide');
doSavedTransition(tmpl);
},
'click .editable-cancel': function (e, tmpl) {
tmpl.$('.m_editable-popup').trigger('hide');
},
'submit .editableform': function (e) {
e.preventDefault();
},
'click .editable-click': function (e, tmpl) {
tmpl.$('.m_editable-popup').trigger(!tmpl.Session.get('popover-visible') ? 'show' : 'hide');
},
'hidden .m_editable-popup': function (e, tmpl) {
tmpl.Session.set('loading', false);
},
'shown .m_editable-popup': function (e, tmpl) {
tmpl.$('.editable-focus').first().focus();
},
'hide .m_editable-popup': function (e, tmpl) {
if (tmpl.Session.equals('popover-visible', false)) {
e.stopImmediatePropagation();
return;
}
tmpl.Session.set('popover-visible', false);
setTimeout(function () {
$(e.target).trigger('hidden');
}, 325); // 325 seems to be the magic number (for my desktop at least) so the user doesn't see the form show up again
},
'show .m_editable-popup': function (e, tmpl) {
if (tmpl.Session.equals('popover-visible', true)) {
e.stopImmediatePropagation();
return;
}
tmpl.Session.set('popover-visible', true);
setTimeout(function () {
$(e.target).trigger('shown');
}, 0);
}
});
m_editable.rendered = function () {
var self = this;
var $popover = self.$('.m_editable-popup');
self.Deps.autorun(function () {
var loading = self.Session.get('loading');
if (typeof loading === 'undefined')
return;
if (loading) {
self.$('.editableform').hide();
self.$('.editableform-loading').show();
} else {
self.$('.editableform-loading').hide();
self.$('.editableform').show();
}
});
self.Deps.autorun(function () {
var visible = self.Session.get('popover-visible');
self.Session.get('loading'); // changes the form size, so need to re-calculate location
var settings = self.Session.get('settings');
if (typeof visible === 'undefined') {
return;
}
if (visible) {
$popover.trigger('show');
$popover.fadeIn();
resizePopover($popover, self.data.position);
} else {
$popover.trigger('hide');
$popover.fadeOut();
}
});
};
function resizePopover ($popover, placement) {
var actualWidth = $popover[0].offsetWidth,
actualHeight = $popover[0].offsetHeight,
pos = $.fn.tooltip.Constructor.prototype.getPosition.call({ $element: $popover.siblings('.editable-click') });
var calculatedOffset = $.fn.tooltip.Constructor.prototype.getCalculatedOffset(placement, pos, actualWidth, actualHeight);
$.fn.tooltip.Constructor.prototype.applyPlacement.call({
tip: function () { return $popover; },
replaceArrow: function (delta, dimension, position) { $popover.find('.arrow').css(position, delta ? (50 * (1 - delta / dimension) + '%') : ''); }
}, calculatedOffset, placement);
}
Meteor.startup(function () {
$(document).on('click.m_editable-popover-close', function (e) {
$('.m_editable-popup:visible').each(function () {
var $popover = $(this);
if (!$popover.is(e.target) &&
!$popover.siblings('.m_editable-popup-handle').is(e.target) &&
$popover.has(e.target).length === 0 &&
$popover.siblings('.m_editable-popup-handle').has(e.target).length === 0) {
$popover.trigger('hide');
}
});
});
});
function valueToText(val, source) {
val = val || '';
val = _.isArray(val) ? val : [val];
if (source && source.length) {
return _.map(val, function (v, i) {
_.each(source, function (s) {
if (v.toString() === s.value.toString()) {
v = s.text;
}
});
return v;
}).join(', ');
}
// if we got this far, return the original value
return val[0];
}
function valueToDisplay(tmpl) {
var v = valueToText(tmpl.value, tmpl.source) || tmpl.emptyText;
if (typeof tmpl.display === 'function') {
return tmpl.display(v, tmpl.value) || tmpl.emptyText;
}
return v || tmpl.emptyText;
}
function generateSettings (settings) {
if (POSSIBLE_POSITIONS.indexOf(settings.position) == -1)
delete settings.position;
if (!mEditable._types.findOne({_id: settings.type }))
delete settings.type;
return _.extend({
type: 'text',
emptyText: 'Empty',
async: false,
showbuttons: true,
onsubmit: null,
display:null,
value: null,
position: 'left',
title: null,
placeholder: null,
}, settings);
}
function doSavedTransition (tmpl) {
var $e = tmpl.$('.editable-click');
setTimeout(function(){
$e.addClass('editable-bg-transition');
setTimeout(function(){
$e.removeClass('editable-bg-transition');
}, 1700);
}, 10);
}
// Session & Deps stuff
m_editable.destroyed = function () { this.Session.destroyAll(); this.Deps.stopAll(); };
m_editable.created = function () {
var self = this;
self.Deps = {
_handles: [],
stopAll: function () { _.each(this._handles, function (d) { d.stop(); }); },
autorun: function (f) { this._handles.push(Deps.autorun(f)); }
};
self._sessId = Random.id();
self.Session = {
destroyAll: function () {
_.each(Object.keys(Session.keys), function (key) {
var sessCheck = new RegExp('-' + self._sessId + '$');
if(sessCheck.test(key)) {
Session.set([key]);
delete Session.keys[key];
}
});
},
set: function (key, val) { return Session.set(key + '-' + self._sessId, val); },
get: function (key) { return Session.get(key + '-' + self._sessId); },
equals: function (key, val) { return Session.equals(key + '-' + self._sessId, val); }
};
};