jquery-atwho-rails 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,152 +18,112 @@
18
18
  return factory(window.jQuery);
19
19
  }
20
20
  })(function($) {
21
- var $CONTAINER, Api, Controller, DEFAULT_CALLBACKS, DEFAULT_TPL, KEY_CODE, Model, View;
22
- KEY_CODE = {
23
- DOWN: 40,
24
- UP: 38,
25
- ESC: 27,
26
- TAB: 9,
27
- ENTER: 13
28
- };
29
- DEFAULT_CALLBACKS = {
30
- before_save: function(data) {
31
- var item, _i, _len, _results;
32
- if (!$.isArray(data)) {
33
- return data;
34
- }
35
- _results = [];
36
- for (_i = 0, _len = data.length; _i < _len; _i++) {
37
- item = data[_i];
38
- if ($.isPlainObject(item)) {
39
- _results.push(item);
40
- } else {
41
- _results.push({
42
- name: item
43
- });
44
- }
45
- }
46
- return _results;
47
- },
48
- matcher: function(flag, subtext) {
49
- var match, regexp;
50
- flag = '(?:^|\\s)' + flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
51
- regexp = new RegExp(flag + '([A-Za-z0-9_\+\-]*)$|' + flag + '([^\\x00-\\xff]*)$', 'gi');
52
- match = regexp.exec(subtext);
53
- if (match) {
54
- return match[2] || match[1];
55
- } else {
56
- return null;
57
- }
58
- },
59
- filter: function(query, data, search_key) {
60
- var item, _i, _len, _results;
61
- _results = [];
62
- for (_i = 0, _len = data.length; _i < _len; _i++) {
63
- item = data[_i];
64
- if (~item[search_key].toLowerCase().indexOf(query)) {
65
- _results.push(item);
66
- }
67
- }
68
- return _results;
69
- },
70
- remote_filter: null,
71
- sorter: function(query, items, search_key) {
72
- var item, _i, _len, _results;
73
- if (!query) {
74
- return items;
75
- }
76
- _results = [];
77
- for (_i = 0, _len = items.length; _i < _len; _i++) {
78
- item = items[_i];
79
- item.atwho_order = item[search_key].toLowerCase().indexOf(query);
80
- if (item.atwho_order > -1) {
81
- _results.push(item);
82
- }
83
- }
84
- return _results.sort(function(a, b) {
85
- return a.atwho_order - b.atwho_order;
86
- });
87
- },
88
- tpl_eval: function(tpl, map) {
89
- try {
90
- return tpl.replace(/\$\{([^\}]*)\}/g, function(tag, key, pos) {
91
- return map[key];
92
- });
93
- } catch (error) {
94
- return "";
95
- }
96
- },
97
- highlighter: function(li, query) {
98
- var regexp;
99
- if (!query) {
100
- return li;
101
- }
102
- regexp = new RegExp(">\\s*(\\w*)(" + query.replace("+", "\\+") + ")(\\w*)\\s*<", 'ig');
103
- return li.replace(regexp, function(str, $1, $2, $3) {
104
- return '> ' + $1 + '<strong>' + $2 + '</strong>' + $3 + ' <';
105
- });
106
- },
107
- before_insert: function(value, $li) {
108
- return value;
109
- }
110
- };
111
- Model = (function() {
112
- var _storage;
113
-
114
- _storage = {};
21
+ var $CONTAINER, Api, App, Controller, DEFAULT_CALLBACKS, DEFAULT_TPL, KEY_CODE, Model, View;
22
+ App = (function() {
115
23
 
116
- function Model(context, key) {
117
- this.context = context;
118
- this.key = key;
24
+ function App(inputor) {
25
+ this.current_flag = null;
26
+ this.controllers = {};
27
+ this.$inputor = $(inputor);
28
+ this.listen();
119
29
  }
120
30
 
121
- Model.prototype.saved = function() {
122
- return this.fetch() > 0;
31
+ App.prototype.controller = function(key) {
32
+ return this.controllers[key || this.current_flag];
123
33
  };
124
34
 
125
- Model.prototype.query = function(query, callback) {
126
- var data, search_key, _ref;
127
- data = this.fetch();
128
- search_key = this.context.get_opt("search_key");
129
- callback(data = this.context.callbacks('filter').call(this.context, query, data, search_key));
130
- if (!(data && data.length > 0)) {
131
- return (_ref = this.context.callbacks('remote_filter')) != null ? _ref.call(this.context, query, callback) : void 0;
132
- }
35
+ App.prototype.set_context_for = function(key) {
36
+ this.current_flag = key;
37
+ return this;
133
38
  };
134
39
 
135
- Model.prototype.fetch = function() {
136
- return _storage[this.key] || [];
40
+ App.prototype.reg = function(flag, setting) {
41
+ var controller, _base;
42
+ controller = (_base = this.controllers)[flag] || (_base[flag] = new Controller(this, flag));
43
+ if (setting.alias) {
44
+ this.controllers[setting.alias] = controller;
45
+ }
46
+ controller.init(setting);
47
+ return this;
137
48
  };
138
49
 
139
- Model.prototype.save = function(data) {
140
- return _storage[this.key] = this.context.callbacks("before_save").call(this.context, data || []);
50
+ App.prototype.listen = function() {
51
+ var _this = this;
52
+ return this.$inputor.on('keyup.atwho', function(e) {
53
+ return _this.on_keyup(e);
54
+ }).on('keydown.atwho', function(e) {
55
+ return _this.on_keydown(e);
56
+ }).on('scroll.atwho', function(e) {
57
+ var _ref;
58
+ return (_ref = _this.controller()) != null ? _ref.view.hide() : void 0;
59
+ }).on('blur.atwho', function(e) {
60
+ var c;
61
+ if (c = _this.controller()) {
62
+ return c.view.hide(c.get_opt("display_timeout"));
63
+ }
64
+ });
141
65
  };
142
66
 
143
- Model.prototype.load = function(data) {
144
- if (!(this.saved() || !data)) {
145
- return this._load(data);
146
- }
67
+ App.prototype.dispatch = function() {
68
+ var _this = this;
69
+ return $.map(this.controllers, function(c) {
70
+ if (c.look_up()) {
71
+ return _this.set_context_for(c.key);
72
+ }
73
+ });
147
74
  };
148
75
 
149
- Model.prototype.reload = function(data) {
150
- return this._load(data);
76
+ App.prototype.on_keyup = function(e) {
77
+ var _ref;
78
+ switch (e.keyCode) {
79
+ case KEY_CODE.ESC:
80
+ e.preventDefault();
81
+ if ((_ref = this.controller()) != null) {
82
+ _ref.view.hide();
83
+ }
84
+ break;
85
+ case KEY_CODE.DOWN:
86
+ case KEY_CODE.UP:
87
+ $.noop();
88
+ break;
89
+ default:
90
+ this.dispatch();
91
+ }
151
92
  };
152
93
 
153
- Model.prototype._load = function(data) {
154
- var _this = this;
155
- if (typeof data === "string") {
156
- return $.ajax(data, {
157
- dataType: "json"
158
- }).done(function(data) {
159
- return _this.save(data);
160
- });
161
- } else {
162
- return this.save(data);
94
+ App.prototype.on_keydown = function(e) {
95
+ var view, _ref;
96
+ view = (_ref = this.controller()) != null ? _ref.view : void 0;
97
+ if (!(view && view.visible())) {
98
+ return;
99
+ }
100
+ switch (e.keyCode) {
101
+ case KEY_CODE.ESC:
102
+ e.preventDefault();
103
+ view.hide();
104
+ break;
105
+ case KEY_CODE.UP:
106
+ e.preventDefault();
107
+ view.prev();
108
+ break;
109
+ case KEY_CODE.DOWN:
110
+ e.preventDefault();
111
+ view.next();
112
+ break;
113
+ case KEY_CODE.TAB:
114
+ case KEY_CODE.ENTER:
115
+ if (!view.visible()) {
116
+ return;
117
+ }
118
+ e.preventDefault();
119
+ view.choose();
120
+ break;
121
+ default:
122
+ $.noop();
163
123
  }
164
124
  };
165
125
 
166
- return Model;
126
+ return App;
167
127
 
168
128
  })();
169
129
  Controller = (function() {
@@ -175,57 +135,22 @@
175
135
  return _uuid += 1;
176
136
  };
177
137
 
178
- function Controller(inputor) {
179
- this.id = inputor.id || uuid();
180
- this.settings = {};
181
- this.pos = 0;
182
- this.current_flag = null;
138
+ function Controller(app, key) {
139
+ this.app = app;
140
+ this.key = key;
141
+ this.$inputor = this.app.$inputor;
142
+ this.id = this.$inputor[0].id || uuid();
143
+ this.setting = null;
183
144
  this.query = null;
184
- this.the_flag = {};
185
- this._views = {};
186
- this._models = {};
187
- this.$inputor = $(inputor);
145
+ this.pos = 0;
188
146
  $CONTAINER.append(this.$el = $("<div id='atwho-ground-" + this.id + "'></div>"));
189
- this.listen();
147
+ this.model = new Model(this);
148
+ this.view = new View(this);
190
149
  }
191
150
 
192
- Controller.prototype.listen = function() {
193
- var _this = this;
194
- return this.$inputor.on('keyup.atwho', function(e) {
195
- return _this.on_keyup(e);
196
- }).on('keydown.atwho', function(e) {
197
- return _this.on_keydown(e);
198
- }).on('scroll.atwho', function(e) {
199
- var _ref;
200
- return (_ref = _this.view) != null ? _ref.hide() : void 0;
201
- }).on('blur.atwho', function(e) {
202
- var _ref;
203
- return (_ref = _this.view) != null ? _ref.hide(_this.get_opt("display_timeout")) : void 0;
204
- });
205
- };
206
-
207
- Controller.prototype.set_context_for = function(flag) {
208
- flag = this.current_flag = this.the_flag[flag];
209
- this.view = this._views[flag];
210
- this.model = this._models[flag];
211
- return this;
212
- };
213
-
214
- Controller.prototype.reg = function(flag, settings) {
215
- var setting;
216
- setting = this.settings[flag] = $.extend({}, this.settings[flag] || $.fn.atwho["default"], settings);
217
- this.set_context_for(flag = (setting.alias ? this.the_flag[setting.alias] = flag : void 0, this.the_flag[flag] = flag));
218
- (this._models[flag] = new Model(this, flag)).reload(setting.data);
219
- this._views[flag] = new View(this, flag);
220
- return this;
221
- };
222
-
223
- Controller.prototype.trigger = function(name, data) {
224
- var alias, event_name;
225
- data.push(this);
226
- alias = this.get_opt('alias');
227
- event_name = alias ? "" + name + "-" + alias + ".atwho" : "" + name + ".atwho";
228
- return this.$inputor.trigger(event_name, data);
151
+ Controller.prototype.init = function(setting) {
152
+ this.setting = $.extend({}, this.setting || $.fn.atwho["default"], setting);
153
+ return this.model.reload(this.setting.data);
229
154
  };
230
155
 
231
156
  Controller.prototype.super_call = function() {
@@ -238,44 +163,32 @@
238
163
  }
239
164
  };
240
165
 
166
+ Controller.prototype.trigger = function(name, data) {
167
+ var alias, event_name;
168
+ data.push(this);
169
+ alias = this.get_opt('alias');
170
+ event_name = alias ? "" + name + "-" + alias + ".atwho" : "" + name + ".atwho";
171
+ return this.$inputor.trigger(event_name, data);
172
+ };
173
+
241
174
  Controller.prototype.callbacks = function(func_name) {
242
175
  return this.get_opt("callbacks")[func_name] || DEFAULT_CALLBACKS[func_name];
243
176
  };
244
177
 
245
178
  Controller.prototype.get_opt = function(key, default_value) {
246
179
  try {
247
- return this.settings[this.current_flag][key];
180
+ return this.setting[key];
248
181
  } catch (e) {
249
182
  return null;
250
183
  }
251
184
  };
252
185
 
253
- Controller.prototype.rect = function() {
254
- var c, scale_bottom;
255
- c = this.$inputor.caret('offset', this.pos - 1);
256
- scale_bottom = document.selection ? 0 : 2;
257
- return {
258
- left: c.left,
259
- top: c.top,
260
- bottom: c.top + c.height + scale_bottom
261
- };
262
- };
263
-
264
186
  Controller.prototype.catch_query = function() {
265
- var caret_pos, content, end, query, start, subtext, _ref,
266
- _this = this;
187
+ var caret_pos, content, end, query, start, subtext;
267
188
  content = this.$inputor.val();
268
189
  caret_pos = this.$inputor.caret('pos');
269
190
  subtext = content.slice(0, caret_pos);
270
- query = null;
271
- $.map(this.settings, function(setting) {
272
- var _result;
273
- _result = _this.callbacks("matcher").call(_this, setting.at, subtext);
274
- if (_result != null) {
275
- query = _result;
276
- return _this.set_context_for(setting.at);
277
- }
278
- });
191
+ query = this.callbacks("matcher").call(this, this.key, subtext);
279
192
  if (typeof query === "string" && query.length <= this.get_opt('max_len', 20)) {
280
193
  start = caret_pos - query.length;
281
194
  end = start + query.length;
@@ -285,21 +198,30 @@
285
198
  'head_pos': start,
286
199
  'end_pos': end
287
200
  };
288
- this.trigger("matched", [this.current_flag, query.text]);
201
+ this.trigger("matched", [this.key, query.text]);
289
202
  } else {
290
- if ((_ref = this.view) != null) {
291
- _ref.hide();
292
- }
203
+ this.view.hide();
293
204
  }
294
205
  return this.query = query;
295
206
  };
296
207
 
208
+ Controller.prototype.rect = function() {
209
+ var c, scale_bottom;
210
+ c = this.$inputor.caret('offset', this.pos - 1);
211
+ scale_bottom = document.selection ? 0 : 2;
212
+ return {
213
+ left: c.left,
214
+ top: c.top,
215
+ bottom: c.top + c.height + scale_bottom
216
+ };
217
+ };
218
+
297
219
  Controller.prototype.insert = function(str) {
298
220
  var $inputor, flag_len, source, start_str, text;
299
221
  $inputor = this.$inputor;
300
222
  str = '' + str;
301
223
  source = $inputor.val();
302
- flag_len = this.get_opt("display_flag") ? 0 : this.current_flag.length;
224
+ flag_len = this.get_opt("display_flag") ? 0 : this.key.length;
303
225
  start_str = source.slice(0, (this.query['head_pos'] || 0) - flag_len);
304
226
  text = "" + start_str + str + " " + (source.slice(this.query['end_pos'] || 0));
305
227
  $inputor.val(text);
@@ -307,52 +229,6 @@
307
229
  return $inputor.change();
308
230
  };
309
231
 
310
- Controller.prototype.on_keyup = function(e) {
311
- switch (e.keyCode) {
312
- case KEY_CODE.ESC:
313
- e.preventDefault();
314
- this.view.hide();
315
- break;
316
- case KEY_CODE.DOWN:
317
- case KEY_CODE.UP:
318
- $.noop();
319
- break;
320
- default:
321
- this.look_up();
322
- }
323
- };
324
-
325
- Controller.prototype.on_keydown = function(e) {
326
- var _ref;
327
- if (!((_ref = this.view) != null ? _ref.visible() : void 0)) {
328
- return;
329
- }
330
- switch (e.keyCode) {
331
- case KEY_CODE.ESC:
332
- e.preventDefault();
333
- this.view.hide();
334
- break;
335
- case KEY_CODE.UP:
336
- e.preventDefault();
337
- this.view.prev();
338
- break;
339
- case KEY_CODE.DOWN:
340
- e.preventDefault();
341
- this.view.next();
342
- break;
343
- case KEY_CODE.TAB:
344
- case KEY_CODE.ENTER:
345
- if (!this.view.visible()) {
346
- return;
347
- }
348
- e.preventDefault();
349
- this.view.choose();
350
- break;
351
- default:
352
- $.noop();
353
- }
354
- };
355
-
356
232
  Controller.prototype.render_view = function(data) {
357
233
  var search_key;
358
234
  search_key = this.get_opt("search_key");
@@ -372,17 +248,76 @@
372
248
  return this.view.hide();
373
249
  }
374
250
  };
375
- return this.model.query(query.text, $.proxy(_callback, this));
251
+ this.model.query(query.text, $.proxy(_callback, this));
252
+ return query;
376
253
  };
377
254
 
378
255
  return Controller;
379
256
 
257
+ })();
258
+ Model = (function() {
259
+ var _storage;
260
+
261
+ _storage = {};
262
+
263
+ function Model(context) {
264
+ this.context = context;
265
+ this.key = this.context.key;
266
+ }
267
+
268
+ Model.prototype.saved = function() {
269
+ return this.fetch() > 0;
270
+ };
271
+
272
+ Model.prototype.query = function(query, callback) {
273
+ var data, search_key, _ref;
274
+ data = this.fetch();
275
+ search_key = this.context.get_opt("search_key");
276
+ callback(data = this.context.callbacks('filter').call(this.context, query, data, search_key));
277
+ if (!(data && data.length > 0)) {
278
+ return (_ref = this.context.callbacks('remote_filter')) != null ? _ref.call(this.context, query, callback) : void 0;
279
+ }
280
+ };
281
+
282
+ Model.prototype.fetch = function() {
283
+ return _storage[this.key] || [];
284
+ };
285
+
286
+ Model.prototype.save = function(data) {
287
+ return _storage[this.key] = this.context.callbacks("before_save").call(this.context, data || []);
288
+ };
289
+
290
+ Model.prototype.load = function(data) {
291
+ if (!(this.saved() || !data)) {
292
+ return this._load(data);
293
+ }
294
+ };
295
+
296
+ Model.prototype.reload = function(data) {
297
+ return this._load(data);
298
+ };
299
+
300
+ Model.prototype._load = function(data) {
301
+ var _this = this;
302
+ if (typeof data === "string") {
303
+ return $.ajax(data, {
304
+ dataType: "json"
305
+ }).done(function(data) {
306
+ return _this.save(data);
307
+ });
308
+ } else {
309
+ return this.save(data);
310
+ }
311
+ };
312
+
313
+ return Model;
314
+
380
315
  })();
381
316
  View = (function() {
382
317
 
383
- function View(context, key) {
318
+ function View(context) {
384
319
  this.context = context;
385
- this.key = key;
320
+ this.key = this.context.key;
386
321
  this.id = this.context.get_opt("alias") || ("at-view-" + (this.key.charCodeAt(0)));
387
322
  this.$el = $("<div id='" + this.id + "' class='atwho-view'><ul id='" + this.id + "-ul' class='atwho-view-url'></ul></div>");
388
323
  this.timeout_id = null;
@@ -493,22 +428,113 @@
493
428
  return View;
494
429
 
495
430
  })();
431
+ KEY_CODE = {
432
+ DOWN: 40,
433
+ UP: 38,
434
+ ESC: 27,
435
+ TAB: 9,
436
+ ENTER: 13
437
+ };
438
+ DEFAULT_CALLBACKS = {
439
+ before_save: function(data) {
440
+ var item, _i, _len, _results;
441
+ if (!$.isArray(data)) {
442
+ return data;
443
+ }
444
+ _results = [];
445
+ for (_i = 0, _len = data.length; _i < _len; _i++) {
446
+ item = data[_i];
447
+ if ($.isPlainObject(item)) {
448
+ _results.push(item);
449
+ } else {
450
+ _results.push({
451
+ name: item
452
+ });
453
+ }
454
+ }
455
+ return _results;
456
+ },
457
+ matcher: function(flag, subtext) {
458
+ var match, regexp;
459
+ flag = '(?:^|\\s)' + flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
460
+ regexp = new RegExp(flag + '([A-Za-z0-9_\+\-]*)$|' + flag + '([^\\x00-\\xff]*)$', 'gi');
461
+ match = regexp.exec(subtext);
462
+ if (match) {
463
+ return match[2] || match[1];
464
+ } else {
465
+ return null;
466
+ }
467
+ },
468
+ filter: function(query, data, search_key) {
469
+ var item, _i, _len, _results;
470
+ _results = [];
471
+ for (_i = 0, _len = data.length; _i < _len; _i++) {
472
+ item = data[_i];
473
+ if (~item[search_key].toLowerCase().indexOf(query)) {
474
+ _results.push(item);
475
+ }
476
+ }
477
+ return _results;
478
+ },
479
+ remote_filter: null,
480
+ sorter: function(query, items, search_key) {
481
+ var item, _i, _len, _results;
482
+ if (!query) {
483
+ return items;
484
+ }
485
+ _results = [];
486
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
487
+ item = items[_i];
488
+ item.atwho_order = item[search_key].toLowerCase().indexOf(query);
489
+ if (item.atwho_order > -1) {
490
+ _results.push(item);
491
+ }
492
+ }
493
+ return _results.sort(function(a, b) {
494
+ return a.atwho_order - b.atwho_order;
495
+ });
496
+ },
497
+ tpl_eval: function(tpl, map) {
498
+ try {
499
+ return tpl.replace(/\$\{([^\}]*)\}/g, function(tag, key, pos) {
500
+ return map[key];
501
+ });
502
+ } catch (error) {
503
+ return "";
504
+ }
505
+ },
506
+ highlighter: function(li, query) {
507
+ var regexp;
508
+ if (!query) {
509
+ return li;
510
+ }
511
+ regexp = new RegExp(">\\s*(\\w*)(" + query.replace("+", "\\+") + ")(\\w*)\\s*<", 'ig');
512
+ return li.replace(regexp, function(str, $1, $2, $3) {
513
+ return '> ' + $1 + '<strong>' + $2 + '</strong>' + $3 + ' <';
514
+ });
515
+ },
516
+ before_insert: function(value, $li) {
517
+ return value;
518
+ }
519
+ };
496
520
  DEFAULT_TPL = "<li data-value='${name}'>${name}</li>";
497
521
  Api = {
498
522
  init: function(options) {
499
523
  var $this, app;
500
524
  app = ($this = $(this)).data("atwho");
501
525
  if (!app) {
502
- $this.data('atwho', (app = new Controller(this)));
526
+ $this.data('atwho', (app = new App(this)));
503
527
  }
504
528
  return app.reg(options.at, options);
505
529
  },
506
- load: function(flag, data) {
507
- this.set_context_for(flag);
508
- return this.model.load(data);
530
+ load: function(key, data) {
531
+ var c;
532
+ if (c = this.controller(key)) {
533
+ return c.model.load(data);
534
+ }
509
535
  },
510
536
  run: function() {
511
- return this.look_up();
537
+ return this.dispatch();
512
538
  }
513
539
  };
514
540
  $CONTAINER = $("<div id='atwho-container'></div>");
@@ -1,7 +1,7 @@
1
1
  module Jquery
2
2
  module Atwho
3
3
  module Rails
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-atwho-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-03 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec