rivets.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // Rivets.js
  2. // version: 0.5.11
  3. // author: Michael Richards
  4. // license: MIT
  5. (function() {
  6. var Rivets,
  7. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  8. __slice = [].slice,
  9. __hasProp = {}.hasOwnProperty,
  10. __extends = 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; },
  11. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  12. Rivets = {};
  13. if (!String.prototype.trim) {
  14. String.prototype.trim = function() {
  15. return this.replace(/^\s+|\s+$/g, '');
  16. };
  17. }
  18. Rivets.Binding = (function() {
  19. function Binding(view, el, type, key, keypath, options) {
  20. var identifier, regexp, value, _ref;
  21. this.view = view;
  22. this.el = el;
  23. this.type = type;
  24. this.key = key;
  25. this.keypath = keypath;
  26. this.options = options != null ? options : {};
  27. this.update = __bind(this.update, this);
  28. this.unbind = __bind(this.unbind, this);
  29. this.bind = __bind(this.bind, this);
  30. this.publish = __bind(this.publish, this);
  31. this.sync = __bind(this.sync, this);
  32. this.set = __bind(this.set, this);
  33. this.eventHandler = __bind(this.eventHandler, this);
  34. this.formattedValue = __bind(this.formattedValue, this);
  35. if (!(this.binder = Rivets.internalBinders[this.type] || this.view.binders[type])) {
  36. _ref = this.view.binders;
  37. for (identifier in _ref) {
  38. value = _ref[identifier];
  39. if (identifier !== '*' && identifier.indexOf('*') !== -1) {
  40. regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
  41. if (regexp.test(type)) {
  42. this.binder = value;
  43. this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type);
  44. this.args.shift();
  45. }
  46. }
  47. }
  48. }
  49. this.binder || (this.binder = this.view.binders['*']);
  50. if (this.binder instanceof Function) {
  51. this.binder = {
  52. routine: this.binder
  53. };
  54. }
  55. this.formatters = this.options.formatters || [];
  56. this.model = this.key ? this.view.models[this.key] : this.view.models;
  57. }
  58. Binding.prototype.formattedValue = function(value) {
  59. var args, formatter, id, _i, _len, _ref;
  60. _ref = this.formatters;
  61. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  62. formatter = _ref[_i];
  63. args = formatter.split(/\s+/);
  64. id = args.shift();
  65. formatter = this.model[id] instanceof Function ? this.model[id] : this.view.formatters[id];
  66. if ((formatter != null ? formatter.read : void 0) instanceof Function) {
  67. value = formatter.read.apply(formatter, [value].concat(__slice.call(args)));
  68. } else if (formatter instanceof Function) {
  69. value = formatter.apply(null, [value].concat(__slice.call(args)));
  70. }
  71. }
  72. return value;
  73. };
  74. Binding.prototype.eventHandler = function(fn) {
  75. var binding, handler;
  76. handler = (binding = this).view.config.handler;
  77. return function(ev) {
  78. return handler.call(fn, this, ev, binding);
  79. };
  80. };
  81. Binding.prototype.set = function(value) {
  82. var _ref;
  83. value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value);
  84. return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0;
  85. };
  86. Binding.prototype.sync = function() {
  87. return this.set(this.options.bypass ? this.model[this.keypath] : this.view.config.adapter.read(this.model, this.keypath));
  88. };
  89. Binding.prototype.publish = function() {
  90. var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
  91. value = Rivets.Util.getInputValue(this.el);
  92. _ref = this.formatters.slice(0).reverse();
  93. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  94. formatter = _ref[_i];
  95. args = formatter.split(/\s+/);
  96. id = args.shift();
  97. if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
  98. value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
  99. }
  100. }
  101. return this.view.config.adapter.publish(this.model, this.keypath, value);
  102. };
  103. Binding.prototype.bind = function() {
  104. var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
  105. if ((_ref = this.binder.bind) != null) {
  106. _ref.call(this, this.el);
  107. }
  108. if (this.options.bypass) {
  109. this.sync();
  110. } else {
  111. this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
  112. if (this.view.config.preloadData) {
  113. this.sync();
  114. }
  115. }
  116. if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
  117. _ref2 = this.options.dependencies;
  118. _results = [];
  119. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  120. dependency = _ref2[_i];
  121. if (/^\./.test(dependency)) {
  122. model = this.model;
  123. keypath = dependency.substr(1);
  124. } else {
  125. dependency = dependency.split('.');
  126. model = this.view.models[dependency.shift()];
  127. keypath = dependency.join('.');
  128. }
  129. _results.push(this.view.config.adapter.subscribe(model, keypath, this.sync));
  130. }
  131. return _results;
  132. }
  133. };
  134. Binding.prototype.unbind = function() {
  135. var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
  136. if ((_ref = this.binder.unbind) != null) {
  137. _ref.call(this, this.el);
  138. }
  139. if (!this.options.bypass) {
  140. this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
  141. }
  142. if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
  143. _ref2 = this.options.dependencies;
  144. _results = [];
  145. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  146. dependency = _ref2[_i];
  147. if (/^\./.test(dependency)) {
  148. model = this.model;
  149. keypath = dependency.substr(1);
  150. } else {
  151. dependency = dependency.split('.');
  152. model = this.view.models[dependency.shift()];
  153. keypath = dependency.join('.');
  154. }
  155. _results.push(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
  156. }
  157. return _results;
  158. }
  159. };
  160. Binding.prototype.update = function(models) {
  161. var _ref;
  162. if (models == null) {
  163. models = {};
  164. }
  165. if (this.key) {
  166. if (models[this.key]) {
  167. if (!this.options.bypass) {
  168. this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
  169. }
  170. this.model = models[this.key];
  171. if (this.options.bypass) {
  172. this.sync();
  173. } else {
  174. this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
  175. if (this.view.config.preloadData) {
  176. this.sync();
  177. }
  178. }
  179. }
  180. } else {
  181. this.sync();
  182. }
  183. return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0;
  184. };
  185. return Binding;
  186. })();
  187. Rivets.ComponentBinding = (function(_super) {
  188. __extends(ComponentBinding, _super);
  189. function ComponentBinding(view, el, type) {
  190. var attribute, _i, _len, _ref, _ref1;
  191. this.view = view;
  192. this.el = el;
  193. this.type = type;
  194. this.unbind = __bind(this.unbind, this);
  195. this.bind = __bind(this.bind, this);
  196. this.update = __bind(this.update, this);
  197. this.locals = __bind(this.locals, this);
  198. this.component = Rivets.components[this.type];
  199. this.attributes = {};
  200. this.inflections = {};
  201. _ref = this.el.attributes || [];
  202. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  203. attribute = _ref[_i];
  204. if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) {
  205. this.attributes[attribute.name] = attribute.value;
  206. } else {
  207. this.inflections[attribute.name] = attribute.value;
  208. }
  209. }
  210. }
  211. ComponentBinding.prototype.sync = function() {};
  212. ComponentBinding.prototype.locals = function(models) {
  213. var inverse, key, model, path, result, _i, _len, _ref, _ref1, _ref2;
  214. if (models == null) {
  215. models = this.view.models;
  216. }
  217. result = {};
  218. _ref = this.inflections;
  219. for (key in _ref) {
  220. inverse = _ref[key];
  221. _ref1 = inverse.split('.');
  222. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  223. path = _ref1[_i];
  224. result[key] = (result[key] || models)[path];
  225. }
  226. }
  227. for (key in models) {
  228. model = models[key];
  229. if ((_ref2 = result[key]) == null) {
  230. result[key] = model;
  231. }
  232. }
  233. return result;
  234. };
  235. ComponentBinding.prototype.update = function(models) {
  236. var _ref;
  237. return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0;
  238. };
  239. ComponentBinding.prototype.bind = function() {
  240. var el, _ref;
  241. if (this.componentView != null) {
  242. return (_ref = this.componentView) != null ? _ref.bind() : void 0;
  243. } else {
  244. el = this.component.build.call(this.attributes);
  245. (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind();
  246. return this.el.parentNode.replaceChild(el, this.el);
  247. }
  248. };
  249. ComponentBinding.prototype.unbind = function() {
  250. var _ref;
  251. return (_ref = this.componentView) != null ? _ref.unbind() : void 0;
  252. };
  253. return ComponentBinding;
  254. })(Rivets.Binding);
  255. Rivets.View = (function() {
  256. function View(els, models, options) {
  257. var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
  258. this.els = els;
  259. this.models = models;
  260. this.options = options != null ? options : {};
  261. this.update = __bind(this.update, this);
  262. this.publish = __bind(this.publish, this);
  263. this.sync = __bind(this.sync, this);
  264. this.unbind = __bind(this.unbind, this);
  265. this.bind = __bind(this.bind, this);
  266. this.select = __bind(this.select, this);
  267. this.build = __bind(this.build, this);
  268. this.componentRegExp = __bind(this.componentRegExp, this);
  269. this.bindingRegExp = __bind(this.bindingRegExp, this);
  270. if (!(this.els.jquery || this.els instanceof Array)) {
  271. this.els = [this.els];
  272. }
  273. _ref = ['config', 'binders', 'formatters'];
  274. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  275. option = _ref[_i];
  276. this[option] = {};
  277. if (this.options[option]) {
  278. _ref1 = this.options[option];
  279. for (k in _ref1) {
  280. v = _ref1[k];
  281. this[option][k] = v;
  282. }
  283. }
  284. _ref2 = Rivets[option];
  285. for (k in _ref2) {
  286. v = _ref2[k];
  287. if ((_ref3 = (_base = this[option])[k]) == null) {
  288. _base[k] = v;
  289. }
  290. }
  291. }
  292. this.build();
  293. }
  294. View.prototype.bindingRegExp = function() {
  295. var prefix;
  296. prefix = this.config.prefix;
  297. if (prefix) {
  298. return new RegExp("^data-" + prefix + "-");
  299. } else {
  300. return /^data-/;
  301. }
  302. };
  303. View.prototype.componentRegExp = function() {
  304. var _ref, _ref1;
  305. return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-");
  306. };
  307. View.prototype.build = function() {
  308. var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref,
  309. _this = this;
  310. this.bindings = [];
  311. skipNodes = [];
  312. bindingRegExp = this.bindingRegExp();
  313. componentRegExp = this.componentRegExp();
  314. buildBinding = function(node, type, declaration) {
  315. var context, ctx, dependencies, key, keypath, options, path, pipe, pipes, splitPath;
  316. options = {};
  317. pipes = (function() {
  318. var _i, _len, _ref, _results;
  319. _ref = declaration.split('|');
  320. _results = [];
  321. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  322. pipe = _ref[_i];
  323. _results.push(pipe.trim());
  324. }
  325. return _results;
  326. })();
  327. context = (function() {
  328. var _i, _len, _ref, _results;
  329. _ref = pipes.shift().split('<');
  330. _results = [];
  331. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  332. ctx = _ref[_i];
  333. _results.push(ctx.trim());
  334. }
  335. return _results;
  336. })();
  337. path = context.shift();
  338. splitPath = path.split(/\.|:/);
  339. options.formatters = pipes;
  340. options.bypass = path.indexOf(':') !== -1;
  341. if (splitPath[0]) {
  342. key = splitPath.shift();
  343. } else {
  344. key = null;
  345. splitPath.shift();
  346. }
  347. keypath = splitPath.join('.');
  348. if (dependencies = context.shift()) {
  349. options.dependencies = dependencies.split(/\s+/);
  350. }
  351. return _this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
  352. };
  353. parse = function(node) {
  354. var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results;
  355. if (__indexOf.call(skipNodes, node) < 0) {
  356. if (node.nodeType === Node.TEXT_NODE) {
  357. parser = Rivets.TextTemplateParser;
  358. if (delimiters = _this.config.templateDelimiters) {
  359. if ((tokens = parser.parse(node.data, delimiters)).length) {
  360. if (!(tokens.length === 1 && tokens[0].type === parser.types.text)) {
  361. startToken = tokens[0], restTokens = 2 <= tokens.length ? __slice.call(tokens, 1) : [];
  362. node.data = startToken.value;
  363. switch (startToken.type) {
  364. case 0:
  365. node.data = startToken.value;
  366. break;
  367. case 1:
  368. buildBinding(node, 'textNode', startToken.value);
  369. }
  370. for (_i = 0, _len = restTokens.length; _i < _len; _i++) {
  371. token = restTokens[_i];
  372. node.parentNode.appendChild((text = document.createTextNode(token.value)));
  373. if (token.type === 1) {
  374. buildBinding(text, 'textNode', token.value);
  375. }
  376. }
  377. }
  378. }
  379. }
  380. } else if (componentRegExp.test(node.tagName)) {
  381. type = node.tagName.replace(componentRegExp, '').toLowerCase();
  382. _this.bindings.push(new Rivets.ComponentBinding(_this, node, type));
  383. } else if (node.attributes != null) {
  384. _ref = node.attributes;
  385. for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
  386. attribute = _ref[_j];
  387. if (bindingRegExp.test(attribute.name)) {
  388. type = attribute.name.replace(bindingRegExp, '');
  389. if (!(binder = _this.binders[type])) {
  390. _ref1 = _this.binders;
  391. for (identifier in _ref1) {
  392. value = _ref1[identifier];
  393. if (identifier !== '*' && identifier.indexOf('*') !== -1) {
  394. regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
  395. if (regexp.test(type)) {
  396. binder = value;
  397. }
  398. }
  399. }
  400. }
  401. binder || (binder = _this.binders['*']);
  402. if (binder.block) {
  403. _ref2 = node.childNodes;
  404. for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
  405. n = _ref2[_k];
  406. skipNodes.push(n);
  407. }
  408. attributes = [attribute];
  409. }
  410. }
  411. }
  412. _ref3 = attributes || node.attributes;
  413. for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
  414. attribute = _ref3[_l];
  415. if (bindingRegExp.test(attribute.name)) {
  416. type = attribute.name.replace(bindingRegExp, '');
  417. buildBinding(node, type, attribute.value);
  418. }
  419. }
  420. }
  421. _ref4 = node.childNodes;
  422. _results = [];
  423. for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) {
  424. childNode = _ref4[_m];
  425. _results.push(parse(childNode));
  426. }
  427. return _results;
  428. }
  429. };
  430. _ref = this.els;
  431. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  432. el = _ref[_i];
  433. parse(el);
  434. }
  435. };
  436. View.prototype.select = function(fn) {
  437. var binding, _i, _len, _ref, _results;
  438. _ref = this.bindings;
  439. _results = [];
  440. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  441. binding = _ref[_i];
  442. if (fn(binding)) {
  443. _results.push(binding);
  444. }
  445. }
  446. return _results;
  447. };
  448. View.prototype.bind = function() {
  449. var binding, _i, _len, _ref, _results;
  450. _ref = this.bindings;
  451. _results = [];
  452. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  453. binding = _ref[_i];
  454. _results.push(binding.bind());
  455. }
  456. return _results;
  457. };
  458. View.prototype.unbind = function() {
  459. var binding, _i, _len, _ref, _results;
  460. _ref = this.bindings;
  461. _results = [];
  462. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  463. binding = _ref[_i];
  464. _results.push(binding.unbind());
  465. }
  466. return _results;
  467. };
  468. View.prototype.sync = function() {
  469. var binding, _i, _len, _ref, _results;
  470. _ref = this.bindings;
  471. _results = [];
  472. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  473. binding = _ref[_i];
  474. _results.push(binding.sync());
  475. }
  476. return _results;
  477. };
  478. View.prototype.publish = function() {
  479. var binding, _i, _len, _ref, _results;
  480. _ref = this.select(function(b) {
  481. return b.binder.publishes;
  482. });
  483. _results = [];
  484. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  485. binding = _ref[_i];
  486. _results.push(binding.publish());
  487. }
  488. return _results;
  489. };
  490. View.prototype.update = function(models) {
  491. var binding, key, model, _i, _len, _ref, _results;
  492. if (models == null) {
  493. models = {};
  494. }
  495. for (key in models) {
  496. model = models[key];
  497. this.models[key] = model;
  498. }
  499. _ref = this.bindings;
  500. _results = [];
  501. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  502. binding = _ref[_i];
  503. _results.push(binding.update(models));
  504. }
  505. return _results;
  506. };
  507. return View;
  508. })();
  509. Rivets.TextTemplateParser = (function() {
  510. function TextTemplateParser() {}
  511. TextTemplateParser.types = {
  512. text: 0,
  513. binding: 1
  514. };
  515. TextTemplateParser.parse = function(template, delimiters) {
  516. var index, lastIndex, lastToken, length, substring, tokens, value;
  517. tokens = [];
  518. length = template.length;
  519. index = 0;
  520. lastIndex = 0;
  521. while (lastIndex < length) {
  522. index = template.indexOf(delimiters[0], lastIndex);
  523. if (index < 0) {
  524. tokens.push({
  525. type: this.types.text,
  526. value: template.slice(lastIndex)
  527. });
  528. break;
  529. } else {
  530. if (index > 0 && lastIndex < index) {
  531. tokens.push({
  532. type: this.types.text,
  533. value: template.slice(lastIndex, index)
  534. });
  535. }
  536. lastIndex = index + 2;
  537. index = template.indexOf(delimiters[1], lastIndex);
  538. if (index < 0) {
  539. substring = template.slice(lastIndex - 2);
  540. lastToken = tokens[tokens.length - 1];
  541. if ((lastToken != null ? lastToken.type : void 0) === this.types.text) {
  542. lastToken.value += substring;
  543. } else {
  544. tokens.push({
  545. type: this.types.text,
  546. value: substring
  547. });
  548. }
  549. break;
  550. }
  551. value = template.slice(lastIndex, index).trim();
  552. tokens.push({
  553. type: this.types.binding,
  554. value: value
  555. });
  556. lastIndex = index + 2;
  557. }
  558. }
  559. return tokens;
  560. };
  561. return TextTemplateParser;
  562. })();
  563. Rivets.Util = {
  564. bindEvent: function(el, event, handler) {
  565. if (window.jQuery != null) {
  566. el = jQuery(el);
  567. if (el.on != null) {
  568. return el.on(event, handler);
  569. } else {
  570. return el.bind(event, handler);
  571. }
  572. } else if (window.addEventListener != null) {
  573. return el.addEventListener(event, handler, false);
  574. } else {
  575. event = 'on' + event;
  576. return el.attachEvent(event, handler);
  577. }
  578. },
  579. unbindEvent: function(el, event, handler) {
  580. if (window.jQuery != null) {
  581. el = jQuery(el);
  582. if (el.off != null) {
  583. return el.off(event, handler);
  584. } else {
  585. return el.unbind(event, handler);
  586. }
  587. } else if (window.removeEventListener != null) {
  588. return el.removeEventListener(event, handler, false);
  589. } else {
  590. event = 'on' + event;
  591. return el.detachEvent(event, handler);
  592. }
  593. },
  594. getInputValue: function(el) {
  595. var o, _i, _len, _results;
  596. if (window.jQuery != null) {
  597. el = jQuery(el);
  598. switch (el[0].type) {
  599. case 'checkbox':
  600. return el.is(':checked');
  601. default:
  602. return el.val();
  603. }
  604. } else {
  605. switch (el.type) {
  606. case 'checkbox':
  607. return el.checked;
  608. case 'select-multiple':
  609. _results = [];
  610. for (_i = 0, _len = el.length; _i < _len; _i++) {
  611. o = el[_i];
  612. if (o.selected) {
  613. _results.push(o.value);
  614. }
  615. }
  616. return _results;
  617. break;
  618. default:
  619. return el.value;
  620. }
  621. }
  622. }
  623. };
  624. Rivets.binders = {
  625. enabled: function(el, value) {
  626. return el.disabled = !value;
  627. },
  628. disabled: function(el, value) {
  629. return el.disabled = !!value;
  630. },
  631. checked: {
  632. publishes: true,
  633. bind: function(el) {
  634. return Rivets.Util.bindEvent(el, 'change', this.publish);
  635. },
  636. unbind: function(el) {
  637. return Rivets.Util.unbindEvent(el, 'change', this.publish);
  638. },
  639. routine: function(el, value) {
  640. var _ref;
  641. if (el.type === 'radio') {
  642. return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
  643. } else {
  644. return el.checked = !!value;
  645. }
  646. }
  647. },
  648. unchecked: {
  649. publishes: true,
  650. bind: function(el) {
  651. return Rivets.Util.bindEvent(el, 'change', this.publish);
  652. },
  653. unbind: function(el) {
  654. return Rivets.Util.unbindEvent(el, 'change', this.publish);
  655. },
  656. routine: function(el, value) {
  657. var _ref;
  658. if (el.type === 'radio') {
  659. return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
  660. } else {
  661. return el.checked = !value;
  662. }
  663. }
  664. },
  665. show: function(el, value) {
  666. return el.style.display = value ? '' : 'none';
  667. },
  668. hide: function(el, value) {
  669. return el.style.display = value ? 'none' : '';
  670. },
  671. html: function(el, value) {
  672. return el.innerHTML = value != null ? value : '';
  673. },
  674. value: {
  675. publishes: true,
  676. bind: function(el) {
  677. return Rivets.Util.bindEvent(el, 'change', this.publish);
  678. },
  679. unbind: function(el) {
  680. return Rivets.Util.unbindEvent(el, 'change', this.publish);
  681. },
  682. routine: function(el, value) {
  683. var o, _i, _len, _ref, _ref1, _ref2, _results;
  684. if (window.jQuery != null) {
  685. el = jQuery(el);
  686. if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
  687. return el.val(value != null ? value : '');
  688. }
  689. } else {
  690. if (el.type === 'select-multiple') {
  691. if (value != null) {
  692. _results = [];
  693. for (_i = 0, _len = el.length; _i < _len; _i++) {
  694. o = el[_i];
  695. _results.push(o.selected = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
  696. }
  697. return _results;
  698. }
  699. } else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.toString() : void 0)) {
  700. return el.value = value != null ? value : '';
  701. }
  702. }
  703. }
  704. },
  705. text: function(el, value) {
  706. if (el.innerText != null) {
  707. return el.innerText = value != null ? value : '';
  708. } else {
  709. return el.textContent = value != null ? value : '';
  710. }
  711. },
  712. "if": {
  713. block: true,
  714. bind: function(el) {
  715. var attr, declaration;
  716. if (this.marker == null) {
  717. attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
  718. declaration = el.getAttribute(attr);
  719. this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " ");
  720. el.removeAttribute(attr);
  721. el.parentNode.insertBefore(this.marker, el);
  722. return el.parentNode.removeChild(el);
  723. }
  724. },
  725. unbind: function() {
  726. var _ref;
  727. return (_ref = this.nested) != null ? _ref.unbind() : void 0;
  728. },
  729. routine: function(el, value) {
  730. var key, model, models, options, _ref;
  731. if (!!value === (this.nested == null)) {
  732. if (value) {
  733. models = {};
  734. _ref = this.view.models;
  735. for (key in _ref) {
  736. model = _ref[key];
  737. models[key] = model;
  738. }
  739. options = {
  740. binders: this.view.options.binders,
  741. formatters: this.view.options.formatters,
  742. config: this.view.options.config
  743. };
  744. (this.nested = new Rivets.View(el, models, options)).bind();
  745. return this.marker.parentNode.insertBefore(el, this.marker.nextSibling);
  746. } else {
  747. el.parentNode.removeChild(el);
  748. this.nested.unbind();
  749. return delete this.nested;
  750. }
  751. }
  752. },
  753. update: function(models) {
  754. return this.nested.update(models);
  755. }
  756. },
  757. unless: {
  758. block: true,
  759. bind: function(el) {
  760. return Rivets.binders["if"].bind.call(this, el);
  761. },
  762. unbind: function() {
  763. return Rivets.binders["if"].unbind.call(this);
  764. },
  765. routine: function(el, value) {
  766. return Rivets.binders["if"].routine.call(this, el, !value);
  767. },
  768. update: function(models) {
  769. return Rivets.binders["if"].update.call(this, models);
  770. }
  771. },
  772. "on-*": {
  773. "function": true,
  774. unbind: function(el) {
  775. if (this.handler) {
  776. return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
  777. }
  778. },
  779. routine: function(el, value) {
  780. if (this.handler) {
  781. Rivets.Util.unbindEvent(el, this.args[0], this.handler);
  782. }
  783. return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
  784. }
  785. },
  786. "each-*": {
  787. block: true,
  788. bind: function(el) {
  789. var attr;
  790. if (this.marker == null) {
  791. attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
  792. this.marker = document.createComment(" rivets: " + this.type + " ");
  793. this.iterated = [];
  794. el.removeAttribute(attr);
  795. el.parentNode.insertBefore(this.marker, el);
  796. return el.parentNode.removeChild(el);
  797. }
  798. },
  799. unbind: function(el) {
  800. var view, _i, _len, _ref, _results;
  801. if (this.iterated != null) {
  802. _ref = this.iterated;
  803. _results = [];
  804. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  805. view = _ref[_i];
  806. _results.push(view.unbind());
  807. }
  808. return _results;
  809. }
  810. },
  811. routine: function(el, collection) {
  812. var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
  813. modelName = this.args[0];
  814. collection = collection || [];
  815. if (this.iterated.length > collection.length) {
  816. _ref = Array(this.iterated.length - collection.length);
  817. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  818. i = _ref[_i];
  819. view = this.iterated.pop();
  820. view.unbind();
  821. this.marker.parentNode.removeChild(view.els[0]);
  822. }
  823. }
  824. _results = [];
  825. for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
  826. model = collection[index];
  827. data = {};
  828. data[modelName] = model;
  829. if (this.iterated[index] == null) {
  830. _ref1 = this.view.models;
  831. for (key in _ref1) {
  832. model = _ref1[key];
  833. if ((_ref2 = data[key]) == null) {
  834. data[key] = model;
  835. }
  836. }
  837. previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
  838. options = {
  839. binders: this.view.options.binders,
  840. formatters: this.view.options.formatters,
  841. config: {}
  842. };
  843. _ref3 = this.view.options.config;
  844. for (k in _ref3) {
  845. v = _ref3[k];
  846. options.config[k] = v;
  847. }
  848. options.config.preloadData = true;
  849. template = el.cloneNode(true);
  850. view = new Rivets.View(template, data, options);
  851. view.bind();
  852. this.iterated.push(view);
  853. _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
  854. } else if (this.iterated[index].models[modelName] !== model) {
  855. _results.push(this.iterated[index].update(data));
  856. } else {
  857. _results.push(void 0);
  858. }
  859. }
  860. return _results;
  861. },
  862. update: function(models) {
  863. var data, key, model, view, _i, _len, _ref, _results;
  864. data = {};
  865. for (key in models) {
  866. model = models[key];
  867. if (key !== this.args[0]) {
  868. data[key] = model;
  869. }
  870. }
  871. _ref = this.iterated;
  872. _results = [];
  873. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  874. view = _ref[_i];
  875. _results.push(view.update(data));
  876. }
  877. return _results;
  878. }
  879. },
  880. "class-*": function(el, value) {
  881. var elClass;
  882. elClass = " " + el.className + " ";
  883. if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) {
  884. return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim();
  885. }
  886. },
  887. "*": function(el, value) {
  888. if (value) {
  889. return el.setAttribute(this.type, value);
  890. } else {
  891. return el.removeAttribute(this.type);
  892. }
  893. }
  894. };
  895. Rivets.internalBinders = {
  896. textNode: function(node, value) {
  897. return node.data = value != null ? value : '';
  898. }
  899. };
  900. Rivets.components = {};
  901. Rivets.config = {
  902. preloadData: true,
  903. handler: function(context, ev, binding) {
  904. return this.call(context, ev, binding.view.models);
  905. }
  906. };
  907. Rivets.formatters = {};
  908. Rivets.factory = function(exports) {
  909. exports._ = Rivets;
  910. exports.binders = Rivets.binders;
  911. exports.components = Rivets.components;
  912. exports.formatters = Rivets.formatters;
  913. exports.config = Rivets.config;
  914. exports.configure = function(options) {
  915. var property, value;
  916. if (options == null) {
  917. options = {};
  918. }
  919. for (property in options) {
  920. value = options[property];
  921. Rivets.config[property] = value;
  922. }
  923. };
  924. return exports.bind = function(el, models, options) {
  925. var view;
  926. if (models == null) {
  927. models = {};
  928. }
  929. if (options == null) {
  930. options = {};
  931. }
  932. view = new Rivets.View(el, models, options);
  933. view.bind();
  934. return view;
  935. };
  936. };
  937. if (typeof exports === 'object') {
  938. Rivets.factory(exports);
  939. } else if (typeof define === 'function' && define.amd) {
  940. define(['exports'], function(exports) {
  941. Rivets.factory(this.rivets = exports);
  942. return exports;
  943. });
  944. } else {
  945. Rivets.factory(this.rivets = {});
  946. }
  947. }).call(this);