recordselect 3.10.8 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README +37 -1
- data/app/assets/javascripts/record_select.js +656 -0
- data/config/locales/en.yml +2 -2
- data/lib/record_select/config.rb +0 -12
- data/lib/record_select/engine.rb +1 -1
- data/lib/record_select/helpers/record_select_helper.rb +40 -25
- data/lib/record_select/version.rb +3 -3
- metadata +4 -6
- data/app/assets/javascripts/jquery/record_select.js +0 -632
- data/app/assets/javascripts/prototype/record_select.js +0 -429
- data/app/assets/javascripts/record_select.js.erb +0 -6
@@ -0,0 +1,656 @@
|
|
1
|
+
//= require jquery.visible.min
|
2
|
+
|
3
|
+
|
4
|
+
(function() {
|
5
|
+
var recordselectInit = function($, undefined) {
|
6
|
+
|
7
|
+
if (typeof(Class) === 'undefined') {
|
8
|
+
/* Simple JavaScript Inheritance for ES 5.1
|
9
|
+
* based on http://ejohn.org/blog/simple-javascript-inheritance/
|
10
|
+
* (inspired by base2 and Prototype)
|
11
|
+
* MIT Licensed.
|
12
|
+
*/
|
13
|
+
(function(global) {
|
14
|
+
"use strict";
|
15
|
+
var fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
|
16
|
+
|
17
|
+
// The base Class implementation (does nothing)
|
18
|
+
function BaseClass(){}
|
19
|
+
|
20
|
+
// Create a new Class that inherits from this class
|
21
|
+
BaseClass.extend = function(props) {
|
22
|
+
var _super = this.prototype;
|
23
|
+
|
24
|
+
// Set up the prototype to inherit from the base class
|
25
|
+
// (but without running the init constructor)
|
26
|
+
var proto = Object.create(_super);
|
27
|
+
|
28
|
+
// Copy the properties over onto the new prototype
|
29
|
+
for (var name in props) {
|
30
|
+
// Check if we're overwriting an existing function
|
31
|
+
proto[name] = typeof props[name] === "function" &&
|
32
|
+
typeof _super[name] == "function" && fnTest.test(props[name])
|
33
|
+
? (function(name, fn){
|
34
|
+
return function() {
|
35
|
+
var tmp = this._super;
|
36
|
+
|
37
|
+
// Add a new ._super() method that is the same method
|
38
|
+
// but on the super-class
|
39
|
+
this._super = _super[name];
|
40
|
+
|
41
|
+
// The method only need to be bound temporarily, so we
|
42
|
+
// remove it when we're done executing
|
43
|
+
var ret = fn.apply(this, arguments);
|
44
|
+
this._super = tmp;
|
45
|
+
|
46
|
+
return ret;
|
47
|
+
};
|
48
|
+
})(name, props[name])
|
49
|
+
: props[name];
|
50
|
+
}
|
51
|
+
|
52
|
+
// The new constructor
|
53
|
+
var newClass = typeof proto.init === "function"
|
54
|
+
? proto.hasOwnProperty("init")
|
55
|
+
? proto.init // All construction is actually done in the init method
|
56
|
+
: function SubClass(){ _super.init.apply(this, arguments); }
|
57
|
+
: function EmptyClass(){};
|
58
|
+
|
59
|
+
// Populate our constructed prototype object
|
60
|
+
newClass.prototype = proto;
|
61
|
+
|
62
|
+
// Enforce the constructor to be what we expect
|
63
|
+
proto.constructor = newClass;
|
64
|
+
|
65
|
+
// And make this class extendable
|
66
|
+
newClass.extend = BaseClass.extend;
|
67
|
+
|
68
|
+
return newClass;
|
69
|
+
};
|
70
|
+
|
71
|
+
// export
|
72
|
+
global.Class = BaseClass;
|
73
|
+
})(window);
|
74
|
+
}
|
75
|
+
|
76
|
+
/*
|
77
|
+
jQuery delayed observer
|
78
|
+
(c) 2007 - Maxime Haineault (max@centdessin.com)
|
79
|
+
|
80
|
+
Special thanks to Stephen Goguen & Tane Piper.
|
81
|
+
|
82
|
+
Slight modifications by Elliot Winkler
|
83
|
+
*/
|
84
|
+
|
85
|
+
if (typeof(jQuery.fn.delayedObserver) === 'undefined') {
|
86
|
+
(function($){
|
87
|
+
$.extend($.fn, {
|
88
|
+
delayedObserver: function(callback, delay, options){
|
89
|
+
return this.each(function(){
|
90
|
+
var el = $(this);
|
91
|
+
var op = options || {};
|
92
|
+
el.data('oldval', el.val())
|
93
|
+
.data('delay', delay === 0 ? delay : (delay || 0.5))
|
94
|
+
.data('condition', op.condition || function() { return ($(this).data('oldval') == $(this).val()); })
|
95
|
+
.data('callback', callback)
|
96
|
+
[(op.event||'keyup')](function(){
|
97
|
+
if (el.data('condition').apply(el)) { return; }
|
98
|
+
else {
|
99
|
+
if (el.data('timer')) { clearTimeout(el.data('timer')); }
|
100
|
+
el.data('timer', setTimeout(function(){
|
101
|
+
var callback = el.data('callback');
|
102
|
+
if (callback) callback.apply(el);
|
103
|
+
}, el.data('delay') * 1000));
|
104
|
+
el.data('oldval', el.val());
|
105
|
+
}
|
106
|
+
});
|
107
|
+
});
|
108
|
+
}
|
109
|
+
});
|
110
|
+
})(jQuery);
|
111
|
+
}
|
112
|
+
|
113
|
+
jQuery(document).ready(function() {
|
114
|
+
RecordSelect.document_loaded = true;
|
115
|
+
jQuery('[data-rs-type]').each(function() { RecordSelect.from_attributes(jQuery(this)); });
|
116
|
+
jQuery(document).on('click', 'div.record-select li.record', function(event) {
|
117
|
+
var link = jQuery(this);
|
118
|
+
if (link.length) {
|
119
|
+
RecordSelect.select_item(link);
|
120
|
+
return false;
|
121
|
+
}
|
122
|
+
return true;
|
123
|
+
});
|
124
|
+
jQuery(document).on('click', '.record-select-option .remove', function(event) {
|
125
|
+
var line = jQuery(this).parent(), value = line.find(':input:hidden').val();
|
126
|
+
line.parent().data('recordselect').obj.trigger('recordselect:remove', [value]);
|
127
|
+
line.remove();
|
128
|
+
return false;
|
129
|
+
});
|
130
|
+
jQuery(document).on('ajax:beforeSend', '.record-select-container', function(event, xhr) {
|
131
|
+
var rs = jQuery(this).data('recordselect'), cur = rs.current_xhr, found = jQuery(this).find('.found');
|
132
|
+
jQuery(this).find('.record, .pagination').remove();
|
133
|
+
found.html(found.data('searching'));
|
134
|
+
rs.current_xhr = xhr;
|
135
|
+
console.log(rs.current_xhr);
|
136
|
+
if (cur) cur.abort();
|
137
|
+
});
|
138
|
+
jQuery(document).on('ajax:complete', '.record-select-container', function(event, xhr, status) {
|
139
|
+
var rs = jQuery(this).data('recordselect');
|
140
|
+
if (status != 'abort' || rs.current_xhr == xhr) {
|
141
|
+
if (rs.is_open()) rs.show();
|
142
|
+
rs.current_xhr = null;
|
143
|
+
}
|
144
|
+
});
|
145
|
+
jQuery(document).on('click', 'input.recordselect ~ .clear-input-button', function() {
|
146
|
+
var $clear_button = jQuery(event.target), $input = $clear_button.prevAll('input');
|
147
|
+
if (!$input.length) return;
|
148
|
+
$input.val('').removeClass('selected');
|
149
|
+
$clear_button.removeClass('enabled');
|
150
|
+
});
|
151
|
+
jQuery(document).on('input recordselect:change', 'input.recordselect', function(event) {
|
152
|
+
var $clear_button = jQuery(event.target).nextAll('.clear-input-button').first();
|
153
|
+
if (!$clear_button.length) return;
|
154
|
+
if (jQuery(event.target).val()) $clear_button.addClass('enabled');
|
155
|
+
else $clear_button.removeClass('enabled');
|
156
|
+
});
|
157
|
+
jQuery(document).on('ajax:beforeSend', 'a.rs-mode', function() {
|
158
|
+
$(this).closest('.record-select').find('form input[name="rs_mode"]').val($(this).data('value'));
|
159
|
+
});
|
160
|
+
});
|
161
|
+
|
162
|
+
window.RecordSelect = new Object();
|
163
|
+
RecordSelect.document_loaded = false;
|
164
|
+
|
165
|
+
RecordSelect.from_attributes = function(item) {
|
166
|
+
var rs_class = RecordSelect[item.data('rs-type')];
|
167
|
+
new rs_class(item.data('rs-id'), item.data('rs-url'), item.data('rs-options'));
|
168
|
+
}
|
169
|
+
|
170
|
+
RecordSelect.select_item = function(item) {
|
171
|
+
var rs = item.closest('.record-select-handler').data('recordselect');
|
172
|
+
if (rs) {
|
173
|
+
try {
|
174
|
+
var label = item.find('label').first().text().trim(), text = item.text().trim();
|
175
|
+
rs.obj.focus();
|
176
|
+
rs.onselect(item.attr('id').substr(2), label || text, text, item);
|
177
|
+
} catch(e) {
|
178
|
+
alert(e);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
RecordSelect.observe = function(form) {
|
184
|
+
if (typeof(form) == 'string') form = '#' + form;
|
185
|
+
form = jQuery(form);
|
186
|
+
var min_length = 0, rs = form.closest('.record-select-container').data('recordselect');
|
187
|
+
if (rs) min_length = rs.min_length;
|
188
|
+
var callback = function() {
|
189
|
+
if (form.closest('body').length) form.trigger("submit");
|
190
|
+
};
|
191
|
+
var delay = parseFloat(rs.obj.data('rsDelay'));
|
192
|
+
if (isNaN(delay)) delay = 0.35;
|
193
|
+
form.find('input.text-input').delayedObserver(callback, delay, {
|
194
|
+
condition: function() {
|
195
|
+
var item = jQuery(this);
|
196
|
+
return item.data('oldval') == item.val() || item.val().length < min_length;
|
197
|
+
}
|
198
|
+
}).on('paste', function() {
|
199
|
+
if (form.closest('body').length) form.trigger("submit");
|
200
|
+
});
|
201
|
+
}
|
202
|
+
|
203
|
+
RecordSelect.render_page = function(record_select_id, page) {
|
204
|
+
jQuery('#' + record_select_id + ' ol').first().replaceWith(page);
|
205
|
+
};
|
206
|
+
|
207
|
+
RecordSelect.Abstract = Class.extend({
|
208
|
+
/**
|
209
|
+
* obj - the id or element that will anchor the recordselect to the page
|
210
|
+
* url - the url to run the recordselect
|
211
|
+
* options - ??? (check concrete classes)
|
212
|
+
*/
|
213
|
+
init: function(obj, url, options) {
|
214
|
+
if (typeof(obj) == 'string') obj = '#' + obj;
|
215
|
+
this.obj = jQuery(obj);
|
216
|
+
this.url = url;
|
217
|
+
this.options = options;
|
218
|
+
this.container;
|
219
|
+
this.min_length = options.min_length || 0;
|
220
|
+
if (this.options.onchange && typeof this.options.onchange != 'function') {
|
221
|
+
this.options.onchange = eval(this.options.onchange);
|
222
|
+
}
|
223
|
+
if (this.options.onselect && typeof this.options.onselect != 'function') {
|
224
|
+
this.options.onselect = eval(this.options.onselect);
|
225
|
+
}
|
226
|
+
|
227
|
+
if (RecordSelect.document_loaded) {
|
228
|
+
this.onload();
|
229
|
+
} else {
|
230
|
+
var _this = this; jQuery(document).ready(function() { _this.onload(); })
|
231
|
+
}
|
232
|
+
},
|
233
|
+
|
234
|
+
/**
|
235
|
+
* Finish the setup - IE doesn't like doing certain things before the page loads
|
236
|
+
* --override--
|
237
|
+
*/
|
238
|
+
onload: function() {},
|
239
|
+
|
240
|
+
/**
|
241
|
+
* the onselect event handler - when someone clicks on a record
|
242
|
+
* --override--
|
243
|
+
*/
|
244
|
+
onselect: function(id, value, text, item) {
|
245
|
+
alert(id + ': ' + value);
|
246
|
+
},
|
247
|
+
|
248
|
+
/**
|
249
|
+
* opens the recordselect
|
250
|
+
*/
|
251
|
+
open: function() {
|
252
|
+
if (this.is_open()) return;
|
253
|
+
var _this = this;
|
254
|
+
jQuery.rails.fire(_this.obj, 'recordselect:before', this);
|
255
|
+
_this.create_form();
|
256
|
+
_this.container.show();
|
257
|
+
var params = _this.obj.data('params'), text = _this.obj.val();
|
258
|
+
|
259
|
+
var search_params = jQuery.param({search: text});
|
260
|
+
params = params ? [params, search_params].join("&") : search_params;
|
261
|
+
this.current_xhr = jQuery.ajax({
|
262
|
+
url: this.url,
|
263
|
+
data: params,
|
264
|
+
success: function(data, status, xhr) {
|
265
|
+
if (_this.current_xhr != xhr) return;
|
266
|
+
if (status != 'abort') _this.current_xhr = null;
|
267
|
+
_this.container.html(data);
|
268
|
+
if (!_this.container.is(':visible')) _this.close();
|
269
|
+
else {
|
270
|
+
_this.container.find('.text-input').val(_this.obj.val());
|
271
|
+
RecordSelect.observe(_this.container.find('form'));
|
272
|
+
_this.container.hide(); // needed to get right document height to position first time
|
273
|
+
if (text.length >= _this.min_length) _this.show();
|
274
|
+
}
|
275
|
+
}
|
276
|
+
});
|
277
|
+
},
|
278
|
+
|
279
|
+
/**
|
280
|
+
* positions and reveals the recordselect
|
281
|
+
*/
|
282
|
+
show: function() {
|
283
|
+
var offset = this.obj.offset(), scroll = jQuery(window).scrollTop(), window_height = jQuery(window).height();
|
284
|
+
if (this.fixed) offset.top -= scroll; // get fixed position
|
285
|
+
var top = this.obj.outerHeight() + offset.top, document_height = jQuery(document).height();
|
286
|
+
|
287
|
+
this.container.show();
|
288
|
+
var height = this.container.outerHeight();
|
289
|
+
this.container.css('left', offset.left);
|
290
|
+
this.container.css('top', '');
|
291
|
+
this.container.css('bottom', '');
|
292
|
+
if (this.above || (this.fixed || this.body_static) && top + height > window_height) {
|
293
|
+
this.container.css('bottom', window_height - offset.top);
|
294
|
+
} else {
|
295
|
+
var below_space = window_height-(top-scroll), above_space = offset.top - scroll, position;
|
296
|
+
if (below_space < height) {
|
297
|
+
if (above_space >= height) position = 'bottom';
|
298
|
+
else position = above_space < below_space ? 'top' : 'bottom';
|
299
|
+
} else position = 'top';
|
300
|
+
if (position == 'top') this.container.css('top', top);
|
301
|
+
else {
|
302
|
+
var bottom = document_height - offset.top, body_height = $(document.body).height();
|
303
|
+
if (body_height < document_height) bottom -= document_height - body_height;
|
304
|
+
this.container.css('bottom', bottom);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
if (this._use_iframe_mask()) {
|
309
|
+
this.container.after('<iframe src="javascript:false;" class="record-select-mask" />');
|
310
|
+
var mask = this.container.next('iframe');
|
311
|
+
mask.css('left', this.container.css('left'))
|
312
|
+
.css('top', this.container.css('top'));
|
313
|
+
}
|
314
|
+
|
315
|
+
if (this._use_iframe_mask()) {
|
316
|
+
var dimensions = this.container.children().first();
|
317
|
+
mask.css('width', dimensions.css('width'))
|
318
|
+
.css('height', dimensions.css('height'));
|
319
|
+
}
|
320
|
+
},
|
321
|
+
|
322
|
+
/**
|
323
|
+
* closes the recordselect by emptying the container
|
324
|
+
*/
|
325
|
+
close: function() {
|
326
|
+
if (this._use_iframe_mask()) {
|
327
|
+
this.container.next('iframe').remove();
|
328
|
+
}
|
329
|
+
|
330
|
+
this.container.hide();
|
331
|
+
// hopefully by using remove() instead of innerHTML we won't leak memory
|
332
|
+
this.container.children().remove();
|
333
|
+
},
|
334
|
+
|
335
|
+
/**
|
336
|
+
* returns true/false for whether the recordselect is open
|
337
|
+
*/
|
338
|
+
is_open: function() {
|
339
|
+
return jQuery.trim(this.container.html()).length != 0;
|
340
|
+
},
|
341
|
+
|
342
|
+
/**
|
343
|
+
* when the user clicks outside the dropdown
|
344
|
+
*/
|
345
|
+
onbodyclick: function(event) {
|
346
|
+
if (!this.is_open()) return;
|
347
|
+
if (this.container.has(jQuery(event.target)).length > 0) {
|
348
|
+
return;
|
349
|
+
} else if (!this.obj.is(event.target)) {
|
350
|
+
this.close();
|
351
|
+
}
|
352
|
+
},
|
353
|
+
|
354
|
+
/**
|
355
|
+
* creates and initializes (and returns) the recordselect container
|
356
|
+
*/
|
357
|
+
create_container: function() {
|
358
|
+
var e = jQuery("<div />", {'class': "record-select-container record-select-handler"}), rs = this;
|
359
|
+
e.css('display', 'none');
|
360
|
+
e.data('recordselect', rs);
|
361
|
+
jQuery(this.obj).add(this.obj.parents()).each(function() {
|
362
|
+
if (jQuery(this).css('position') == 'fixed') {
|
363
|
+
rs.fixed = jQuery(this);
|
364
|
+
e.css('position', 'fixed');
|
365
|
+
return false;
|
366
|
+
}
|
367
|
+
});
|
368
|
+
jQuery(this.obj).each(function() {
|
369
|
+
if (jQuery(this).data('rs-above')) {
|
370
|
+
rs.above = true;
|
371
|
+
return false;
|
372
|
+
}
|
373
|
+
});
|
374
|
+
jQuery(document.body).append(e);
|
375
|
+
jQuery(document.body).mousedown(jQuery.proxy(this, "onbodyclick"));
|
376
|
+
if (!rs.fixed && e.offsetParent().css('position') == 'static') rs.body_static = true;
|
377
|
+
e.get(0).onselect = jQuery.proxy(this, "onselect")
|
378
|
+
return e;
|
379
|
+
},
|
380
|
+
|
381
|
+
create_form: function() {
|
382
|
+
var div = jQuery('<div>').addClass('record-select').attr('id', this.options.id);
|
383
|
+
var form = jQuery('<form>').attr('action', this.url).attr('data-remote', true).attr('method', 'get');
|
384
|
+
form.append(jQuery('<input type="text" name="search" class="text-input">'));
|
385
|
+
form.append(jQuery('<input type="hidden" name="page" value="1">'));
|
386
|
+
form.append(jQuery('<input type="hidden" name="update" value="1">'));
|
387
|
+
div.append(form).append(jQuery('<ol>'));
|
388
|
+
this.container.html(div);
|
389
|
+
RecordSelect.observe(form);
|
390
|
+
},
|
391
|
+
|
392
|
+
onkeyup: function(event) {
|
393
|
+
if (!this.is_open()) return;
|
394
|
+
this.container.find('.text-input').val(this.obj.val()).trigger(event);
|
395
|
+
},
|
396
|
+
|
397
|
+
onpaste: function(event) {
|
398
|
+
if (!this.is_open()) return;
|
399
|
+
setTimeout(function () {
|
400
|
+
this.container.find('.text-input').val(this.obj.val()).trigger(event);
|
401
|
+
}.bind(this), 0);
|
402
|
+
},
|
403
|
+
|
404
|
+
/**
|
405
|
+
* all the behavior to respond to a text field as a search box
|
406
|
+
*/
|
407
|
+
_respond_to_text_field: function(text_field) {
|
408
|
+
// attach the events to start this party
|
409
|
+
text_field.focus(jQuery.proxy(this, 'open'));
|
410
|
+
|
411
|
+
// the autosearch event - needs to happen slightly late (keyup is later than keypress)
|
412
|
+
text_field.keyup(jQuery.proxy(this, 'onkeyup'));
|
413
|
+
|
414
|
+
// the autosearch event - needs to happen slightly late (keyup is later than keypress)
|
415
|
+
text_field.on('paste', jQuery.proxy(this, 'onpaste'));
|
416
|
+
|
417
|
+
// keyboard navigation, if available
|
418
|
+
if (this.onkeydown) {
|
419
|
+
text_field.keydown(jQuery.proxy(this, "onkeydown"));
|
420
|
+
}
|
421
|
+
},
|
422
|
+
|
423
|
+
_use_iframe_mask: function() {
|
424
|
+
return this.container.insertAdjacentHTML ? true : false;
|
425
|
+
}
|
426
|
+
});
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
/**
|
431
|
+
* Adds keyboard navigation to RecordSelect objects
|
432
|
+
*/
|
433
|
+
jQuery.extend(RecordSelect.Abstract.prototype, {
|
434
|
+
current: null,
|
435
|
+
|
436
|
+
/**
|
437
|
+
* keyboard navigation - where to intercept the keys is up to the concrete class
|
438
|
+
*/
|
439
|
+
onkeydown: function(ev) {
|
440
|
+
var elem;
|
441
|
+
switch (ev.keyCode) {
|
442
|
+
case 38: //Event.KEY_UP
|
443
|
+
if (this.current && this.current.closest('html').length) elem = this.current.prev();
|
444
|
+
if (!elem) elem = this.container.find('ol li.record').last();
|
445
|
+
this.highlight(elem);
|
446
|
+
break;
|
447
|
+
case 40: //Event.KEY_DOWN
|
448
|
+
if (this.current && this.current.closest('html').length) elem = this.current.next();
|
449
|
+
if (!elem) elem = this.container.find('ol li.record').first();
|
450
|
+
this.highlight(elem);
|
451
|
+
break;
|
452
|
+
case 13: // Event.KEY_RETURN
|
453
|
+
if (this.current) this.current.click();
|
454
|
+
break;
|
455
|
+
case 39: // Event.KEY_RIGHT
|
456
|
+
elem = this.container.find('li.pagination.next');
|
457
|
+
if (elem) elem.find('a').click();
|
458
|
+
break;
|
459
|
+
case 37: // Event.KEY_LEFT
|
460
|
+
elem = this.container.find('li.pagination.previous');
|
461
|
+
if (elem) elem.find('a').click();
|
462
|
+
break;
|
463
|
+
case 27: // Event.KEY_ESC
|
464
|
+
case 9: // Event.KEY_TAB
|
465
|
+
this.close();
|
466
|
+
break;
|
467
|
+
default:
|
468
|
+
return true;
|
469
|
+
}
|
470
|
+
if (ev.keyCode != 9) { // don't prevent tabbing
|
471
|
+
ev.preventDefault(); // so "enter" doesn't submit the form, among other things(?)
|
472
|
+
}
|
473
|
+
},
|
474
|
+
|
475
|
+
/**
|
476
|
+
* moves the highlight to a new object
|
477
|
+
*/
|
478
|
+
highlight: function(obj) {
|
479
|
+
if (this.current) this.current.removeClass('current');
|
480
|
+
this.current = jQuery(obj);
|
481
|
+
obj.addClass('current');
|
482
|
+
}
|
483
|
+
});
|
484
|
+
|
485
|
+
/**
|
486
|
+
* Used by link_to_record_select
|
487
|
+
* The options hash should contain a onselect: key, with a javascript function as value
|
488
|
+
*/
|
489
|
+
RecordSelect.Dialog = RecordSelect.Abstract.extend({
|
490
|
+
onload: function() {
|
491
|
+
this.container = this.create_container();
|
492
|
+
this.obj.click(jQuery.proxy(this, "toggle"));
|
493
|
+
if (this.onkeypress) this.obj.keypress(jQuery.proxy(this, 'onkeypress'));
|
494
|
+
},
|
495
|
+
|
496
|
+
onselect: function(id, value, text, item) {
|
497
|
+
if (this.options.onselect(id, value, text, item) != false) this.close();
|
498
|
+
},
|
499
|
+
|
500
|
+
toggle: function(e) {
|
501
|
+
e.preventDefault();
|
502
|
+
if (this.is_open()) this.close();
|
503
|
+
else this.open();
|
504
|
+
}
|
505
|
+
});
|
506
|
+
|
507
|
+
/**
|
508
|
+
* Used by record_select_field helper
|
509
|
+
* The options hash may contain id: and label: keys, designating the current value
|
510
|
+
* The options hash may also include an onchange: key, where the value is a javascript function (or eval-able string) for an callback routine
|
511
|
+
* and field_name: key, where value will be set as name of the input field.
|
512
|
+
*/
|
513
|
+
RecordSelect.Single = RecordSelect.Abstract.extend({
|
514
|
+
onload: function() {
|
515
|
+
var rs = this;
|
516
|
+
// initialize the container
|
517
|
+
this.container = this.create_container();
|
518
|
+
this.container.addClass('record-select-autocomplete');
|
519
|
+
this.container.submit(function() {
|
520
|
+
rs.hidden_input.val('');
|
521
|
+
rs.obj.removeClass('selected');
|
522
|
+
rs.obj.trigger('recordselect:unset', [rs]);
|
523
|
+
});
|
524
|
+
|
525
|
+
// create the hidden input
|
526
|
+
this.obj.after('<input type="hidden" name="" value="" />');
|
527
|
+
this.hidden_input = this.obj.next();
|
528
|
+
|
529
|
+
// transfer the input name from the text input to the hidden input
|
530
|
+
this.hidden_input.attr('name', this.obj.attr('name'));
|
531
|
+
this.obj.attr('name', this.options.field_name || '');
|
532
|
+
|
533
|
+
// initialize the values
|
534
|
+
if (this.options.label) this.set(this.options.id, this.options.label);
|
535
|
+
|
536
|
+
this._respond_to_text_field(this.obj);
|
537
|
+
if (this.obj.prop('focused')) this.open(); // if it was focused before we could attach observers
|
538
|
+
},
|
539
|
+
|
540
|
+
onselect: function(id, value, text, item) {
|
541
|
+
this.set(id, value);
|
542
|
+
if (this.options.onchange) this.options.onchange.call(this, id, value, text, item);
|
543
|
+
this.obj.trigger("recordselect:change", [id, value, text, item]);
|
544
|
+
this.close();
|
545
|
+
},
|
546
|
+
|
547
|
+
/**
|
548
|
+
* sets the id/label
|
549
|
+
*/
|
550
|
+
set: function(id, label) {
|
551
|
+
// unescaped html missing for label
|
552
|
+
this.obj.val(label);
|
553
|
+
this.hidden_input.val(id);
|
554
|
+
this.obj.addClass('selected');
|
555
|
+
}
|
556
|
+
});
|
557
|
+
|
558
|
+
/**
|
559
|
+
* Used by record_select_autocomplete helper
|
560
|
+
* The options hash may contain label: key, designating the current value
|
561
|
+
* The options hash may also include an onchange: key, where the value is a javascript function (or eval-able string) for an callback routine.
|
562
|
+
*/
|
563
|
+
RecordSelect.Autocomplete = RecordSelect.Abstract.extend({
|
564
|
+
onload: function() {
|
565
|
+
// initialize the container
|
566
|
+
this.container = this.create_container();
|
567
|
+
this.container.addClass('record-select-autocomplete');
|
568
|
+
|
569
|
+
// initialize the values
|
570
|
+
if (this.options.label) this.set(this.options.label);
|
571
|
+
|
572
|
+
this._respond_to_text_field(this.obj);
|
573
|
+
if (this.obj.prop('focused')) this.open(); // if it was focused before we could attach observers
|
574
|
+
},
|
575
|
+
|
576
|
+
close: function() {
|
577
|
+
// if they close the dialog with the text field empty, then delete the id value
|
578
|
+
if (this.obj.val() == '') this.set('');
|
579
|
+
|
580
|
+
RecordSelect.Abstract.prototype.close.call(this);
|
581
|
+
},
|
582
|
+
|
583
|
+
onselect: function(id, value, text, item) {
|
584
|
+
this.set(value);
|
585
|
+
if (this.options.onchange) this.options.onchange.call(this, id, value, text, item);
|
586
|
+
this.obj.trigger("recordselect:change", [id, value, text, item]);
|
587
|
+
this.close();
|
588
|
+
},
|
589
|
+
|
590
|
+
/**
|
591
|
+
* sets the id/label
|
592
|
+
*/
|
593
|
+
set: function(label) {
|
594
|
+
// unescaped html missing for label
|
595
|
+
this.obj.val(label);
|
596
|
+
}
|
597
|
+
});
|
598
|
+
|
599
|
+
/**
|
600
|
+
* Used by record_multi_select_field helper.
|
601
|
+
* Options:
|
602
|
+
* list - the id (or object) of the <ul> to contain the <li>s of selected entries
|
603
|
+
* current - an array of id:/label: keys designating the currently selected entries
|
604
|
+
*/
|
605
|
+
RecordSelect.Multiple = RecordSelect.Abstract.extend({
|
606
|
+
onload: function() {
|
607
|
+
// initialize the container
|
608
|
+
this.container = this.create_container();
|
609
|
+
this.container.addClass('record-select-autocomplete');
|
610
|
+
|
611
|
+
// decide where the <li> entries should be placed
|
612
|
+
if (this.options.list) this.list_container = jQuery(this.options.list);
|
613
|
+
else this.list_container = this.obj.siblings('ul');
|
614
|
+
this.list_container.data('recordselect', this);
|
615
|
+
|
616
|
+
// take the input name from the text input, and store it for this.add()
|
617
|
+
this.input_name = this.obj.attr('name');
|
618
|
+
this.obj.attr('name', '');
|
619
|
+
|
620
|
+
// initialize the list
|
621
|
+
for(var i = 0, length = this.options.current.length; i < length; i++) {
|
622
|
+
this.add(this.options.current[i].id, this.options.current[i].label);
|
623
|
+
}
|
624
|
+
|
625
|
+
this._respond_to_text_field(this.obj);
|
626
|
+
if (this.obj.prop('focused')) this.open(); // if it was focused before we could attach observers
|
627
|
+
},
|
628
|
+
|
629
|
+
onselect: function(id, value, text, item) {
|
630
|
+
this.add(id, value);
|
631
|
+
this.obj.trigger("recordselect:add", [id, value, text, item]);
|
632
|
+
},
|
633
|
+
|
634
|
+
/**
|
635
|
+
* Adds a record to the selected list
|
636
|
+
*/
|
637
|
+
add: function(id, label) {
|
638
|
+
// return silently if this value has already been selected
|
639
|
+
if (this.list_container.has('input[value=' + id + ']').length > 0) return;
|
640
|
+
|
641
|
+
var entry = '<li class="record-select-option">'
|
642
|
+
+ '<a href="#" class="remove">remove</a>'
|
643
|
+
+ '<input type="hidden" name="' + this.input_name + '" value="' + id + '" />'
|
644
|
+
+ '<label>' + label + '</label>'
|
645
|
+
+ '</li>';
|
646
|
+
this.list_container.prepend(entry)
|
647
|
+
}
|
648
|
+
});
|
649
|
+
};
|
650
|
+
|
651
|
+
if (window.jQuery) {
|
652
|
+
recordselectInit(jQuery);
|
653
|
+
} else if (typeof exports === 'object' && typeof module === 'object') {
|
654
|
+
module.exports = recordselectInit;
|
655
|
+
}
|
656
|
+
})();
|
data/config/locales/en.yml
CHANGED
data/lib/record_select/config.rb
CHANGED
@@ -13,18 +13,6 @@ module RecordSelect
|
|
13
13
|
@pagination = options.include?(:pagination) ? options[:pagination] : true
|
14
14
|
@toggle_search_mode = options[:toggle_search_mode]
|
15
15
|
end
|
16
|
-
|
17
|
-
def self.js_framework=(framework)
|
18
|
-
@@js_framework = framework
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.js_framework
|
22
|
-
@@js_framework ||= if defined? Jquery
|
23
|
-
:jquery
|
24
|
-
elsif defined? PrototypeRails
|
25
|
-
:prototype
|
26
|
-
end
|
27
|
-
end
|
28
16
|
|
29
17
|
def pagination?
|
30
18
|
@pagination
|
data/lib/record_select/engine.rb
CHANGED