ichord-At.js-mod

Add Github like mentions autocomplete to your application.

目前为 2016-02-11 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/16996/107001/ichord-Atjs-mod.js

  1. /**
  2. * at.js - 1.4.1-mod (by Mottie)
  3. * Copyright (c) 2016 chord.luo <chord.luo@gmail.com>;
  4. * Homepage: http://ichord.github.com/At.js
  5. * License: MIT
  6. */
  7. (function (root, factory) {
  8. if (typeof define === 'function' && define.amd) {
  9. // AMD. Register as an anonymous module unless amdModuleId is set
  10. define(["jquery"], function (a0) {
  11. return (factory(a0));
  12. });
  13. } else if (typeof exports === 'object') {
  14. // Node. Does not work with strict CommonJS, but
  15. // only CommonJS-like environments that support module.exports,
  16. // like Node.
  17. module.exports = factory(require("jquery"));
  18. } else {
  19. factory(jQuery);
  20. }
  21. }(this, function ($) {
  22. var DEFAULT_CALLBACKS, KEY_CODE;
  23.  
  24. KEY_CODE = {
  25. DOWN: 40,
  26. UP: 38,
  27. ESC: 27,
  28. TAB: 9,
  29. ENTER: 13,
  30. CTRL: 17,
  31. A: 65,
  32. P: 80,
  33. N: 78,
  34. LEFT: 37,
  35. UP: 38,
  36. RIGHT: 39,
  37. DOWN: 40,
  38. BACKSPACE: 8,
  39. SPACE: 32
  40. };
  41.  
  42. DEFAULT_CALLBACKS = {
  43. beforeSave: function(data) {
  44. return Controller.arrayToDefaultHash(data);
  45. },
  46. matcher: function(flag, subtext, should_startWithSpace, acceptSpaceBar) {
  47. var _a, _y, match, regexp, space;
  48. flag = flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  49. if (should_startWithSpace) {
  50. flag = '(?:^|\\s)' + flag;
  51. }
  52. _a = decodeURI("%C3%80");
  53. _y = decodeURI("%C3%BF");
  54. space = acceptSpaceBar ? "\ " : "";
  55. regexp = new RegExp(flag + "([A-Za-z" + _a + "-" + _y + "0-9_" + space + "\'\.\+\-]*)$|" + flag + "([^\\x00-\\xff]*)$", 'gi');
  56. match = regexp.exec(subtext);
  57. if (match) {
  58. return match[2] || match[1];
  59. } else {
  60. return null;
  61. }
  62. },
  63. filter: function(query, data, searchKey) {
  64. var _results, i, item, len;
  65. _results = [];
  66. for (i = 0, len = data.length; i < len; i++) {
  67. item = data[i];
  68. if (~new String(item[searchKey]).toLowerCase().indexOf(query.toLowerCase())) {
  69. _results.push(item);
  70. }
  71. }
  72. return _results;
  73. },
  74. remoteFilter: null,
  75. sorter: function(query, items, searchKey) {
  76. var _results, i, item, len;
  77. if (!query) {
  78. return items;
  79. }
  80. _results = [];
  81. for (i = 0, len = items.length; i < len; i++) {
  82. item = items[i];
  83. item.atwho_order = new String(item[searchKey]).toLowerCase().indexOf(query.toLowerCase());
  84. if (item.atwho_order > -1) {
  85. _results.push(item);
  86. }
  87. }
  88. return _results.sort(function(a, b) {
  89. return a.atwho_order - b.atwho_order;
  90. });
  91. },
  92. tplEval: function(tpl, map) {
  93. var error, error1, template;
  94. template = tpl;
  95. try {
  96. if (typeof tpl !== 'string') {
  97. template = tpl(map);
  98. }
  99. return template.replace(/\$\{([^\}]*)\}/g, function(tag, key, pos) {
  100. return map[key];
  101. });
  102. } catch (error1) {
  103. error = error1;
  104. return "";
  105. }
  106. },
  107. highlighter: function(li, query) {
  108. var regexp;
  109. if (!query) {
  110. return li;
  111. }
  112. regexp = new RegExp(">\\s*(\\w*?)(" + query.replace("+", "\\+") + ")(\\w*)\\s*<", 'ig');
  113. return li.replace(regexp, function(str, $1, $2, $3) {
  114. return '> ' + $1 + '<strong>' + $2 + '</strong>' + $3 + ' <';
  115. });
  116. },
  117. beforeInsert: function(value, $li) {
  118. return value;
  119. },
  120. beforeReposition: function(offset) {
  121. return offset;
  122. },
  123. afterMatchFailed: function(at, el) {}
  124. };
  125.  
  126. var App;
  127.  
  128. App = (function() {
  129. function App(inputor) {
  130. this.currentFlag = null;
  131. this.controllers = {};
  132. this.aliasMaps = {};
  133. this.$inputor = $(inputor);
  134. this.setupRootElement();
  135. this.listen();
  136. }
  137.  
  138. App.prototype.createContainer = function(doc) {
  139. var ref;
  140. if ((ref = this.$el) != null) {
  141. ref.remove();
  142. }
  143. return $(doc.body).append(this.$el = $("<div class='atwho-container'></div>"));
  144. };
  145.  
  146. App.prototype.setupRootElement = function(iframe, asRoot) {
  147. var error, error1;
  148. if (asRoot == null) {
  149. asRoot = false;
  150. }
  151. if (iframe) {
  152. this.window = iframe.contentWindow;
  153. this.document = iframe.contentDocument || this.window.document;
  154. this.iframe = iframe;
  155. } else {
  156. this.document = this.$inputor[0].ownerDocument;
  157. this.window = this.document.defaultView || this.document.parentWindow;
  158. try {
  159. this.iframe = this.window.frameElement;
  160. } catch (error1) {
  161. error = error1;
  162. this.iframe = null;
  163. if ($.fn.atwho.debug) {
  164. throw new Error("iframe auto-discovery is failed.\nPlease use `setIframe` to set the target iframe manually.\n" + error);
  165. }
  166. }
  167. }
  168. return this.createContainer((this.iframeAsRoot = asRoot) ? this.document : document);
  169. };
  170.  
  171. App.prototype.controller = function(at) {
  172. var c, current, currentFlag, ref;
  173. if (this.aliasMaps[at]) {
  174. current = this.controllers[this.aliasMaps[at]];
  175. } else {
  176. ref = this.controllers;
  177. for (currentFlag in ref) {
  178. c = ref[currentFlag];
  179. if (currentFlag === at) {
  180. current = c;
  181. break;
  182. }
  183. }
  184. }
  185. if (current) {
  186. return current;
  187. } else {
  188. return this.controllers[this.currentFlag];
  189. }
  190. };
  191.  
  192. App.prototype.setContextFor = function(at) {
  193. this.currentFlag = at;
  194. return this;
  195. };
  196.  
  197. App.prototype.reg = function(flag, setting) {
  198. var base, controller;
  199. controller = (base = this.controllers)[flag] || (base[flag] = this.$inputor.is('[contentEditable]') ? new EditableController(this, flag) : new TextareaController(this, flag));
  200. if (setting.alias) {
  201. this.aliasMaps[setting.alias] = flag;
  202. }
  203. controller.init(setting);
  204. return this;
  205. };
  206.  
  207. App.prototype.listen = function() {
  208. return this.$inputor.on('compositionstart', (function(_this) {
  209. return function(e) {
  210. var ref;
  211. if ((ref = _this.controller()) != null) {
  212. ref.view.hide();
  213. }
  214. _this.isComposing = true;
  215. return null;
  216. };
  217. })(this)).on('compositionend', (function(_this) {
  218. return function(e) {
  219. _this.isComposing = false;
  220. return null;
  221. };
  222. })(this)).on('keyup.atwhoInner', (function(_this) {
  223. return function(e) {
  224. return _this.onKeyup(e);
  225. };
  226. })(this)).on('keydown.atwhoInner', (function(_this) {
  227. return function(e) {
  228. return _this.onKeydown(e);
  229. };
  230. })(this)).on('blur.atwhoInner', (function(_this) {
  231. return function(e) {
  232. var c;
  233. if (c = _this.controller()) {
  234. c.expectedQueryCBId = null;
  235. return c.view.hide(e, c.getOpt("displayTimeout"));
  236. }
  237. };
  238. })(this)).on('click.atwhoInner', (function(_this) {
  239. return function(e) {
  240. return _this.dispatch(e);
  241. };
  242. })(this)).on('scroll.atwhoInner', (function(_this) {
  243. return function() {
  244. var lastScrollTop;
  245. lastScrollTop = _this.$inputor.scrollTop();
  246. return function(e) {
  247. var currentScrollTop, ref;
  248. currentScrollTop = e.target.scrollTop;
  249. if (lastScrollTop !== currentScrollTop) {
  250. if ((ref = _this.controller()) != null) {
  251. ref.view.hide(e);
  252. }
  253. }
  254. lastScrollTop = currentScrollTop;
  255. return true;
  256. };
  257. };
  258. })(this)());
  259. };
  260.  
  261. App.prototype.shutdown = function() {
  262. var _, c, ref;
  263. ref = this.controllers;
  264. for (_ in ref) {
  265. c = ref[_];
  266. c.destroy();
  267. delete this.controllers[_];
  268. }
  269. this.$inputor.off('.atwhoInner');
  270. return this.$el.remove();
  271. };
  272.  
  273. App.prototype.dispatch = function(e) {
  274. var _, c, ref, results;
  275. ref = this.controllers;
  276. results = [];
  277. for (_ in ref) {
  278. c = ref[_];
  279. results.push(c.lookUp(e));
  280. }
  281. return results;
  282. };
  283.  
  284. App.prototype.onKeyup = function(e) {
  285. var ref;
  286. switch (e.keyCode) {
  287. case KEY_CODE.ESC:
  288. e.preventDefault();
  289. if ((ref = this.controller()) != null) {
  290. ref.view.hide();
  291. }
  292. break;
  293. case KEY_CODE.DOWN:
  294. case KEY_CODE.UP:
  295. case KEY_CODE.CTRL:
  296. case KEY_CODE.ENTER:
  297. $.noop();
  298. break;
  299. case KEY_CODE.P:
  300. case KEY_CODE.N:
  301. if (!e.ctrlKey) {
  302. this.dispatch(e);
  303. }
  304. break;
  305. default:
  306. this.dispatch(e);
  307. }
  308. };
  309.  
  310. App.prototype.onKeydown = function(e) {
  311. var ref, view;
  312. view = (ref = this.controller()) != null ? ref.view : void 0;
  313. if (!(view && view.visible())) {
  314. return;
  315. }
  316. switch (e.keyCode) {
  317. case KEY_CODE.ESC:
  318. e.preventDefault();
  319. view.hide(e);
  320. break;
  321. case KEY_CODE.UP:
  322. e.preventDefault();
  323. view.prev();
  324. break;
  325. case KEY_CODE.DOWN:
  326. e.preventDefault();
  327. view.next();
  328. break;
  329. case KEY_CODE.P:
  330. if (!e.ctrlKey) {
  331. return;
  332. }
  333. e.preventDefault();
  334. view.prev();
  335. break;
  336. case KEY_CODE.N:
  337. if (!e.ctrlKey) {
  338. return;
  339. }
  340. e.preventDefault();
  341. view.next();
  342. break;
  343. case KEY_CODE.TAB:
  344. case KEY_CODE.ENTER:
  345. case KEY_CODE.SPACE:
  346. if (!view.visible()) {
  347. return;
  348. }
  349. if (!this.controller().getOpt('spaceSelectsMatch') && e.keyCode === KEY_CODE.SPACE) {
  350. return;
  351. }
  352. if (!this.controller().getOpt('tabSelectsMatch') && e.keyCode === KEY_CODE.TAB) {
  353. return;
  354. }
  355. if (view.highlighted()) {
  356. e.preventDefault();
  357. view.choose(e);
  358. } else {
  359. view.hide(e);
  360. }
  361. break;
  362. default:
  363. $.noop();
  364. }
  365. };
  366.  
  367. return App;
  368.  
  369. })();
  370.  
  371. var Controller,
  372. slice = [].slice;
  373.  
  374. Controller = (function() {
  375. Controller.prototype.uid = function() {
  376. return (Math.random().toString(16) + "000000000").substr(2, 8) + (new Date().getTime());
  377. };
  378.  
  379. function Controller(app, at1) {
  380. this.app = app;
  381. this.at = at1;
  382. this.$inputor = this.app.$inputor;
  383. this.id = (this.$inputor[0].id || this.uid()).replace(/\W/g,'');
  384. this.expectedQueryCBId = null;
  385. this.setting = null;
  386. this.query = null;
  387. this.pos = 0;
  388. this.range = null;
  389. if ((this.$el = $("#atwho-ground-" + this.id, this.app.$el)).length === 0) {
  390. this.app.$el.append(this.$el = $("<div id='atwho-ground-" + this.id + "'></div>"));
  391. }
  392. this.model = new Model(this);
  393. this.view = new View(this);
  394. }
  395.  
  396. Controller.prototype.init = function(setting) {
  397. this.setting = $.extend({}, this.setting || $.fn.atwho["default"], setting);
  398. this.view.init();
  399. return this.model.reload(this.setting.data);
  400. };
  401.  
  402. Controller.prototype.destroy = function() {
  403. this.trigger('beforeDestroy');
  404. this.model.destroy();
  405. this.view.destroy();
  406. return this.$el.remove();
  407. };
  408.  
  409. Controller.prototype.callDefault = function() {
  410. var args, error, error1, funcName;
  411. funcName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  412. try {
  413. return DEFAULT_CALLBACKS[funcName].apply(this, args);
  414. } catch (error1) {
  415. error = error1;
  416. return $.error(error + " Or maybe At.js doesn't have function " + funcName);
  417. }
  418. };
  419.  
  420. Controller.prototype.trigger = function(name, data) {
  421. var alias, eventName;
  422. if (data == null) {
  423. data = [];
  424. }
  425. data.push(this);
  426. alias = this.getOpt('alias');
  427. eventName = alias ? name + "-" + alias + ".atwho" : name + ".atwho";
  428. return this.$inputor.trigger(eventName, data);
  429. };
  430.  
  431. Controller.prototype.callbacks = function(funcName) {
  432. return this.getOpt("callbacks")[funcName] || DEFAULT_CALLBACKS[funcName];
  433. };
  434.  
  435. Controller.prototype.getOpt = function(at, default_value) {
  436. var e, error1;
  437. try {
  438. return this.setting[at];
  439. } catch (error1) {
  440. e = error1;
  441. return null;
  442. }
  443. };
  444.  
  445. Controller.prototype.insertContentFor = function($li) {
  446. var data, tpl;
  447. tpl = this.getOpt('insertTpl');
  448. data = $.extend({}, $li.data('item-data'), {
  449. 'atwho-at': this.at
  450. });
  451. return this.callbacks("tplEval").call(this, tpl, data, "onInsert");
  452. };
  453.  
  454. Controller.prototype.renderView = function(data) {
  455. var searchKey;
  456. searchKey = this.getOpt("searchKey");
  457. data = this.callbacks("sorter").call(this, this.query.text, data.slice(0, 1001), searchKey);
  458. return this.view.render(data.slice(0, this.getOpt('limit')));
  459. };
  460.  
  461. Controller.arrayToDefaultHash = function(data) {
  462. var i, item, len, results;
  463. if (!$.isArray(data)) {
  464. return data;
  465. }
  466. results = [];
  467. for (i = 0, len = data.length; i < len; i++) {
  468. item = data[i];
  469. if ($.isPlainObject(item)) {
  470. results.push(item);
  471. } else {
  472. results.push({
  473. name: item
  474. });
  475. }
  476. }
  477. return results;
  478. };
  479.  
  480. Controller.prototype.lookUp = function(e) {
  481. var query, wait;
  482. if (e && e.type === 'click' && !this.getOpt('lookUpOnClick')) {
  483. return;
  484. }
  485. if (this.getOpt('suspendOnComposing') && this.app.isComposing) {
  486. return;
  487. }
  488. query = this.catchQuery(e);
  489. if (!query) {
  490. this.expectedQueryCBId = null;
  491. return query;
  492. }
  493. this.app.setContextFor(this.at);
  494. if (wait = this.getOpt('delay')) {
  495. this._delayLookUp(query, wait);
  496. } else {
  497. this._lookUp(query);
  498. }
  499. return query;
  500. };
  501.  
  502. Controller.prototype._delayLookUp = function(query, wait) {
  503. var now, remaining;
  504. now = Date.now ? Date.now() : new Date().getTime();
  505. this.previousCallTime || (this.previousCallTime = now);
  506. remaining = wait - (now - this.previousCallTime);
  507. if ((0 < remaining && remaining < wait)) {
  508. this.previousCallTime = now;
  509. this._stopDelayedCall();
  510. return this.delayedCallTimeout = setTimeout((function(_this) {
  511. return function() {
  512. _this.previousCallTime = 0;
  513. _this.delayedCallTimeout = null;
  514. return _this._lookUp(query);
  515. };
  516. })(this), wait);
  517. } else {
  518. this._stopDelayedCall();
  519. if (this.previousCallTime !== now) {
  520. this.previousCallTime = 0;
  521. }
  522. return this._lookUp(query);
  523. }
  524. };
  525.  
  526. Controller.prototype._stopDelayedCall = function() {
  527. if (this.delayedCallTimeout) {
  528. clearTimeout(this.delayedCallTimeout);
  529. return this.delayedCallTimeout = null;
  530. }
  531. };
  532.  
  533. Controller.prototype._generateQueryCBId = function() {
  534. return {};
  535. };
  536.  
  537. Controller.prototype._lookUp = function(query) {
  538. var _callback;
  539. _callback = function(queryCBId, data) {
  540. if (queryCBId !== this.expectedQueryCBId) {
  541. return;
  542. }
  543. if (data && data.length > 0) {
  544. return this.renderView(this.constructor.arrayToDefaultHash(data));
  545. } else {
  546. return this.view.hide();
  547. }
  548. };
  549. this.expectedQueryCBId = this._generateQueryCBId();
  550. return this.model.query(query.text, $.proxy(_callback, this, this.expectedQueryCBId));
  551. };
  552.  
  553. return Controller;
  554.  
  555. })();
  556.  
  557. var TextareaController,
  558. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  559. hasProp = {}.hasOwnProperty;
  560.  
  561. TextareaController = (function(superClass) {
  562. extend(TextareaController, superClass);
  563.  
  564. function TextareaController() {
  565. return TextareaController.__super__.constructor.apply(this, arguments);
  566. }
  567.  
  568. TextareaController.prototype.catchQuery = function() {
  569. var caretPos, content, end, isString, query, start, subtext;
  570. content = this.$inputor.val();
  571. caretPos = this.$inputor.caret('pos', {
  572. iframe: this.app.iframe
  573. });
  574. subtext = content.slice(0, caretPos);
  575. query = this.callbacks("matcher").call(this, this.at, subtext, this.getOpt('startWithSpace'));
  576. isString = typeof query === 'string';
  577. if (isString && query.length < this.getOpt('minLen', 0)) {
  578. return;
  579. }
  580. if (isString && query.length <= this.getOpt('maxLen', 20)) {
  581. start = caretPos - query.length;
  582. end = start + query.length;
  583. this.pos = start;
  584. query = {
  585. 'text': query,
  586. 'headPos': start,
  587. 'endPos': end
  588. };
  589. this.trigger("matched", [this.at, query.text]);
  590. } else {
  591. query = null;
  592. this.view.hide();
  593. }
  594. return this.query = query;
  595. };
  596.  
  597. TextareaController.prototype.rect = function() {
  598. var c, iframeOffset, scaleBottom;
  599. if (!(c = this.$inputor.caret('offset', this.pos - 1, {
  600. iframe: this.app.iframe
  601. }))) {
  602. return;
  603. }
  604. if (this.app.iframe && !this.app.iframeAsRoot) {
  605. iframeOffset = $(this.app.iframe).offset();
  606. c.left += iframeOffset.left;
  607. c.top += iframeOffset.top;
  608. }
  609. scaleBottom = this.app.document.selection ? 0 : 2;
  610. return {
  611. left: c.left,
  612. top: c.top,
  613. bottom: c.top + c.height + scaleBottom
  614. };
  615. };
  616.  
  617. TextareaController.prototype.insert = function(content, $li) {
  618. var $inputor, source, startStr, suffix, text;
  619. $inputor = this.$inputor;
  620. source = $inputor.val();
  621. startStr = source.slice(0, Math.max(this.query.headPos - this.at.length, 0));
  622. suffix = (suffix = this.getOpt('suffix')) === "" ? suffix : suffix || " ";
  623. content += suffix;
  624. text = "" + startStr + content + (source.slice(this.query['endPos'] || 0));
  625. $inputor.val(text);
  626. $inputor.caret('pos', startStr.length + content.length, {
  627. iframe: this.app.iframe
  628. });
  629. if (!$inputor.is(':focus')) {
  630. $inputor.focus();
  631. }
  632. return $inputor.change();
  633. };
  634.  
  635. return TextareaController;
  636.  
  637. })(Controller);
  638.  
  639. var EditableController,
  640. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  641. hasProp = {}.hasOwnProperty;
  642.  
  643. EditableController = (function(superClass) {
  644. extend(EditableController, superClass);
  645.  
  646. function EditableController() {
  647. return EditableController.__super__.constructor.apply(this, arguments);
  648. }
  649.  
  650. EditableController.prototype._getRange = function() {
  651. var sel;
  652. sel = this.app.window.getSelection();
  653. if (sel.rangeCount > 0) {
  654. return sel.getRangeAt(0);
  655. }
  656. };
  657.  
  658. EditableController.prototype._setRange = function(position, node, range) {
  659. if (range == null) {
  660. range = this._getRange();
  661. }
  662. if (!range) {
  663. return;
  664. }
  665. node = $(node)[0];
  666. if (position === 'after') {
  667. range.setEndAfter(node);
  668. range.setStartAfter(node);
  669. } else {
  670. range.setEndBefore(node);
  671. range.setStartBefore(node);
  672. }
  673. range.collapse(false);
  674. return this._clearRange(range);
  675. };
  676.  
  677. EditableController.prototype._clearRange = function(range) {
  678. var sel;
  679. if (range == null) {
  680. range = this._getRange();
  681. }
  682. sel = this.app.window.getSelection();
  683. if (this.ctrl_a_pressed == null) {
  684. sel.removeAllRanges();
  685. return sel.addRange(range);
  686. }
  687. };
  688.  
  689. EditableController.prototype._movingEvent = function(e) {
  690. var ref;
  691. return e.type === 'click' || ((ref = e.which) === KEY_CODE.RIGHT || ref === KEY_CODE.LEFT || ref === KEY_CODE.UP || ref === KEY_CODE.DOWN);
  692. };
  693.  
  694. EditableController.prototype._unwrap = function(node) {
  695. var next;
  696. node = $(node).unwrap().get(0);
  697. if ((next = node.nextSibling) && next.nodeValue) {
  698. node.nodeValue += next.nodeValue;
  699. $(next).remove();
  700. }
  701. return node;
  702. };
  703.  
  704. EditableController.prototype.catchQuery = function(e) {
  705. var $inserted, $query, _range, index, inserted, isString, lastNode, matched, offset, query, query_content, range;
  706. if (!(range = this._getRange())) {
  707. return;
  708. }
  709. if (!range.collapsed) {
  710. return;
  711. }
  712. if (e.which === KEY_CODE.ENTER) {
  713. ($query = $(range.startContainer).closest('.atwho-query')).contents().unwrap();
  714. if ($query.is(':empty')) {
  715. $query.remove();
  716. }
  717. ($query = $(".atwho-query", this.app.document)).text($query.text()).contents().last().unwrap();
  718. this._clearRange();
  719. return;
  720. }
  721. if (/firefox/i.test(navigator.userAgent)) {
  722. if ($(range.startContainer).is(this.$inputor)) {
  723. this._clearRange();
  724. return;
  725. }
  726. if (e.which === KEY_CODE.BACKSPACE && range.startContainer.nodeType === document.ELEMENT_NODE && (offset = range.startOffset - 1) >= 0) {
  727. _range = range.cloneRange();
  728. _range.setStart(range.startContainer, offset);
  729. if ($(_range.cloneContents()).contents().last().is('.atwho-inserted')) {
  730. inserted = $(range.startContainer).contents().get(offset);
  731. this._setRange('after', $(inserted).contents().last());
  732. }
  733. } else if (e.which === KEY_CODE.LEFT && range.startContainer.nodeType === document.TEXT_NODE) {
  734. $inserted = $(range.startContainer.previousSibling);
  735. if ($inserted.is('.atwho-inserted') && range.startOffset === 0) {
  736. this._setRange('after', $inserted.contents().last());
  737. }
  738. }
  739. }
  740. $(range.startContainer).closest('.atwho-inserted').addClass('atwho-query').siblings().removeClass('atwho-query');
  741. if (($query = $(".atwho-query", this.app.document)).length > 0 && $query.is(':empty') && $query.text().length === 0) {
  742. $query.remove();
  743. }
  744. if (!this._movingEvent(e)) {
  745. $query.removeClass('atwho-inserted');
  746. }
  747. if ($query.length > 0) {
  748. switch (e.which) {
  749. case KEY_CODE.LEFT:
  750. this._setRange('before', $query.get(0), range);
  751. $query.removeClass('atwho-query');
  752. return;
  753. case KEY_CODE.RIGHT:
  754. this._setRange('after', $query.get(0).nextSibling, range);
  755. $query.removeClass('atwho-query');
  756. return;
  757. }
  758. }
  759. if ($query.length > 0 && (query_content = $query.attr('data-atwho-at-query'))) {
  760. $query.empty().html(query_content).attr('data-atwho-at-query', null);
  761. this._setRange('after', $query.get(0), range);
  762. }
  763. _range = range.cloneRange();
  764. _range.setStart(range.startContainer, 0);
  765. matched = this.callbacks("matcher").call(this, this.at, _range.toString(), this.getOpt('startWithSpace'));
  766. isString = typeof matched === 'string';
  767. if ($query.length === 0 && isString && (index = range.startOffset - this.at.length - matched.length) >= 0) {
  768. range.setStart(range.startContainer, index);
  769. $query = $('<span/>', this.app.document).attr(this.getOpt("editableAtwhoQueryAttrs")).addClass('atwho-query');
  770. range.surroundContents($query.get(0));
  771. lastNode = $query.contents().last().get(0);
  772. if (/firefox/i.test(navigator.userAgent)) {
  773. range.setStart(lastNode, lastNode.length);
  774. range.setEnd(lastNode, lastNode.length);
  775. this._clearRange(range);
  776. } else {
  777. this._setRange('after', lastNode, range);
  778. }
  779. }
  780. if (isString && matched.length < this.getOpt('minLen', 0)) {
  781. return;
  782. }
  783. if (isString && matched.length <= this.getOpt('maxLen', 20)) {
  784. query = {
  785. text: matched,
  786. el: $query
  787. };
  788. this.trigger("matched", [this.at, query.text]);
  789. return this.query = query;
  790. } else {
  791. this.view.hide();
  792. this.query = {
  793. el: $query
  794. };
  795. if ($query.text().indexOf(this.at) >= 0) {
  796. if (this._movingEvent(e) && $query.hasClass('atwho-inserted')) {
  797. $query.removeClass('atwho-query');
  798. } else if (false !== this.callbacks('afterMatchFailed').call(this, this.at, $query)) {
  799. this._setRange("after", this._unwrap($query.text($query.text()).contents().first()));
  800. }
  801. }
  802. return null;
  803. }
  804. };
  805.  
  806. EditableController.prototype.rect = function() {
  807. var $iframe, iframeOffset, rect;
  808. rect = this.query.el.offset();
  809. if (this.app.iframe && !this.app.iframeAsRoot) {
  810. iframeOffset = ($iframe = $(this.app.iframe)).offset();
  811. rect.left += iframeOffset.left - this.$inputor.scrollLeft();
  812. rect.top += iframeOffset.top - this.$inputor.scrollTop();
  813. }
  814. rect.bottom = rect.top + this.query.el.height();
  815. return rect;
  816. };
  817.  
  818. EditableController.prototype.insert = function(content, $li) {
  819. var data, range, suffix, suffixNode;
  820. suffix = (suffix = this.getOpt('suffix')) === "" ? suffix : suffix || "\u00A0";
  821. data = $li.data('item-data');
  822. this.query.el.removeClass('atwho-query').addClass('atwho-inserted').html(content).attr('data-atwho-at-query', "" + data['atwho-at'] + this.query.text);
  823. if (range = this._getRange()) {
  824. range.setEndAfter(this.query.el[0]);
  825. range.collapse(false);
  826. range.insertNode(suffixNode = this.app.document.createTextNode("\u200D" + suffix));
  827. this._setRange('after', suffixNode, range);
  828. }
  829. if (!this.$inputor.is(':focus')) {
  830. this.$inputor.focus();
  831. }
  832. return this.$inputor.change();
  833. };
  834.  
  835. return EditableController;
  836.  
  837. })(Controller);
  838.  
  839. var Model;
  840.  
  841. Model = (function() {
  842. function Model(context) {
  843. this.context = context;
  844. this.at = this.context.at;
  845. this.storage = this.context.$inputor;
  846. }
  847.  
  848. Model.prototype.destroy = function() {
  849. return this.storage.data(this.at, null);
  850. };
  851.  
  852. Model.prototype.saved = function() {
  853. return this.fetch() > 0;
  854. };
  855.  
  856. Model.prototype.query = function(query, callback) {
  857. var _remoteFilter, data, searchKey;
  858. data = this.fetch();
  859. searchKey = this.context.getOpt("searchKey");
  860. data = this.context.callbacks('filter').call(this.context, query, data, searchKey) || [];
  861. _remoteFilter = this.context.callbacks('remoteFilter');
  862. if (data.length > 0 || (!_remoteFilter && data.length === 0)) {
  863. return callback(data);
  864. } else {
  865. return _remoteFilter.call(this.context, query, callback);
  866. }
  867. };
  868.  
  869. Model.prototype.fetch = function() {
  870. return this.storage.data(this.at) || [];
  871. };
  872.  
  873. Model.prototype.save = function(data) {
  874. return this.storage.data(this.at, this.context.callbacks("beforeSave").call(this.context, data || []));
  875. };
  876.  
  877. Model.prototype.load = function(data) {
  878. if (!(this.saved() || !data)) {
  879. return this._load(data);
  880. }
  881. };
  882.  
  883. Model.prototype.reload = function(data) {
  884. return this._load(data);
  885. };
  886.  
  887. Model.prototype._load = function(data) {
  888. if (typeof data === "string") {
  889. return $.ajax(data, {
  890. dataType: "json"
  891. }).done((function(_this) {
  892. return function(data) {
  893. return _this.save(data);
  894. };
  895. })(this));
  896. } else {
  897. return this.save(data);
  898. }
  899. };
  900.  
  901. return Model;
  902.  
  903. })();
  904.  
  905. var View;
  906.  
  907. View = (function() {
  908. function View(context) {
  909. this.context = context;
  910. this.$el = $("<div class='atwho-view'><ul class='atwho-view-ul'></ul></div>");
  911. this.$elUl = this.$el.children();
  912. this.timeoutID = null;
  913. this.context.$el.append(this.$el);
  914. this.bindEvent();
  915. }
  916.  
  917. View.prototype.init = function() {
  918. var header_tpl, id;
  919. id = this.context.getOpt("alias") || this.context.at.charCodeAt(0);
  920. header_tpl = this.context.getOpt("headerTpl");
  921. if (header_tpl && this.$el.children().length === 1) {
  922. this.$el.prepend(header_tpl);
  923. }
  924. return this.$el.attr({
  925. 'id': "at-view-" + id
  926. });
  927. };
  928.  
  929. View.prototype.destroy = function() {
  930. return this.$el.remove();
  931. };
  932.  
  933. View.prototype.bindEvent = function() {
  934. var $menu, lastCoordX, lastCoordY;
  935. $menu = this.$el.find('ul');
  936. lastCoordX = 0;
  937. lastCoordY = 0;
  938. return $menu.on('mousemove.atwho-view', 'li', (function(_this) {
  939. return function(e) {
  940. var $cur;
  941. if (lastCoordX === e.clientX && lastCoordY === e.clientY) {
  942. return;
  943. }
  944. lastCoordX = e.clientX;
  945. lastCoordY = e.clientY;
  946. $cur = $(e.currentTarget);
  947. if ($cur.hasClass('navigation-focus')) {
  948. return;
  949. }
  950. $menu.find('.navigation-focus').removeClass('navigation-focus');
  951. return $cur.addClass('navigation-focus');
  952. };
  953. })(this)).on('click.atwho-view', 'li', (function(_this) {
  954. return function(e) {
  955. $menu.find('.navigation-focus').removeClass('navigation-focus');
  956. $(e.currentTarget).addClass('navigation-focus');
  957. _this.choose(e);
  958. return e.preventDefault();
  959. };
  960. })(this));
  961. };
  962.  
  963. View.prototype.visible = function() {
  964. return this.$el.is(":visible");
  965. };
  966.  
  967. View.prototype.highlighted = function() {
  968. return this.$el.find(".navigation-focus").length > 0;
  969. };
  970.  
  971. View.prototype.choose = function(e) {
  972. var $li, content;
  973. if (($li = this.$el.find(".navigation-focus")).length) {
  974. content = this.context.insertContentFor($li);
  975. this.context._stopDelayedCall();
  976. this.context.insert(this.context.callbacks("beforeInsert").call(this.context, content, $li), $li);
  977. this.context.trigger("inserted", [$li, e]);
  978. this.hide(e);
  979. }
  980. if (this.context.getOpt("hideWithoutSuffix")) {
  981. return this.stopShowing = true;
  982. }
  983. };
  984.  
  985. View.prototype.reposition = function(rect) {
  986. var _window, offset, overflowOffset, ref;
  987. _window = this.context.app.iframeAsRoot ? this.context.app.window : window;
  988. if (rect.bottom + this.$el.height() - $(_window).scrollTop() > $(_window).height()) {
  989. rect.bottom = rect.top - this.$el.height();
  990. }
  991. if (rect.left > (overflowOffset = $(_window).width() - this.$el.width() - 5)) {
  992. rect.left = overflowOffset;
  993. }
  994. offset = {
  995. left: rect.left,
  996. top: rect.bottom
  997. };
  998. if ((ref = this.context.callbacks("beforeReposition")) != null) {
  999. ref.call(this.context, offset);
  1000. }
  1001. this.$el.offset(offset);
  1002. return this.context.trigger("reposition", [offset]);
  1003. };
  1004.  
  1005. View.prototype.next = function() {
  1006. var cur, next, nextEl, offset;
  1007. cur = this.$el.find('.navigation-focus').removeClass('navigation-focus');
  1008. next = cur.next();
  1009. if (!next.length) {
  1010. next = this.$el.find('li:first');
  1011. }
  1012. next.addClass('navigation-focus');
  1013. nextEl = next[0];
  1014. offset = nextEl.offsetTop + nextEl.offsetHeight + (nextEl.nextSibling ? nextEl.nextSibling.offsetHeight : 0);
  1015. return this.scrollTop(Math.max(0, offset - this.$el.height()));
  1016. };
  1017.  
  1018. View.prototype.prev = function() {
  1019. var cur, offset, prev, prevEl;
  1020. cur = this.$el.find('.navigation-focus').removeClass('navigation-focus');
  1021. prev = cur.prev();
  1022. if (!prev.length) {
  1023. prev = this.$el.find('li:last');
  1024. }
  1025. prev.addClass('navigation-focus');
  1026. prevEl = prev[0];
  1027. offset = prevEl.offsetTop + prevEl.offsetHeight + (prevEl.nextSibling ? prevEl.nextSibling.offsetHeight : 0);
  1028. return this.scrollTop(Math.max(0, offset - this.$el.height()));
  1029. };
  1030.  
  1031. View.prototype.scrollTop = function(scrollTop) {
  1032. var scrollDuration;
  1033. scrollDuration = this.context.getOpt('scrollDuration');
  1034. if (scrollDuration) {
  1035. return this.$elUl.animate({
  1036. scrollTop: scrollTop
  1037. }, scrollDuration);
  1038. } else {
  1039. return this.$elUl.scrollTop(scrollTop);
  1040. }
  1041. };
  1042.  
  1043. View.prototype.show = function() {
  1044. var rect;
  1045. if (this.stopShowing) {
  1046. this.stopShowing = false;
  1047. return;
  1048. }
  1049. if (!this.visible()) {
  1050. this.$el.show();
  1051. this.$el.scrollTop(0);
  1052. this.context.trigger('shown');
  1053. }
  1054. if (rect = this.context.rect()) {
  1055. return this.reposition(rect);
  1056. }
  1057. };
  1058.  
  1059. View.prototype.hide = function(e, time) {
  1060. var callback;
  1061. if (!this.visible()) {
  1062. return;
  1063. }
  1064. if (isNaN(time)) {
  1065. this.$el.hide();
  1066. return this.context.trigger('hidden', [e]);
  1067. } else {
  1068. callback = (function(_this) {
  1069. return function() {
  1070. return _this.hide();
  1071. };
  1072. })(this);
  1073. clearTimeout(this.timeoutID);
  1074. return this.timeoutID = setTimeout(callback, time);
  1075. }
  1076. };
  1077.  
  1078. View.prototype.render = function(list) {
  1079. var $li, $ul, i, item, len, li, tpl;
  1080. if (!($.isArray(list) && list.length > 0)) {
  1081. this.hide();
  1082. return;
  1083. }
  1084. this.$el.find('ul').empty();
  1085. $ul = this.$el.find('ul');
  1086. tpl = this.context.getOpt('displayTpl');
  1087. for (i = 0, len = list.length; i < len; i++) {
  1088. item = list[i];
  1089. item = $.extend({}, item, {
  1090. 'atwho-at': this.context.at
  1091. });
  1092. li = this.context.callbacks("tplEval").call(this.context, tpl, item, "onDisplay");
  1093. $li = $(this.context.callbacks("highlighter").call(this.context, li, this.context.query.text));
  1094. $li.data("item-data", item);
  1095. $ul.append($li);
  1096. }
  1097. this.show();
  1098. if (this.context.getOpt('highlightFirst')) {
  1099. return $ul.find("li:first").addClass("navigation-focus");
  1100. }
  1101. };
  1102.  
  1103. return View;
  1104.  
  1105. })();
  1106.  
  1107. var Api;
  1108.  
  1109. Api = {
  1110. load: function(at, data) {
  1111. var c;
  1112. if (c = this.controller(at)) {
  1113. return c.model.load(data);
  1114. }
  1115. },
  1116. isSelecting: function() {
  1117. var ref;
  1118. return !!((ref = this.controller()) != null ? ref.view.visible() : void 0);
  1119. },
  1120. hide: function() {
  1121. var ref;
  1122. return (ref = this.controller()) != null ? ref.view.hide() : void 0;
  1123. },
  1124. reposition: function() {
  1125. var c;
  1126. if (c = this.controller()) {
  1127. return c.view.reposition(c.rect());
  1128. }
  1129. },
  1130. setIframe: function(iframe, asRoot) {
  1131. this.setupRootElement(iframe, asRoot);
  1132. return null;
  1133. },
  1134. run: function() {
  1135. return this.dispatch();
  1136. },
  1137. destroy: function() {
  1138. this.shutdown();
  1139. return this.$inputor.data('atwho', null);
  1140. }
  1141. };
  1142.  
  1143. $.fn.atwho = function(method) {
  1144. var _args, result;
  1145. _args = arguments;
  1146. result = null;
  1147. this.filter('textarea, input, [contenteditable=""], [contenteditable=true]').each(function() {
  1148. var $this, app;
  1149. if (!(app = ($this = $(this)).data("atwho"))) {
  1150. $this.data('atwho', (app = new App(this)));
  1151. }
  1152. if (typeof method === 'object' || !method) {
  1153. return app.reg(method.at, method);
  1154. } else if (Api[method] && app) {
  1155. return result = Api[method].apply(app, Array.prototype.slice.call(_args, 1));
  1156. } else {
  1157. return $.error("Method " + method + " does not exist on jQuery.atwho");
  1158. }
  1159. });
  1160. if (result != null) {
  1161. return result;
  1162. } else {
  1163. return this;
  1164. }
  1165. };
  1166.  
  1167. $.fn.atwho["default"] = {
  1168. at: void 0,
  1169. alias: void 0,
  1170. data: null,
  1171. displayTpl: "<li>${name}</li>",
  1172. insertTpl: "${atwho-at}${name}",
  1173. callbacks: DEFAULT_CALLBACKS,
  1174. searchKey: "name",
  1175. suffix: void 0,
  1176. hideWithoutSuffix: false,
  1177. startWithSpace: true,
  1178. highlightFirst: true,
  1179. limit: 5,
  1180. maxLen: 20,
  1181. minLen: 0,
  1182. displayTimeout: 300,
  1183. delay: null,
  1184. spaceSelectsMatch: false,
  1185. tabSelectsMatch: true,
  1186. editableAtwhoQueryAttrs: {},
  1187. scrollDuration: 150,
  1188. suspendOnComposing: true,
  1189. lookUpOnClick: true
  1190. };
  1191.  
  1192. $.fn.atwho.debug = false;
  1193.  
  1194. }));