|
9 | 9 |
|
10 | 10 |
|
11 | 11 | (function() {
|
| 12 | + var __slice = [].slice; |
12 | 13 |
|
13 | 14 | (function(factory) {
|
14 | 15 | if (typeof define === 'function' && define.amd) {
|
|
17 | 18 | return factory(window.jQuery);
|
18 | 19 | }
|
19 | 20 | })(function($) {
|
20 |
| - var Controller, DEFAULT_CALLBACKS, DEFAULT_TPL, KEY_CODE, View; |
| 21 | + var Controller, DEFAULT_CALLBACKS, DEFAULT_TPL, KEY_CODE, Model, View; |
21 | 22 | KEY_CODE = {
|
22 | 23 | DOWN: 40,
|
23 | 24 | UP: 38,
|
|
41 | 42 | },
|
42 | 43 | matcher: function(flag, subtext) {
|
43 | 44 | var match, matched, regexp;
|
| 45 | + flag = flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); |
44 | 46 | regexp = new RegExp(flag + '([A-Za-z0-9_\+\-]*)$|' + flag + '([^\\x00-\\xff]*)$', 'gi');
|
45 | 47 | match = regexp.exec(subtext);
|
46 | 48 | matched = null;
|
|
116 | 118 | }
|
117 | 119 | }
|
118 | 120 | };
|
| 121 | + Model = (function() { |
| 122 | + |
| 123 | + function Model(context) { |
| 124 | + this.context = context; |
| 125 | + this._data_sets = {}; |
| 126 | + } |
| 127 | + |
| 128 | + Model.prototype.query = function(query, callback) { |
| 129 | + var data, remote_filter, search_key; |
| 130 | + data = this.all() || []; |
| 131 | + search_key = this.context.get_opt("search_key"); |
| 132 | + data = this.context.callbacks('filter').call(this.context, query, data, search_key); |
| 133 | + if (data && data.length > 0) { |
| 134 | + callback(data); |
| 135 | + } else if ((remote_filter = this.context.callbacks('remote_filter'))) { |
| 136 | + remote_filter.call(this.context, query.text, callback); |
| 137 | + } else { |
| 138 | + return false; |
| 139 | + } |
| 140 | + return true; |
| 141 | + }; |
| 142 | + |
| 143 | + Model.prototype.all = function(key) { |
| 144 | + return this._data_sets[key || (key = this.context.current_flag)]; |
| 145 | + }; |
| 146 | + |
| 147 | + Model.prototype.reset = function(data, key) { |
| 148 | + return this._data_sets[key || (key = this.context.current_flag)] = this.context.callbacks("data_refactor").call(this.context, data); |
| 149 | + }; |
| 150 | + |
| 151 | + Model.prototype.load = function(data) { |
| 152 | + if (typeof data === "string") { |
| 153 | + return this._load_remote_data(data); |
| 154 | + } else { |
| 155 | + return this.reset(data); |
| 156 | + } |
| 157 | + }; |
| 158 | + |
| 159 | + Model.prototype._load_remote_data = function(url) { |
| 160 | + var _this = this; |
| 161 | + return $.ajax(url, { |
| 162 | + dataType: "json" |
| 163 | + }).done(function(data) { |
| 164 | + return _this.reset(data); |
| 165 | + }); |
| 166 | + }; |
| 167 | + |
| 168 | + return Model; |
| 169 | + |
| 170 | + })(); |
119 | 171 | Controller = (function() {
|
120 | 172 |
|
121 | 173 | function Controller(inputor) {
|
|
124 | 176 | this.flags = null;
|
125 | 177 | this.current_flag = null;
|
126 | 178 | this.query = null;
|
127 |
| - this._data_sets = {}; |
128 | 179 | this.$inputor = $(inputor);
|
129 | 180 | this.view = new View(this, this.$el);
|
| 181 | + this.model = new Model(this); |
130 | 182 | this.listen();
|
131 | 183 | }
|
132 | 184 |
|
|
144 | 196 | };
|
145 | 197 |
|
146 | 198 | Controller.prototype.reg = function(flag, settings) {
|
147 |
| - var current_settings, data; |
| 199 | + var current_settings; |
148 | 200 | this.current_flag = flag;
|
149 | 201 | current_settings = this.settings[flag] ? this.settings[flag] = $.extend({}, this.settings[flag], settings) : this.settings[flag] = $.extend({}, $.fn.atwho["default"], settings);
|
150 |
| - data = current_settings.data; |
151 |
| - if (typeof data === "string") { |
152 |
| - this.load_remote_data(data); |
153 |
| - } else { |
154 |
| - this.save_data(data); |
155 |
| - } |
| 202 | + this.model.load(current_settings.data); |
156 | 203 | return this;
|
157 | 204 | };
|
158 | 205 |
|
|
162 | 209 | return this.$inputor.trigger("" + name + ".atwho", data);
|
163 | 210 | };
|
164 | 211 |
|
165 |
| - Controller.prototype.get_data = function(key) { |
166 |
| - return this._data_sets[key || (key = this.current_flag)]; |
167 |
| - }; |
168 |
| - |
169 |
| - Controller.prototype.save_data = function(data, key) { |
170 |
| - return this._data_sets[key || (key = this.current_flag)] = this.callbacks("data_refactor").call(this, data); |
| 212 | + Controller.prototype.super_call = function() { |
| 213 | + var args, func_name; |
| 214 | + func_name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; |
| 215 | + try { |
| 216 | + return DEFAULT_CALLBACKS[func_name].apply(this, args); |
| 217 | + } catch (error) { |
| 218 | + return $.error("" + error + " Or maybe At.js doesn't have function " + func_name); |
| 219 | + } |
171 | 220 | };
|
172 | 221 |
|
173 | 222 | Controller.prototype.callbacks = function(func_name) {
|
|
191 | 240 | var c, scale, scale_bottom;
|
192 | 241 | c = this.$inputor.caret('offset', this.pos - 1);
|
193 | 242 | if (document.selection) {
|
194 |
| - scale_bottom = scale = -2; |
| 243 | + scale_bottom = scale = 0; |
195 | 244 | } else {
|
196 | 245 | scale = 0;
|
197 | 246 | scale_bottom = 2;
|
|
259 | 308 | default:
|
260 | 309 | this.look_up();
|
261 | 310 | }
|
262 |
| - return e.stopPropagation(); |
263 | 311 | };
|
264 | 312 |
|
265 | 313 | Controller.prototype.on_keydown = function(e) {
|
|
290 | 338 | default:
|
291 | 339 | $.noop();
|
292 | 340 | }
|
293 |
| - return e.stopPropagation(); |
294 | 341 | };
|
295 | 342 |
|
296 | 343 | Controller.prototype.render_view = function(data) {
|
|
301 | 348 | return this.view.render(data);
|
302 | 349 | };
|
303 | 350 |
|
304 |
| - Controller.prototype.remote_call = function(query) { |
305 |
| - var _callback; |
306 |
| - _callback = function(data) { |
307 |
| - return this.render_view(data); |
308 |
| - }; |
309 |
| - _callback = $.proxy(_callback, this); |
310 |
| - return this.callbacks('remote_filter').call(this, query.text, _callback); |
311 |
| - }; |
312 |
| - |
313 |
| - Controller.prototype.load_remote_data = function(url) { |
314 |
| - var _this = this; |
315 |
| - return $.get(url).done(function(data) { |
316 |
| - return _this.save_data(data); |
317 |
| - }); |
318 |
| - }; |
319 |
| - |
320 | 351 | Controller.prototype.look_up = function() {
|
321 |
| - var data, query, search_key; |
| 352 | + var query, _callback; |
322 | 353 | query = this.catch_query();
|
323 | 354 | if (!query) {
|
324 |
| - return false; |
| 355 | + return; |
325 | 356 | }
|
326 |
| - data = this.get_data(); |
327 |
| - search_key = this.get_opt("search_key"); |
328 |
| - data = this.callbacks('filter').call(this, query.text, data || [], search_key); |
329 |
| - if (data && data.length > 0) { |
330 |
| - this.render_view(data); |
331 |
| - } else if (this.callbacks('remote_filter')) { |
332 |
| - this.remote_call(query); |
333 |
| - } else { |
334 |
| - this.view.hide(); |
| 357 | + _callback = function(data) { |
| 358 | + if (data) { |
| 359 | + return this.render_view(data); |
| 360 | + } else { |
| 361 | + return this.view.hide(); |
| 362 | + } |
| 363 | + }; |
| 364 | + _callback = $.proxy(_callback, this); |
| 365 | + if (!this.model.query(query.text, _callback)) { |
| 366 | + return this.view.hide(); |
335 | 367 | }
|
336 |
| - return $.noop(); |
337 | 368 | };
|
338 | 369 |
|
339 | 370 | return Controller;
|
340 | 371 |
|
341 | 372 | })();
|
342 | 373 | View = (function() {
|
343 | 374 |
|
344 |
| - function View(controller) { |
345 |
| - this.controller = controller; |
346 |
| - this.id = this.controller.get_opt("view_id") || "at-view"; |
| 375 | + function View(context) { |
| 376 | + this.context = context; |
| 377 | + this.id = this.context.get_opt("view_id") || "at-view"; |
347 | 378 | this.timeout_id = null;
|
348 | 379 | this.$el = $("#" + this.id);
|
349 | 380 | this.create_view();
|
|
363 | 394 | $menu.find('.cur').removeClass('cur');
|
364 | 395 | return $(e.currentTarget).addClass('cur');
|
365 | 396 | }).on('click', function(e) {
|
366 |
| - e.stopPropagation(); |
367 | 397 | e.preventDefault();
|
368 | 398 | return _this.$el.data("_view").choose();
|
369 | 399 | });
|
|
380 | 410 | View.prototype.choose = function() {
|
381 | 411 | var $li;
|
382 | 412 | $li = this.$el.find(".cur");
|
383 |
| - this.controller.callbacks("selector").call(this.controller, $li); |
384 |
| - this.controller.trigger("choose", [$li]); |
| 413 | + this.context.callbacks("selector").call(this.context, $li); |
| 414 | + this.context.trigger("choose", [$li]); |
385 | 415 | return this.hide();
|
386 | 416 | };
|
387 | 417 |
|
388 | 418 | View.prototype.reposition = function() {
|
389 | 419 | var offset, rect;
|
390 |
| - rect = this.controller.rect(); |
| 420 | + rect = this.context.rect(); |
391 | 421 | if (rect.bottom + this.$el.height() - $(window).scrollTop() > $(window).height()) {
|
392 | 422 | rect.bottom = rect.top - this.$el.height();
|
393 | 423 | }
|
|
396 | 426 | top: rect.bottom
|
397 | 427 | };
|
398 | 428 | this.$el.offset(offset);
|
399 |
| - return this.controller.trigger("reposition", [offset]); |
| 429 | + return this.context.trigger("reposition", [offset]); |
400 | 430 | };
|
401 | 431 |
|
402 | 432 | View.prototype.next = function() {
|
|
459 | 489 | this.clear();
|
460 | 490 | this.$el.data("_view", this);
|
461 | 491 | $ul = this.$el.find('ul');
|
462 |
| - tpl = this.controller.get_opt('tpl', DEFAULT_TPL); |
| 492 | + tpl = this.context.get_opt('tpl', DEFAULT_TPL); |
463 | 493 | $.each(list, function(i, item) {
|
464 | 494 | var $li, li;
|
465 |
| - li = _this.controller.callbacks("tpl_eval").call(_this.controller, tpl, item); |
466 |
| - $li = $(_this.controller.callbacks("highlighter").call(_this.controller, li, _this.controller.query.text)); |
| 495 | + li = _this.context.callbacks("tpl_eval").call(_this.context, tpl, item); |
| 496 | + $li = $(_this.context.callbacks("highlighter").call(_this.context, li, _this.context.query.text)); |
467 | 497 | $li.data("info", item);
|
468 | 498 | return $ul.append($li);
|
469 | 499 | });
|
|
487 | 517 | });
|
488 | 518 | };
|
489 | 519 | $.fn.atwho.Controller = Controller;
|
490 |
| - $.fn.atwho.View = View; |
491 | 520 | return $.fn.atwho["default"] = {
|
492 | 521 | data: null,
|
493 | 522 | search_key: "name",
|
|
655 | 684 | var $inputor, h, offset, position, range, x, y;
|
656 | 685 | $inputor = this.$inputor;
|
657 | 686 | if (document.selection) {
|
658 |
| - range = this.domInputor.createRange(); |
| 687 | + range = this.domInputor.createTextRange(); |
659 | 688 | if (pos) {
|
660 | 689 | range.move('character', pos);
|
661 | 690 | }
|
|
0 commit comments