stimulusjs-rails 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c63f2c8aaa21bd73c621b44a07bd678afd06ac6e
4
- data.tar.gz: 9c6b77a3ab67d33b54f615d5af8da5cb9d282ea8
2
+ SHA256:
3
+ metadata.gz: 5aed8203bf2b7d71994937f5af4633a877e71baa1b823f6421b731b6e083705a
4
+ data.tar.gz: e8061afb836dfa4dc034804e3da23bb6e9012a2c241b8c292bbc6c8a910d51a2
5
5
  SHA512:
6
- metadata.gz: ad001a34f571c90c8d2f861ba610b57360264498facdeb9a53f3885faae643264e4acc167471114800b0691758b03a0944dd74db58929e5b6fea78ab56ebd753
7
- data.tar.gz: 64c1ecf45df8e2c575e450e2b28e041a8af667f206b698e484572bd9fd07b72b46bd5bcadfa081c2c748c8968fe81538d091c195792192d2fcd1873029f02b70
6
+ metadata.gz: 31f490c1314cf6dd024f31257e6c15b8c1ebf3f8c9efc8e9511a4a88ee2eb2e82c1f0274318da10039c594ff964f3064f8655d4e6c0d91d75dad3d2501389035
7
+ data.tar.gz: a47eeb5ac99b7f8be0de7f78c82bb7370e907a2aed1a068567a7d99b7a470a43a0282fdf702a97910dfd5a133800b93dff590abee8dfdb9b152fad51f22e7573
data/Readme.md CHANGED
@@ -3,28 +3,55 @@
3
3
  [![Gem](https://img.shields.io/gem/v/stimulusjs-rails.svg)](https://rubygems.org/gems/stimulusjs-rails)
4
4
  [![Gem](https://img.shields.io/gem/dt/stimulusjs-rails.svg)](https://rubygems.org/gems/stimulusjs-rails)
5
5
 
6
- ### About
6
+ ## About
7
7
 
8
8
  Rails 4.2+ asset-pipeline gem to provide Stimulus.js
9
9
 
10
- ### Package Versions
10
+ ## Package Versions
11
11
 
12
+ - Stimulus 1.1.1
12
13
  - Stimulus 1.0.1
13
14
 
14
- ### Setup
15
+ ## Setup
15
16
 
16
- Have in your Gemfile:
17
+ Have in your `Gemfile`:
17
18
 
18
19
  ```ruby
19
- gem 'stimulusjs-rails'
20
+ gem 'stimulusjs-rails', '~> 1.1.1'
20
21
  ```
21
22
 
22
- And in your application.js manifest:
23
+ And in your `application.js` manifest:
23
24
 
24
25
  ```js
25
26
  //= require stimulus
27
+
28
+ window.application = Stimulus.Application.start()
26
29
  ```
27
30
 
31
+ ## Example
32
+
33
+ Add in your `application.js`
34
+
35
+ ```js
36
+ //= require controllers/hello_controller
37
+ ```
38
+
39
+ and in `controllers/hello_controller.es6`
40
+
41
+ ```js
42
+ window.application.register('hello', class extends Stimulus.Controller {
43
+ static get targets() {
44
+ return [ "name" ]
45
+ }
46
+
47
+ // …
48
+ })
49
+ ```
50
+
51
+ ## Tips
52
+
53
+ See this for Sprocket + ES6 support: https://github.com/rails/sprockets#es6-support
54
+
28
55
  ## Contributing
29
56
 
30
57
  Contributions are welcome, please follow [GitHub Flow](https://guides.github.com/introduction/flow/index.html)
@@ -1,5 +1,5 @@
1
1
  module Stimulus
2
2
  module Rails
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.1'
4
4
  end
5
5
  end
@@ -1,5 +1,1754 @@
1
1
  /*
2
- Stimulus 1.0.1
3
- Copyright © 2018 Basecamp, LLC
2
+ Stimulus 1.1.1
3
+ Copyright © 2019 Basecamp, LLC
4
4
  */
5
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Stimulus=e():t.Stimulus=e()}("undefined"!=typeof self?self:this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";function r(t){throw new Error(t)}function o(t){return"window"==t?window:"document"==t?document:void 0}function i(t){return t==window?"window":t==document?"document":void 0}function c(t,e,n){u(t,e).add(n)}function s(t,e,n){u(t,e).delete(n),a(t,e)}function u(t,e){var n=t.get(e);return n||(n=new Set,t.set(e,n)),n}function a(t,e){var n=t.get(e);null!=n&&0==n.size&&t.delete(e)}function l(t){return t.toString().replace(/([A-Z])/g,function(t,e){return"-"+e.toLowerCase()})}function f(t,e){return"["+t+'~="'+e+'"]'}function h(t){return{identifier:t.identifier,controllerConstructor:p(t.controllerConstructor)}}function p(t){var e=I(t);return e.bless(),e}function d(t){var e=t.prototype;m(t).forEach(function(t){return b(e,(n={},n[t+"Target"]={get:function(){var e=this.targets.find(t);if(e)return e;throw new Error('Missing target element "'+this.identifier+"."+t+'"')}},n[t+"Targets"]={get:function(){return this.targets.findAll(t)}},n["has"+v(t)+"Target"]={get:function(){return this.targets.has(t)}},n));var n})}function m(t){var e=y(t);return Array.from(e.reduce(function(t,e){return g(e).forEach(function(e){return t.add(e)}),t},new Set))}function y(t){for(var e=[];t;)e.push(t),t=Object.getPrototypeOf(t);return e}function g(t){var e=t.targets;return Array.isArray(e)?e:[]}function b(t,e){Object.keys(e).forEach(function(n){if(!(n in t)){var r=e[n];Object.defineProperty(t,n,r)}})}function v(t){return t.charAt(0).toUpperCase()+t.slice(1)}Object.defineProperty(e,"__esModule",{value:!0});var E=function(){function t(t,e,n){this.context=t,this.descriptor=e,this.eventTarget=n}return t.prototype.connect=function(){this.eventTarget.addEventListener(this.eventName,this,!1)},t.prototype.disconnect=function(){this.eventTarget.removeEventListener(this.eventName,this,!1)},t.prototype.hasSameDescriptorAs=function(t){return null!=t&&t.descriptor.isEqualTo(this.descriptor)},t.prototype.handleEvent=function(t){this.willBeInvokedByEvent(t)&&this.invokeWithEvent(t)},Object.defineProperty(t.prototype,"eventName",{get:function(){return this.descriptor.eventName},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"method",{get:function(){var t=this.controller[this.methodName];if("function"==typeof t)return t;throw new Error('Action "'+this.descriptor+'" references undefined method "'+this.methodName+'"')},enumerable:!0,configurable:!0}),t.prototype.invokeWithEvent=function(t){try{this.method.call(this.controller,t)}catch(e){this.context.handleError(e,'invoking action "'+this.descriptor+'"',{event:t})}},t.prototype.willBeInvokedByEvent=function(t){var e=t.target;return this.element===e||(!(e instanceof Element&&this.element.contains(e))||this.scope.containsElement(e))},Object.defineProperty(t.prototype,"controller",{get:function(){return this.context.controller},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"methodName",{get:function(){return this.descriptor.methodName},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scope",{get:function(){return this.context.scope},enumerable:!0,configurable:!0}),t}(),A=/^((.+?)(@(window|document))?->)?(.+?)#(.+)$/,O=function(){function t(t,e,n,r){this.identifier=t,this.eventName=e,this.methodName=n,this.eventTarget=r}return t.forOptions=function(e){return new t(e.identifier||r("Missing identifier in action descriptor"),e.eventName||r("Missing event name in action descriptor"),e.methodName||r("Missing method name in action descriptor"),e.eventTarget||r("Missing event target in action descriptor"))},t.forElementWithInlineDescriptorString=function(e,n){try{var r=this.parseOptionsFromInlineActionDescriptorString(n);return r.eventName=r.eventName||this.getDefaultEventNameForElement(e),r.eventTarget=r.eventTarget||e,t.forOptions(r)}catch(t){throw new Error('Bad action descriptor "'+n+'": '+t.message)}},t.parseOptionsFromInlineActionDescriptorString=function(t){var e=t.trim(),n=e.match(A)||r("Invalid action descriptor syntax");return{identifier:n[5],eventName:n[2],methodName:n[6],eventTarget:o(n[4])}},t.getDefaultEventNameForElement=function(t){return this.defaultEventNames[t.tagName.toLowerCase()](t)},Object.defineProperty(t.prototype,"eventTargetName",{get:function(){return i(this.eventTarget)},enumerable:!0,configurable:!0}),t.prototype.isEqualTo=function(t){return null!=t&&t.identifier==this.identifier&&t.eventName==this.eventName&&t.methodName==this.methodName&&t.eventTarget==this.eventTarget},t.prototype.toString=function(){var t=this.eventTargetName?"@"+this.eventTargetName:"";return""+this.eventName+t+"->"+this.identifier+"#"+this.methodName},t.defaultEventNames={a:function(t){return"click"},button:function(t){return"click"},form:function(t){return"submit"},input:function(t){return"submit"==t.getAttribute("type")?"click":"change"},select:function(t){return"change"},textarea:function(t){return"change"}},t}(),N=function(){function t(t){this.context=t,this.started=!1,this.actions=new Set}return t.prototype.start=function(){this.started||(this.started=!0,this.connectActions())},t.prototype.stop=function(){this.started&&(this.disconnectActions(),this.started=!1)},t.prototype.add=function(t){this.actions.has(t)||(t.connect(),this.actions.add(t))},t.prototype.delete=function(t){this.actions.has(t)&&(this.actions.delete(t),t.disconnect())},t.prototype.connectActions=function(){this.actions.forEach(function(t){return t.connect()})},t.prototype.disconnectActions=function(){this.actions.forEach(function(t){return t.disconnect()})},t}(),j=function(){function t(){this.valuesByKey=new Map}return Object.defineProperty(t.prototype,"values",{get:function(){return Array.from(this.valuesByKey.values()).reduce(function(t,e){return t.concat(Array.from(e))},[])},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"size",{get:function(){return Array.from(this.valuesByKey.values()).reduce(function(t,e){return t+e.size},0)},enumerable:!0,configurable:!0}),t.prototype.add=function(t,e){c(this.valuesByKey,t,e)},t.prototype.delete=function(t,e){s(this.valuesByKey,t,e)},t.prototype.has=function(t,e){var n=this.valuesByKey.get(t);return null!=n&&n.has(e)},t.prototype.hasKey=function(t){return this.valuesByKey.has(t)},t.prototype.hasValue=function(t){return Array.from(this.valuesByKey.values()).some(function(e){return e.has(t)})},t.prototype.getValuesForKey=function(t){var e=this.valuesByKey.get(t);return e?Array.from(e):[]},t.prototype.getKeysForValue=function(t){return Array.from(this.valuesByKey).filter(function(e){e[0];return e[1].has(t)}).map(function(t){var e=t[0];t[1];return e})},t}(),w=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),P=function(t){function e(){var e=t.call(this)||this;return e.keysByValue=new Map,e}return w(e,t),Object.defineProperty(e.prototype,"values",{get:function(){return Array.from(this.keysByValue.keys())},enumerable:!0,configurable:!0}),e.prototype.add=function(e,n){t.prototype.add.call(this,e,n),c(this.keysByValue,n,e)},e.prototype.delete=function(e,n){t.prototype.delete.call(this,e,n),s(this.keysByValue,n,e)},e.prototype.hasValue=function(t){return this.keysByValue.has(t)},e.prototype.getKeysForValue=function(t){var e=this.keysByValue.get(t);return e?Array.from(e):[]},e}(j),T=function(){function t(t,e){var n=this;this.element=t,this.started=!1,this.delegate=e,this.elements=new Set,this.mutationObserver=new MutationObserver(function(t){return n.processMutations(t)})}return t.prototype.start=function(){this.started||(this.mutationObserver.observe(this.element,{attributes:!0,childList:!0,subtree:!0}),this.started=!0,this.refresh())},t.prototype.stop=function(){this.started&&(this.mutationObserver.takeRecords(),this.mutationObserver.disconnect(),this.started=!1)},t.prototype.refresh=function(){if(this.started){for(var t=new Set(this.matchElementsInTree()),e=0,n=Array.from(this.elements);e<n.length;e++){var r=n[e];t.has(r)||this.removeElement(r)}for(var o=0,i=Array.from(t);o<i.length;o++){var r=i[o];this.addElement(r)}}},t.prototype.processMutations=function(t){for(var e=0,n=t;e<n.length;e++){var r=n[e];this.processMutation(r)}},t.prototype.processMutation=function(t){"attributes"==t.type?this.processAttributeChange(t.target,t.attributeName):"childList"==t.type&&(this.processRemovedNodes(t.removedNodes),this.processAddedNodes(t.addedNodes))},t.prototype.processAttributeChange=function(t,e){var n=t;this.elements.has(n)?this.delegate.elementAttributeChanged&&this.matchElement(n)?this.delegate.elementAttributeChanged(n,e):this.removeElement(n):this.matchElement(n)&&this.addElement(n)},t.prototype.processRemovedNodes=function(t){for(var e=0,n=Array.from(t);e<n.length;e++){var r=n[e];this.processNode(r,this.removeElement)}},t.prototype.processAddedNodes=function(t){for(var e=0,n=Array.from(t);e<n.length;e++){var r=n[e];this.processNode(r,this.addElement)}},t.prototype.matchElement=function(t){return this.delegate.matchElement(t)},t.prototype.matchElementsInTree=function(t){return void 0===t&&(t=this.element),this.delegate.matchElementsInTree(t)},t.prototype.processNode=function(t,e){var n=this.elementFromNode(t);if(n)for(var r=0,o=this.matchElementsInTree(n);r<o.length;r++){var i=o[r];e.call(this,i)}},t.prototype.elementFromNode=function(t){if(t.nodeType==Node.ELEMENT_NODE)return t},t.prototype.addElement=function(t){this.elements.has(t)||(this.elements.add(t),this.delegate.elementMatched&&this.delegate.elementMatched(t))},t.prototype.removeElement=function(t){this.elements.has(t)&&(this.elements.delete(t),this.delegate.elementUnmatched&&this.delegate.elementUnmatched(t))},t}(),F=(function(){function t(t,e,n){this.attributeName=e,this.delegate=n,this.elementObserver=new T(t,this)}Object.defineProperty(t.prototype,"element",{get:function(){return this.elementObserver.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"selector",{get:function(){return"["+this.attributeName+"]"},enumerable:!0,configurable:!0}),t.prototype.start=function(){this.elementObserver.start()},t.prototype.stop=function(){this.elementObserver.stop()},t.prototype.matchElement=function(t){return t.hasAttribute(this.attributeName)},t.prototype.matchElementsInTree=function(t){var e=this.matchElement(t)?[t]:[],n=Array.from(t.querySelectorAll(this.selector));return e.concat(n)},t.prototype.elementMatched=function(t){this.delegate.elementMatchedAttribute&&this.delegate.elementMatchedAttribute(t,this.attributeName)},t.prototype.elementUnmatched=function(t){this.delegate.elementUnmatchedAttribute&&this.delegate.elementUnmatchedAttribute(t,this.attributeName)},t.prototype.elementAttributeChanged=function(t,e){this.delegate.elementAttributeValueChanged&&this.attributeName==e&&this.delegate.elementAttributeValueChanged(t,e)}}(),function(){function t(t,e,n){this.attributeName=e,this.delegate=n,this.elementObserver=new T(t,this),this.tokensByElement=new P}return Object.defineProperty(t.prototype,"started",{get:function(){return this.elementObserver.started},enumerable:!0,configurable:!0}),t.prototype.start=function(){this.elementObserver.start()},t.prototype.stop=function(){this.elementObserver.stop()},t.prototype.refresh=function(){this.elementObserver.refresh()},Object.defineProperty(t.prototype,"element",{get:function(){return this.elementObserver.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"selector",{get:function(){return"["+this.attributeName+"]"},enumerable:!0,configurable:!0}),t.prototype.getElementsMatchingToken=function(t){return this.tokensByElement.getKeysForValue(t)},t.prototype.matchElement=function(t){return t.hasAttribute(this.attributeName)},t.prototype.matchElementsInTree=function(t){var e=this.matchElement(t)?[t]:[],n=Array.from(t.querySelectorAll(this.selector));return e.concat(n)},t.prototype.elementMatched=function(t){for(var e=Array.from(this.readTokenSetForElement(t)),n=0,r=e;n<r.length;n++){var o=r[n];this.elementMatchedToken(t,o)}},t.prototype.elementUnmatched=function(t){for(var e=this.getTokensForElement(t),n=0,r=e;n<r.length;n++){var o=r[n];this.elementUnmatchedToken(t,o)}},t.prototype.elementAttributeChanged=function(t){for(var e=this.readTokenSetForElement(t),n=0,r=Array.from(e);n<r.length;n++){var o=r[n];this.elementMatchedToken(t,o)}for(var i=0,c=this.getTokensForElement(t);i<c.length;i++){var o=c[i];e.has(o)||this.elementUnmatchedToken(t,o)}},t.prototype.elementMatchedToken=function(t,e){this.tokensByElement.has(t,e)||(this.tokensByElement.add(t,e),this.delegate.elementMatchedTokenForAttribute&&this.delegate.elementMatchedTokenForAttribute(t,e,this.attributeName))},t.prototype.elementUnmatchedToken=function(t,e){this.tokensByElement.has(t,e)&&(this.tokensByElement.delete(t,e),this.delegate.elementUnmatchedTokenForAttribute&&this.delegate.elementUnmatchedTokenForAttribute(t,e,this.attributeName))},t.prototype.getTokensForElement=function(t){return this.tokensByElement.getValuesForKey(t)},t.prototype.readTokenSetForElement=function(t){for(var e=new Set,n=t.getAttribute(this.attributeName)||"",r=0,o=n.split(/\s+/);r<o.length;r++){var i=o[r];i.length&&e.add(i)}return e},t}()),k=function(){function t(t,e){this.context=t,this.delegate=e,this.tokenListObserver=new F(this.element,this.attributeName,this),this.connectedActions=new j}return Object.defineProperty(t.prototype,"scope",{get:function(){return this.context.scope},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"schema",{get:function(){return this.context.schema},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"attributeName",{get:function(){return this.schema.actionAttribute},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"identifier",{get:function(){return this.scope.identifier},enumerable:!0,configurable:!0}),t.prototype.start=function(){this.tokenListObserver.start()},t.prototype.stop=function(){this.tokenListObserver.stop()},t.prototype.elementMatchedTokenForAttribute=function(t,e,n){if(this.scope.containsElement(t)){var r=this.buildActionForElementWithDescriptorString(t,e);r&&(this.connectedActions.add(t,r),this.delegate.inlineActionConnected(r))}},t.prototype.elementUnmatchedTokenForAttribute=function(t,e,n){var r=this.getConnectedActionForElementWithDescriptorString(t,e);r&&(this.connectedActions.delete(t,r),this.delegate.inlineActionDisconnected(r))},t.prototype.getConnectedActionForElementWithDescriptorString=function(t,e){var n=this.buildActionForElementWithDescriptorString(t,e);if(n){return this.connectedActions.getValuesForKey(t).find(function(t){return t.hasSameDescriptorAs(n)})}},t.prototype.buildActionForElementWithDescriptorString=function(t,e){try{var n=O.forElementWithInlineDescriptorString(t,e);if(n.identifier==this.identifier)return new E(this.context,n,n.eventTarget)}catch(n){this.context.handleError(n,'parsing descriptor string "'+e+'"',{element:t})}},t}(),x=function(){function t(t){this.scope=t}return Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"identifier",{get:function(){return this.scope.identifier},enumerable:!0,configurable:!0}),t.prototype.get=function(t){return t=this.getFormattedKey(t),this.element.getAttribute(t)},t.prototype.set=function(t,e){return t=this.getFormattedKey(t),this.element.setAttribute(t,e),this.get(t)},t.prototype.has=function(t){return t=this.getFormattedKey(t),this.element.hasAttribute(t)},t.prototype.delete=function(t){return!!this.has(t)&&(t=this.getFormattedKey(t),this.element.removeAttribute(t),!0)},t.prototype.getFormattedKey=function(t){return"data-"+this.identifier+"-"+l(t)},t}(),B=function(){function t(t){this.scope=t}return Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"identifier",{get:function(){return this.scope.identifier},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"schema",{get:function(){return this.scope.schema},enumerable:!0,configurable:!0}),t.prototype.has=function(t){return null!=this.find(t)},t.prototype.find=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=this.getSelectorForTargetNames(t);return this.scope.findElement(n)},t.prototype.findAll=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=this.getSelectorForTargetNames(t);return this.scope.findAllElements(n)},t.prototype.getSelectorForTargetNames=function(t){var e=this;return t.map(function(t){return e.getSelectorForTargetName(t)}).join(", ")},t.prototype.getSelectorForTargetName=function(t){var e=this.identifier+"."+t;return f(this.schema.targetAttribute,e)},t}(),M=function(){function t(t,e,n){this.schema=t,this.identifier=e,this.element=n,this.targets=new B(this),this.data=new x(this)}return t.prototype.findElement=function(t){return this.findAllElements(t)[0]},t.prototype.findAllElements=function(t){var e=this.element.matches(t)?[this.element]:[],n=this.filterElements(Array.from(this.element.querySelectorAll(t)));return e.concat(n)},t.prototype.filterElements=function(t){var e=this;return t.filter(function(t){return e.containsElement(t)})},t.prototype.containsElement=function(t){return t.closest(this.controllerSelector)===this.element},Object.defineProperty(t.prototype,"controllerSelector",{get:function(){return f(this.schema.controllerAttribute,this.identifier)},enumerable:!0,configurable:!0}),t}(),C=function(){function t(t,e){this.module=t,this.scope=new M(this.schema,this.identifier,e),this.actions=new N(this),this.inlineActionObserver=new k(this,this);try{this.controller=new t.controllerConstructor(this),this.controller.initialize()}catch(t){this.handleError(t,"initializing controller")}}return t.prototype.connect=function(){this.actions.start(),this.inlineActionObserver.start();try{this.controller.connect()}catch(t){this.handleError(t,"connecting controller")}},t.prototype.disconnect=function(){try{this.controller.disconnect()}catch(t){this.handleError(t,"disconnecting controller")}this.inlineActionObserver.stop(),this.actions.stop()},Object.defineProperty(t.prototype,"application",{get:function(){return this.module.application},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"identifier",{get:function(){return this.module.identifier},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"schema",{get:function(){return this.application.schema},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"parentElement",{get:function(){return this.element.parentElement},enumerable:!0,configurable:!0}),t.prototype.inlineActionConnected=function(t){this.actions.add(t)},t.prototype.inlineActionDisconnected=function(t){this.actions.delete(t)},t.prototype.handleError=function(t,e,n){void 0===n&&(n={});var r=this,o=r.identifier,i=r.controller,c=r.element;n=Object.assign({identifier:o,controller:i,element:c},n),this.application.handleError(t,"Error "+e,n)},t}(),S=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}(),I=function(){function t(t){function e(){var n=this&&this instanceof e?this.constructor:void 0;return Reflect.construct(t,arguments,n)}return e.prototype=Object.create(t.prototype,{constructor:{value:e}}),Reflect.setPrototypeOf(e,t),e}try{return function(){var e=function(){this.a.call(this)},n=t(e);n.prototype.a=function(){},new n}(),t}catch(t){return function(t){return function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return S(e,t),e}(t)}}}(),_=function(){function t(t,e){this.application=t,this.definition=h(e),this.contextsByElement=new WeakMap,this.connectedContexts=new Set}return Object.defineProperty(t.prototype,"identifier",{get:function(){return this.definition.identifier},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"controllerConstructor",{get:function(){return this.definition.controllerConstructor},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"contexts",{get:function(){return Array.from(this.connectedContexts)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"size",{get:function(){return this.connectedContexts.size},enumerable:!0,configurable:!0}),t.prototype.connectElement=function(t){var e=this.fetchContextForElement(t);e&&!this.connectedContexts.has(e)&&(this.connectedContexts.add(e),e.connect())},t.prototype.disconnectElement=function(t){var e=this.fetchContextForElement(t);e&&this.connectedContexts.has(e)&&(this.connectedContexts.delete(e),e.disconnect())},t.prototype.getContextForElement=function(t){return this.contextsByElement.get(t)},t.prototype.fetchContextForElement=function(t){var e=this.contextsByElement.get(t);return e||(e=new C(this,t),this.contextsByElement.set(t,e)),e},t}(),K=function(){function t(t){this.application=t,this.tokenListObserver=new F(this.element,this.controllerAttribute,this),this.modulesByIdentifier=new Map}return Object.defineProperty(t.prototype,"schema",{get:function(){return this.application.schema},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"element",{get:function(){return this.application.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"controllerAttribute",{get:function(){return this.schema.controllerAttribute},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"modules",{get:function(){return Array.from(this.modulesByIdentifier.values())},enumerable:!0,configurable:!0}),t.prototype.start=function(){this.tokenListObserver.start()},t.prototype.stop=function(){this.tokenListObserver.stop()},t.prototype.loadDefinition=function(t){var e=t.identifier;this.unloadIdentifier(e);var n=new _(this.application,t);this.modulesByIdentifier.set(e,n),this.connectModule(n)},t.prototype.unloadIdentifier=function(t){var e=this.modulesByIdentifier.get(t);e&&(this.disconnectModule(e),this.modulesByIdentifier.delete(t))},t.prototype.elementMatchedTokenForAttribute=function(t,e,n){this.connectModuleForIdentifierToElement(e,t)},t.prototype.elementUnmatchedTokenForAttribute=function(t,e,n){this.disconnectModuleForIdentifierFromElement(e,t)},Object.defineProperty(t.prototype,"contexts",{get:function(){return this.modules.reduce(function(t,e){return t.concat(Array.from(e.contexts))},[])},enumerable:!0,configurable:!0}),t.prototype.getContextForElementAndIdentifier=function(t,e){var n=this.modulesByIdentifier.get(e);if(n)return n.getContextForElement(t)},t.prototype.connectModule=function(t){for(var e=this.tokenListObserver.getElementsMatchingToken(t.identifier),n=0,r=e;n<r.length;n++){var o=r[n];t.connectElement(o)}},t.prototype.disconnectModule=function(t){for(var e=t.contexts,n=0,r=e;n<r.length;n++){var o=r[n].element;t.disconnectElement(o)}},t.prototype.connectModuleForIdentifierToElement=function(t,e){var n=this.modulesByIdentifier.get(t);n&&n.connectElement(e)},t.prototype.disconnectModuleForIdentifierFromElement=function(t,e){var n=this.modulesByIdentifier.get(t);n&&n.disconnectElement(e)},t}(),D={controllerAttribute:"data-controller",actionAttribute:"data-action",targetAttribute:"data-target"},V=function(){function t(t,e){void 0===t&&(t=document.documentElement),void 0===e&&(e=D),this.element=t,this.schema=e,this.router=new K(this)}return t.start=function(e,n){var r=new t(e,n);return r.start(),r},t.prototype.start=function(){this.router.start()},t.prototype.stop=function(){this.router.stop()},t.prototype.register=function(t,e){this.load({identifier:t,controllerConstructor:e})},t.prototype.load=function(t){for(var e=this,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];(Array.isArray(t)?t:[t].concat(n)).forEach(function(t){return e.router.loadDefinition(t)})},t.prototype.unload=function(t){for(var e=this,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];(Array.isArray(t)?t:[t].concat(n)).forEach(function(t){return e.router.unloadIdentifier(t)})},Object.defineProperty(t.prototype,"controllers",{get:function(){return this.router.contexts.map(function(t){return t.controller})},enumerable:!0,configurable:!0}),t.prototype.getControllerForElementAndIdentifier=function(t,e){var n=this.router.getContextForElementAndIdentifier(t,e);return n?n.controller:null},t.prototype.handleError=function(t,e,n){console.error("%s\n\n%o\n\n%o",e,t,n)},t}(),L=function(){function t(t){this.context=t}return t.bless=function(){d(this)},Object.defineProperty(t.prototype,"application",{get:function(){return this.context.application},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scope",{get:function(){return this.context.scope},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"element",{get:function(){return this.scope.element},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"identifier",{get:function(){return this.scope.identifier},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"targets",{get:function(){return this.scope.targets},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"data",{get:function(){return this.scope.data},enumerable:!0,configurable:!0}),t.prototype.initialize=function(){},t.prototype.connect=function(){},t.prototype.disconnect=function(){},t.targets=[],t}();n.d(e,"Action",function(){return E}),n.d(e,"ActionDescriptor",function(){return O}),n.d(e,"Application",function(){return V}),n.d(e,"Context",function(){return C}),n.d(e,"Controller",function(){return L}),n.d(e,"defaultSchema",function(){return D})}])});
5
+ (function(global, factory) {
6
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : factory(global.Stimulus = {});
7
+ })(this, function(exports) {
8
+ "use strict";
9
+ var EventListener = function() {
10
+ function EventListener(eventTarget, eventName) {
11
+ this.eventTarget = eventTarget;
12
+ this.eventName = eventName;
13
+ this.unorderedBindings = new Set();
14
+ }
15
+ EventListener.prototype.connect = function() {
16
+ this.eventTarget.addEventListener(this.eventName, this, false);
17
+ };
18
+ EventListener.prototype.disconnect = function() {
19
+ this.eventTarget.removeEventListener(this.eventName, this, false);
20
+ };
21
+ EventListener.prototype.bindingConnected = function(binding) {
22
+ this.unorderedBindings.add(binding);
23
+ };
24
+ EventListener.prototype.bindingDisconnected = function(binding) {
25
+ this.unorderedBindings.delete(binding);
26
+ };
27
+ EventListener.prototype.handleEvent = function(event) {
28
+ var extendedEvent = extendEvent(event);
29
+ for (var _i = 0, _a = this.bindings; _i < _a.length; _i++) {
30
+ var binding = _a[_i];
31
+ if (extendedEvent.immediatePropagationStopped) {
32
+ break;
33
+ } else {
34
+ binding.handleEvent(extendedEvent);
35
+ }
36
+ }
37
+ };
38
+ Object.defineProperty(EventListener.prototype, "bindings", {
39
+ get: function() {
40
+ return Array.from(this.unorderedBindings).sort(function(left, right) {
41
+ var leftIndex = left.index, rightIndex = right.index;
42
+ return leftIndex < rightIndex ? -1 : leftIndex > rightIndex ? 1 : 0;
43
+ });
44
+ },
45
+ enumerable: true,
46
+ configurable: true
47
+ });
48
+ return EventListener;
49
+ }();
50
+ function extendEvent(event) {
51
+ if ("immediatePropagationStopped" in event) {
52
+ return event;
53
+ } else {
54
+ var stopImmediatePropagation_1 = event.stopImmediatePropagation;
55
+ return Object.assign(event, {
56
+ immediatePropagationStopped: false,
57
+ stopImmediatePropagation: function() {
58
+ this.immediatePropagationStopped = true;
59
+ stopImmediatePropagation_1.call(this);
60
+ }
61
+ });
62
+ }
63
+ }
64
+ var Dispatcher = function() {
65
+ function Dispatcher(application) {
66
+ this.application = application;
67
+ this.eventListenerMaps = new Map();
68
+ this.started = false;
69
+ }
70
+ Dispatcher.prototype.start = function() {
71
+ if (!this.started) {
72
+ this.started = true;
73
+ this.eventListeners.forEach(function(eventListener) {
74
+ return eventListener.connect();
75
+ });
76
+ }
77
+ };
78
+ Dispatcher.prototype.stop = function() {
79
+ if (this.started) {
80
+ this.started = false;
81
+ this.eventListeners.forEach(function(eventListener) {
82
+ return eventListener.disconnect();
83
+ });
84
+ }
85
+ };
86
+ Object.defineProperty(Dispatcher.prototype, "eventListeners", {
87
+ get: function() {
88
+ return Array.from(this.eventListenerMaps.values()).reduce(function(listeners, map) {
89
+ return listeners.concat(Array.from(map.values()));
90
+ }, []);
91
+ },
92
+ enumerable: true,
93
+ configurable: true
94
+ });
95
+ Dispatcher.prototype.bindingConnected = function(binding) {
96
+ this.fetchEventListenerForBinding(binding).bindingConnected(binding);
97
+ };
98
+ Dispatcher.prototype.bindingDisconnected = function(binding) {
99
+ this.fetchEventListenerForBinding(binding).bindingDisconnected(binding);
100
+ };
101
+ Dispatcher.prototype.handleError = function(error, message, detail) {
102
+ if (detail === void 0) {
103
+ detail = {};
104
+ }
105
+ this.application.handleError(error, "Error " + message, detail);
106
+ };
107
+ Dispatcher.prototype.fetchEventListenerForBinding = function(binding) {
108
+ var eventTarget = binding.eventTarget, eventName = binding.eventName;
109
+ return this.fetchEventListener(eventTarget, eventName);
110
+ };
111
+ Dispatcher.prototype.fetchEventListener = function(eventTarget, eventName) {
112
+ var eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget);
113
+ var eventListener = eventListenerMap.get(eventName);
114
+ if (!eventListener) {
115
+ eventListener = this.createEventListener(eventTarget, eventName);
116
+ eventListenerMap.set(eventName, eventListener);
117
+ }
118
+ return eventListener;
119
+ };
120
+ Dispatcher.prototype.createEventListener = function(eventTarget, eventName) {
121
+ var eventListener = new EventListener(eventTarget, eventName);
122
+ if (this.started) {
123
+ eventListener.connect();
124
+ }
125
+ return eventListener;
126
+ };
127
+ Dispatcher.prototype.fetchEventListenerMapForEventTarget = function(eventTarget) {
128
+ var eventListenerMap = this.eventListenerMaps.get(eventTarget);
129
+ if (!eventListenerMap) {
130
+ eventListenerMap = new Map();
131
+ this.eventListenerMaps.set(eventTarget, eventListenerMap);
132
+ }
133
+ return eventListenerMap;
134
+ };
135
+ return Dispatcher;
136
+ }();
137
+ var descriptorPattern = /^((.+?)(@(window|document))?->)?(.+?)(#(.+))?$/;
138
+ function parseDescriptorString(descriptorString) {
139
+ var source = descriptorString.trim();
140
+ var matches = source.match(descriptorPattern) || [];
141
+ return {
142
+ eventTarget: parseEventTarget(matches[4]),
143
+ eventName: matches[2],
144
+ identifier: matches[5],
145
+ methodName: matches[7]
146
+ };
147
+ }
148
+ function parseEventTarget(eventTargetName) {
149
+ if (eventTargetName == "window") {
150
+ return window;
151
+ } else if (eventTargetName == "document") {
152
+ return document;
153
+ }
154
+ }
155
+ function stringifyEventTarget(eventTarget) {
156
+ if (eventTarget == window) {
157
+ return "window";
158
+ } else if (eventTarget == document) {
159
+ return "document";
160
+ }
161
+ }
162
+ var Action = function() {
163
+ function Action(element, index, descriptor) {
164
+ this.element = element;
165
+ this.index = index;
166
+ this.eventTarget = descriptor.eventTarget || element;
167
+ this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name");
168
+ this.identifier = descriptor.identifier || error("missing identifier");
169
+ this.methodName = descriptor.methodName || error("missing method name");
170
+ }
171
+ Action.forToken = function(token) {
172
+ return new this(token.element, token.index, parseDescriptorString(token.content));
173
+ };
174
+ Action.prototype.toString = function() {
175
+ var eventNameSuffix = this.eventTargetName ? "@" + this.eventTargetName : "";
176
+ return "" + this.eventName + eventNameSuffix + "->" + this.identifier + "#" + this.methodName;
177
+ };
178
+ Object.defineProperty(Action.prototype, "eventTargetName", {
179
+ get: function() {
180
+ return stringifyEventTarget(this.eventTarget);
181
+ },
182
+ enumerable: true,
183
+ configurable: true
184
+ });
185
+ return Action;
186
+ }();
187
+ var defaultEventNames = {
188
+ a: function(e) {
189
+ return "click";
190
+ },
191
+ button: function(e) {
192
+ return "click";
193
+ },
194
+ form: function(e) {
195
+ return "submit";
196
+ },
197
+ input: function(e) {
198
+ return e.getAttribute("type") == "submit" ? "click" : "change";
199
+ },
200
+ select: function(e) {
201
+ return "change";
202
+ },
203
+ textarea: function(e) {
204
+ return "change";
205
+ }
206
+ };
207
+ function getDefaultEventNameForElement(element) {
208
+ var tagName = element.tagName.toLowerCase();
209
+ if (tagName in defaultEventNames) {
210
+ return defaultEventNames[tagName](element);
211
+ }
212
+ }
213
+ function error(message) {
214
+ throw new Error(message);
215
+ }
216
+ var Binding = function() {
217
+ function Binding(context, action) {
218
+ this.context = context;
219
+ this.action = action;
220
+ }
221
+ Object.defineProperty(Binding.prototype, "index", {
222
+ get: function() {
223
+ return this.action.index;
224
+ },
225
+ enumerable: true,
226
+ configurable: true
227
+ });
228
+ Object.defineProperty(Binding.prototype, "eventTarget", {
229
+ get: function() {
230
+ return this.action.eventTarget;
231
+ },
232
+ enumerable: true,
233
+ configurable: true
234
+ });
235
+ Object.defineProperty(Binding.prototype, "identifier", {
236
+ get: function() {
237
+ return this.context.identifier;
238
+ },
239
+ enumerable: true,
240
+ configurable: true
241
+ });
242
+ Binding.prototype.handleEvent = function(event) {
243
+ if (this.willBeInvokedByEvent(event)) {
244
+ this.invokeWithEvent(event);
245
+ }
246
+ };
247
+ Object.defineProperty(Binding.prototype, "eventName", {
248
+ get: function() {
249
+ return this.action.eventName;
250
+ },
251
+ enumerable: true,
252
+ configurable: true
253
+ });
254
+ Object.defineProperty(Binding.prototype, "method", {
255
+ get: function() {
256
+ var method = this.controller[this.methodName];
257
+ if (typeof method == "function") {
258
+ return method;
259
+ }
260
+ throw new Error('Action "' + this.action + '" references undefined method "' + this.methodName + '"');
261
+ },
262
+ enumerable: true,
263
+ configurable: true
264
+ });
265
+ Binding.prototype.invokeWithEvent = function(event) {
266
+ try {
267
+ this.method.call(this.controller, event);
268
+ } catch (error) {
269
+ var _a = this, identifier = _a.identifier, controller = _a.controller, element = _a.element, index = _a.index;
270
+ var detail = {
271
+ identifier: identifier,
272
+ controller: controller,
273
+ element: element,
274
+ index: index,
275
+ event: event
276
+ };
277
+ this.context.handleError(error, 'invoking action "' + this.action + '"', detail);
278
+ }
279
+ };
280
+ Binding.prototype.willBeInvokedByEvent = function(event) {
281
+ var eventTarget = event.target;
282
+ if (this.element === eventTarget) {
283
+ return true;
284
+ } else if (eventTarget instanceof Element && this.element.contains(eventTarget)) {
285
+ return this.scope.containsElement(eventTarget);
286
+ } else {
287
+ return true;
288
+ }
289
+ };
290
+ Object.defineProperty(Binding.prototype, "controller", {
291
+ get: function() {
292
+ return this.context.controller;
293
+ },
294
+ enumerable: true,
295
+ configurable: true
296
+ });
297
+ Object.defineProperty(Binding.prototype, "methodName", {
298
+ get: function() {
299
+ return this.action.methodName;
300
+ },
301
+ enumerable: true,
302
+ configurable: true
303
+ });
304
+ Object.defineProperty(Binding.prototype, "element", {
305
+ get: function() {
306
+ return this.scope.element;
307
+ },
308
+ enumerable: true,
309
+ configurable: true
310
+ });
311
+ Object.defineProperty(Binding.prototype, "scope", {
312
+ get: function() {
313
+ return this.context.scope;
314
+ },
315
+ enumerable: true,
316
+ configurable: true
317
+ });
318
+ return Binding;
319
+ }();
320
+ var ElementObserver = function() {
321
+ function ElementObserver(element, delegate) {
322
+ var _this = this;
323
+ this.element = element;
324
+ this.started = false;
325
+ this.delegate = delegate;
326
+ this.elements = new Set();
327
+ this.mutationObserver = new MutationObserver(function(mutations) {
328
+ return _this.processMutations(mutations);
329
+ });
330
+ }
331
+ ElementObserver.prototype.start = function() {
332
+ if (!this.started) {
333
+ this.started = true;
334
+ this.mutationObserver.observe(this.element, {
335
+ attributes: true,
336
+ childList: true,
337
+ subtree: true
338
+ });
339
+ this.refresh();
340
+ }
341
+ };
342
+ ElementObserver.prototype.stop = function() {
343
+ if (this.started) {
344
+ this.mutationObserver.takeRecords();
345
+ this.mutationObserver.disconnect();
346
+ this.started = false;
347
+ }
348
+ };
349
+ ElementObserver.prototype.refresh = function() {
350
+ if (this.started) {
351
+ var matches = new Set(this.matchElementsInTree());
352
+ for (var _i = 0, _a = Array.from(this.elements); _i < _a.length; _i++) {
353
+ var element = _a[_i];
354
+ if (!matches.has(element)) {
355
+ this.removeElement(element);
356
+ }
357
+ }
358
+ for (var _b = 0, _c = Array.from(matches); _b < _c.length; _b++) {
359
+ var element = _c[_b];
360
+ this.addElement(element);
361
+ }
362
+ }
363
+ };
364
+ ElementObserver.prototype.processMutations = function(mutations) {
365
+ if (this.started) {
366
+ for (var _i = 0, mutations_1 = mutations; _i < mutations_1.length; _i++) {
367
+ var mutation = mutations_1[_i];
368
+ this.processMutation(mutation);
369
+ }
370
+ }
371
+ };
372
+ ElementObserver.prototype.processMutation = function(mutation) {
373
+ if (mutation.type == "attributes") {
374
+ this.processAttributeChange(mutation.target, mutation.attributeName);
375
+ } else if (mutation.type == "childList") {
376
+ this.processRemovedNodes(mutation.removedNodes);
377
+ this.processAddedNodes(mutation.addedNodes);
378
+ }
379
+ };
380
+ ElementObserver.prototype.processAttributeChange = function(node, attributeName) {
381
+ var element = node;
382
+ if (this.elements.has(element)) {
383
+ if (this.delegate.elementAttributeChanged && this.matchElement(element)) {
384
+ this.delegate.elementAttributeChanged(element, attributeName);
385
+ } else {
386
+ this.removeElement(element);
387
+ }
388
+ } else if (this.matchElement(element)) {
389
+ this.addElement(element);
390
+ }
391
+ };
392
+ ElementObserver.prototype.processRemovedNodes = function(nodes) {
393
+ for (var _i = 0, _a = Array.from(nodes); _i < _a.length; _i++) {
394
+ var node = _a[_i];
395
+ var element = this.elementFromNode(node);
396
+ if (element) {
397
+ this.processTree(element, this.removeElement);
398
+ }
399
+ }
400
+ };
401
+ ElementObserver.prototype.processAddedNodes = function(nodes) {
402
+ for (var _i = 0, _a = Array.from(nodes); _i < _a.length; _i++) {
403
+ var node = _a[_i];
404
+ var element = this.elementFromNode(node);
405
+ if (element && this.elementIsActive(element)) {
406
+ this.processTree(element, this.addElement);
407
+ }
408
+ }
409
+ };
410
+ ElementObserver.prototype.matchElement = function(element) {
411
+ return this.delegate.matchElement(element);
412
+ };
413
+ ElementObserver.prototype.matchElementsInTree = function(tree) {
414
+ if (tree === void 0) {
415
+ tree = this.element;
416
+ }
417
+ return this.delegate.matchElementsInTree(tree);
418
+ };
419
+ ElementObserver.prototype.processTree = function(tree, processor) {
420
+ for (var _i = 0, _a = this.matchElementsInTree(tree); _i < _a.length; _i++) {
421
+ var element = _a[_i];
422
+ processor.call(this, element);
423
+ }
424
+ };
425
+ ElementObserver.prototype.elementFromNode = function(node) {
426
+ if (node.nodeType == Node.ELEMENT_NODE) {
427
+ return node;
428
+ }
429
+ };
430
+ ElementObserver.prototype.elementIsActive = function(element) {
431
+ if (element.isConnected != this.element.isConnected) {
432
+ return false;
433
+ } else {
434
+ return this.element.contains(element);
435
+ }
436
+ };
437
+ ElementObserver.prototype.addElement = function(element) {
438
+ if (!this.elements.has(element)) {
439
+ if (this.elementIsActive(element)) {
440
+ this.elements.add(element);
441
+ if (this.delegate.elementMatched) {
442
+ this.delegate.elementMatched(element);
443
+ }
444
+ }
445
+ }
446
+ };
447
+ ElementObserver.prototype.removeElement = function(element) {
448
+ if (this.elements.has(element)) {
449
+ this.elements.delete(element);
450
+ if (this.delegate.elementUnmatched) {
451
+ this.delegate.elementUnmatched(element);
452
+ }
453
+ }
454
+ };
455
+ return ElementObserver;
456
+ }();
457
+ var AttributeObserver = function() {
458
+ function AttributeObserver(element, attributeName, delegate) {
459
+ this.attributeName = attributeName;
460
+ this.delegate = delegate;
461
+ this.elementObserver = new ElementObserver(element, this);
462
+ }
463
+ Object.defineProperty(AttributeObserver.prototype, "element", {
464
+ get: function() {
465
+ return this.elementObserver.element;
466
+ },
467
+ enumerable: true,
468
+ configurable: true
469
+ });
470
+ Object.defineProperty(AttributeObserver.prototype, "selector", {
471
+ get: function() {
472
+ return "[" + this.attributeName + "]";
473
+ },
474
+ enumerable: true,
475
+ configurable: true
476
+ });
477
+ AttributeObserver.prototype.start = function() {
478
+ this.elementObserver.start();
479
+ };
480
+ AttributeObserver.prototype.stop = function() {
481
+ this.elementObserver.stop();
482
+ };
483
+ AttributeObserver.prototype.refresh = function() {
484
+ this.elementObserver.refresh();
485
+ };
486
+ Object.defineProperty(AttributeObserver.prototype, "started", {
487
+ get: function() {
488
+ return this.elementObserver.started;
489
+ },
490
+ enumerable: true,
491
+ configurable: true
492
+ });
493
+ AttributeObserver.prototype.matchElement = function(element) {
494
+ return element.hasAttribute(this.attributeName);
495
+ };
496
+ AttributeObserver.prototype.matchElementsInTree = function(tree) {
497
+ var match = this.matchElement(tree) ? [ tree ] : [];
498
+ var matches = Array.from(tree.querySelectorAll(this.selector));
499
+ return match.concat(matches);
500
+ };
501
+ AttributeObserver.prototype.elementMatched = function(element) {
502
+ if (this.delegate.elementMatchedAttribute) {
503
+ this.delegate.elementMatchedAttribute(element, this.attributeName);
504
+ }
505
+ };
506
+ AttributeObserver.prototype.elementUnmatched = function(element) {
507
+ if (this.delegate.elementUnmatchedAttribute) {
508
+ this.delegate.elementUnmatchedAttribute(element, this.attributeName);
509
+ }
510
+ };
511
+ AttributeObserver.prototype.elementAttributeChanged = function(element, attributeName) {
512
+ if (this.delegate.elementAttributeValueChanged && this.attributeName == attributeName) {
513
+ this.delegate.elementAttributeValueChanged(element, attributeName);
514
+ }
515
+ };
516
+ return AttributeObserver;
517
+ }();
518
+ function add(map, key, value) {
519
+ fetch(map, key).add(value);
520
+ }
521
+ function del(map, key, value) {
522
+ fetch(map, key).delete(value);
523
+ prune(map, key);
524
+ }
525
+ function fetch(map, key) {
526
+ var values = map.get(key);
527
+ if (!values) {
528
+ values = new Set();
529
+ map.set(key, values);
530
+ }
531
+ return values;
532
+ }
533
+ function prune(map, key) {
534
+ var values = map.get(key);
535
+ if (values != null && values.size == 0) {
536
+ map.delete(key);
537
+ }
538
+ }
539
+ var Multimap = function() {
540
+ function Multimap() {
541
+ this.valuesByKey = new Map();
542
+ }
543
+ Object.defineProperty(Multimap.prototype, "values", {
544
+ get: function() {
545
+ var sets = Array.from(this.valuesByKey.values());
546
+ return sets.reduce(function(values, set) {
547
+ return values.concat(Array.from(set));
548
+ }, []);
549
+ },
550
+ enumerable: true,
551
+ configurable: true
552
+ });
553
+ Object.defineProperty(Multimap.prototype, "size", {
554
+ get: function() {
555
+ var sets = Array.from(this.valuesByKey.values());
556
+ return sets.reduce(function(size, set) {
557
+ return size + set.size;
558
+ }, 0);
559
+ },
560
+ enumerable: true,
561
+ configurable: true
562
+ });
563
+ Multimap.prototype.add = function(key, value) {
564
+ add(this.valuesByKey, key, value);
565
+ };
566
+ Multimap.prototype.delete = function(key, value) {
567
+ del(this.valuesByKey, key, value);
568
+ };
569
+ Multimap.prototype.has = function(key, value) {
570
+ var values = this.valuesByKey.get(key);
571
+ return values != null && values.has(value);
572
+ };
573
+ Multimap.prototype.hasKey = function(key) {
574
+ return this.valuesByKey.has(key);
575
+ };
576
+ Multimap.prototype.hasValue = function(value) {
577
+ var sets = Array.from(this.valuesByKey.values());
578
+ return sets.some(function(set) {
579
+ return set.has(value);
580
+ });
581
+ };
582
+ Multimap.prototype.getValuesForKey = function(key) {
583
+ var values = this.valuesByKey.get(key);
584
+ return values ? Array.from(values) : [];
585
+ };
586
+ Multimap.prototype.getKeysForValue = function(value) {
587
+ return Array.from(this.valuesByKey).filter(function(_a) {
588
+ var key = _a[0], values = _a[1];
589
+ return values.has(value);
590
+ }).map(function(_a) {
591
+ var key = _a[0], values = _a[1];
592
+ return key;
593
+ });
594
+ };
595
+ return Multimap;
596
+ }();
597
+ var __extends = undefined && undefined.__extends || function() {
598
+ var extendStatics = Object.setPrototypeOf || {
599
+ __proto__: []
600
+ } instanceof Array && function(d, b) {
601
+ d.__proto__ = b;
602
+ } || function(d, b) {
603
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
604
+ };
605
+ return function(d, b) {
606
+ extendStatics(d, b);
607
+ function __() {
608
+ this.constructor = d;
609
+ }
610
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
611
+ };
612
+ }();
613
+ var IndexedMultimap = function(_super) {
614
+ __extends(IndexedMultimap, _super);
615
+ function IndexedMultimap() {
616
+ var _this = _super.call(this) || this;
617
+ _this.keysByValue = new Map();
618
+ return _this;
619
+ }
620
+ Object.defineProperty(IndexedMultimap.prototype, "values", {
621
+ get: function() {
622
+ return Array.from(this.keysByValue.keys());
623
+ },
624
+ enumerable: true,
625
+ configurable: true
626
+ });
627
+ IndexedMultimap.prototype.add = function(key, value) {
628
+ _super.prototype.add.call(this, key, value);
629
+ add(this.keysByValue, value, key);
630
+ };
631
+ IndexedMultimap.prototype.delete = function(key, value) {
632
+ _super.prototype.delete.call(this, key, value);
633
+ del(this.keysByValue, value, key);
634
+ };
635
+ IndexedMultimap.prototype.hasValue = function(value) {
636
+ return this.keysByValue.has(value);
637
+ };
638
+ IndexedMultimap.prototype.getKeysForValue = function(value) {
639
+ var set = this.keysByValue.get(value);
640
+ return set ? Array.from(set) : [];
641
+ };
642
+ return IndexedMultimap;
643
+ }(Multimap);
644
+ var TokenListObserver = function() {
645
+ function TokenListObserver(element, attributeName, delegate) {
646
+ this.attributeObserver = new AttributeObserver(element, attributeName, this);
647
+ this.delegate = delegate;
648
+ this.tokensByElement = new Multimap();
649
+ }
650
+ Object.defineProperty(TokenListObserver.prototype, "started", {
651
+ get: function() {
652
+ return this.attributeObserver.started;
653
+ },
654
+ enumerable: true,
655
+ configurable: true
656
+ });
657
+ TokenListObserver.prototype.start = function() {
658
+ this.attributeObserver.start();
659
+ };
660
+ TokenListObserver.prototype.stop = function() {
661
+ this.attributeObserver.stop();
662
+ };
663
+ TokenListObserver.prototype.refresh = function() {
664
+ this.attributeObserver.refresh();
665
+ };
666
+ Object.defineProperty(TokenListObserver.prototype, "element", {
667
+ get: function() {
668
+ return this.attributeObserver.element;
669
+ },
670
+ enumerable: true,
671
+ configurable: true
672
+ });
673
+ Object.defineProperty(TokenListObserver.prototype, "attributeName", {
674
+ get: function() {
675
+ return this.attributeObserver.attributeName;
676
+ },
677
+ enumerable: true,
678
+ configurable: true
679
+ });
680
+ TokenListObserver.prototype.elementMatchedAttribute = function(element) {
681
+ this.tokensMatched(this.readTokensForElement(element));
682
+ };
683
+ TokenListObserver.prototype.elementAttributeValueChanged = function(element) {
684
+ var _a = this.refreshTokensForElement(element), unmatchedTokens = _a[0], matchedTokens = _a[1];
685
+ this.tokensUnmatched(unmatchedTokens);
686
+ this.tokensMatched(matchedTokens);
687
+ };
688
+ TokenListObserver.prototype.elementUnmatchedAttribute = function(element) {
689
+ this.tokensUnmatched(this.tokensByElement.getValuesForKey(element));
690
+ };
691
+ TokenListObserver.prototype.tokensMatched = function(tokens) {
692
+ var _this = this;
693
+ tokens.forEach(function(token) {
694
+ return _this.tokenMatched(token);
695
+ });
696
+ };
697
+ TokenListObserver.prototype.tokensUnmatched = function(tokens) {
698
+ var _this = this;
699
+ tokens.forEach(function(token) {
700
+ return _this.tokenUnmatched(token);
701
+ });
702
+ };
703
+ TokenListObserver.prototype.tokenMatched = function(token) {
704
+ this.delegate.tokenMatched(token);
705
+ this.tokensByElement.add(token.element, token);
706
+ };
707
+ TokenListObserver.prototype.tokenUnmatched = function(token) {
708
+ this.delegate.tokenUnmatched(token);
709
+ this.tokensByElement.delete(token.element, token);
710
+ };
711
+ TokenListObserver.prototype.refreshTokensForElement = function(element) {
712
+ var previousTokens = this.tokensByElement.getValuesForKey(element);
713
+ var currentTokens = this.readTokensForElement(element);
714
+ var firstDifferingIndex = zip(previousTokens, currentTokens).findIndex(function(_a) {
715
+ var previousToken = _a[0], currentToken = _a[1];
716
+ return !tokensAreEqual(previousToken, currentToken);
717
+ });
718
+ if (firstDifferingIndex == -1) {
719
+ return [ [], [] ];
720
+ } else {
721
+ return [ previousTokens.slice(firstDifferingIndex), currentTokens.slice(firstDifferingIndex) ];
722
+ }
723
+ };
724
+ TokenListObserver.prototype.readTokensForElement = function(element) {
725
+ var attributeName = this.attributeName;
726
+ var tokenString = element.getAttribute(attributeName) || "";
727
+ return parseTokenString(tokenString, element, attributeName);
728
+ };
729
+ return TokenListObserver;
730
+ }();
731
+ function parseTokenString(tokenString, element, attributeName) {
732
+ return tokenString.trim().split(/\s+/).filter(function(content) {
733
+ return content.length;
734
+ }).map(function(content, index) {
735
+ return {
736
+ element: element,
737
+ attributeName: attributeName,
738
+ content: content,
739
+ index: index
740
+ };
741
+ });
742
+ }
743
+ function zip(left, right) {
744
+ var length = Math.max(left.length, right.length);
745
+ return Array.from({
746
+ length: length
747
+ }, function(_, index) {
748
+ return [ left[index], right[index] ];
749
+ });
750
+ }
751
+ function tokensAreEqual(left, right) {
752
+ return left && right && left.index == right.index && left.content == right.content;
753
+ }
754
+ var ValueListObserver = function() {
755
+ function ValueListObserver(element, attributeName, delegate) {
756
+ this.tokenListObserver = new TokenListObserver(element, attributeName, this);
757
+ this.delegate = delegate;
758
+ this.parseResultsByToken = new WeakMap();
759
+ this.valuesByTokenByElement = new WeakMap();
760
+ }
761
+ Object.defineProperty(ValueListObserver.prototype, "started", {
762
+ get: function() {
763
+ return this.tokenListObserver.started;
764
+ },
765
+ enumerable: true,
766
+ configurable: true
767
+ });
768
+ ValueListObserver.prototype.start = function() {
769
+ this.tokenListObserver.start();
770
+ };
771
+ ValueListObserver.prototype.stop = function() {
772
+ this.tokenListObserver.stop();
773
+ };
774
+ ValueListObserver.prototype.refresh = function() {
775
+ this.tokenListObserver.refresh();
776
+ };
777
+ Object.defineProperty(ValueListObserver.prototype, "element", {
778
+ get: function() {
779
+ return this.tokenListObserver.element;
780
+ },
781
+ enumerable: true,
782
+ configurable: true
783
+ });
784
+ Object.defineProperty(ValueListObserver.prototype, "attributeName", {
785
+ get: function() {
786
+ return this.tokenListObserver.attributeName;
787
+ },
788
+ enumerable: true,
789
+ configurable: true
790
+ });
791
+ ValueListObserver.prototype.tokenMatched = function(token) {
792
+ var element = token.element;
793
+ var value = this.fetchParseResultForToken(token).value;
794
+ if (value) {
795
+ this.fetchValuesByTokenForElement(element).set(token, value);
796
+ this.delegate.elementMatchedValue(element, value);
797
+ }
798
+ };
799
+ ValueListObserver.prototype.tokenUnmatched = function(token) {
800
+ var element = token.element;
801
+ var value = this.fetchParseResultForToken(token).value;
802
+ if (value) {
803
+ this.fetchValuesByTokenForElement(element).delete(token);
804
+ this.delegate.elementUnmatchedValue(element, value);
805
+ }
806
+ };
807
+ ValueListObserver.prototype.fetchParseResultForToken = function(token) {
808
+ var parseResult = this.parseResultsByToken.get(token);
809
+ if (!parseResult) {
810
+ parseResult = this.parseToken(token);
811
+ this.parseResultsByToken.set(token, parseResult);
812
+ }
813
+ return parseResult;
814
+ };
815
+ ValueListObserver.prototype.fetchValuesByTokenForElement = function(element) {
816
+ var valuesByToken = this.valuesByTokenByElement.get(element);
817
+ if (!valuesByToken) {
818
+ valuesByToken = new Map();
819
+ this.valuesByTokenByElement.set(element, valuesByToken);
820
+ }
821
+ return valuesByToken;
822
+ };
823
+ ValueListObserver.prototype.parseToken = function(token) {
824
+ try {
825
+ var value = this.delegate.parseValueForToken(token);
826
+ return {
827
+ value: value
828
+ };
829
+ } catch (error) {
830
+ return {
831
+ error: error
832
+ };
833
+ }
834
+ };
835
+ return ValueListObserver;
836
+ }();
837
+ var BindingObserver = function() {
838
+ function BindingObserver(context, delegate) {
839
+ this.context = context;
840
+ this.delegate = delegate;
841
+ this.bindingsByAction = new Map();
842
+ }
843
+ BindingObserver.prototype.start = function() {
844
+ if (!this.valueListObserver) {
845
+ this.valueListObserver = new ValueListObserver(this.element, this.actionAttribute, this);
846
+ this.valueListObserver.start();
847
+ }
848
+ };
849
+ BindingObserver.prototype.stop = function() {
850
+ if (this.valueListObserver) {
851
+ this.valueListObserver.stop();
852
+ delete this.valueListObserver;
853
+ this.disconnectAllActions();
854
+ }
855
+ };
856
+ Object.defineProperty(BindingObserver.prototype, "element", {
857
+ get: function() {
858
+ return this.context.element;
859
+ },
860
+ enumerable: true,
861
+ configurable: true
862
+ });
863
+ Object.defineProperty(BindingObserver.prototype, "identifier", {
864
+ get: function() {
865
+ return this.context.identifier;
866
+ },
867
+ enumerable: true,
868
+ configurable: true
869
+ });
870
+ Object.defineProperty(BindingObserver.prototype, "actionAttribute", {
871
+ get: function() {
872
+ return this.schema.actionAttribute;
873
+ },
874
+ enumerable: true,
875
+ configurable: true
876
+ });
877
+ Object.defineProperty(BindingObserver.prototype, "schema", {
878
+ get: function() {
879
+ return this.context.schema;
880
+ },
881
+ enumerable: true,
882
+ configurable: true
883
+ });
884
+ Object.defineProperty(BindingObserver.prototype, "bindings", {
885
+ get: function() {
886
+ return Array.from(this.bindingsByAction.values());
887
+ },
888
+ enumerable: true,
889
+ configurable: true
890
+ });
891
+ BindingObserver.prototype.connectAction = function(action) {
892
+ var binding = new Binding(this.context, action);
893
+ this.bindingsByAction.set(action, binding);
894
+ this.delegate.bindingConnected(binding);
895
+ };
896
+ BindingObserver.prototype.disconnectAction = function(action) {
897
+ var binding = this.bindingsByAction.get(action);
898
+ if (binding) {
899
+ this.bindingsByAction.delete(action);
900
+ this.delegate.bindingDisconnected(binding);
901
+ }
902
+ };
903
+ BindingObserver.prototype.disconnectAllActions = function() {
904
+ var _this = this;
905
+ this.bindings.forEach(function(binding) {
906
+ return _this.delegate.bindingDisconnected(binding);
907
+ });
908
+ this.bindingsByAction.clear();
909
+ };
910
+ BindingObserver.prototype.parseValueForToken = function(token) {
911
+ var action = Action.forToken(token);
912
+ if (action.identifier == this.identifier) {
913
+ return action;
914
+ }
915
+ };
916
+ BindingObserver.prototype.elementMatchedValue = function(element, action) {
917
+ this.connectAction(action);
918
+ };
919
+ BindingObserver.prototype.elementUnmatchedValue = function(element, action) {
920
+ this.disconnectAction(action);
921
+ };
922
+ return BindingObserver;
923
+ }();
924
+ var Context = function() {
925
+ function Context(module, scope) {
926
+ this.module = module;
927
+ this.scope = scope;
928
+ this.controller = new module.controllerConstructor(this);
929
+ this.bindingObserver = new BindingObserver(this, this.dispatcher);
930
+ try {
931
+ this.controller.initialize();
932
+ } catch (error) {
933
+ this.handleError(error, "initializing controller");
934
+ }
935
+ }
936
+ Context.prototype.connect = function() {
937
+ this.bindingObserver.start();
938
+ try {
939
+ this.controller.connect();
940
+ } catch (error) {
941
+ this.handleError(error, "connecting controller");
942
+ }
943
+ };
944
+ Context.prototype.disconnect = function() {
945
+ try {
946
+ this.controller.disconnect();
947
+ } catch (error) {
948
+ this.handleError(error, "disconnecting controller");
949
+ }
950
+ this.bindingObserver.stop();
951
+ };
952
+ Object.defineProperty(Context.prototype, "application", {
953
+ get: function() {
954
+ return this.module.application;
955
+ },
956
+ enumerable: true,
957
+ configurable: true
958
+ });
959
+ Object.defineProperty(Context.prototype, "identifier", {
960
+ get: function() {
961
+ return this.module.identifier;
962
+ },
963
+ enumerable: true,
964
+ configurable: true
965
+ });
966
+ Object.defineProperty(Context.prototype, "schema", {
967
+ get: function() {
968
+ return this.application.schema;
969
+ },
970
+ enumerable: true,
971
+ configurable: true
972
+ });
973
+ Object.defineProperty(Context.prototype, "dispatcher", {
974
+ get: function() {
975
+ return this.application.dispatcher;
976
+ },
977
+ enumerable: true,
978
+ configurable: true
979
+ });
980
+ Object.defineProperty(Context.prototype, "element", {
981
+ get: function() {
982
+ return this.scope.element;
983
+ },
984
+ enumerable: true,
985
+ configurable: true
986
+ });
987
+ Object.defineProperty(Context.prototype, "parentElement", {
988
+ get: function() {
989
+ return this.element.parentElement;
990
+ },
991
+ enumerable: true,
992
+ configurable: true
993
+ });
994
+ Context.prototype.handleError = function(error, message, detail) {
995
+ if (detail === void 0) {
996
+ detail = {};
997
+ }
998
+ var _a = this, identifier = _a.identifier, controller = _a.controller, element = _a.element;
999
+ detail = Object.assign({
1000
+ identifier: identifier,
1001
+ controller: controller,
1002
+ element: element
1003
+ }, detail);
1004
+ this.application.handleError(error, "Error " + message, detail);
1005
+ };
1006
+ return Context;
1007
+ }();
1008
+ var __extends$1 = undefined && undefined.__extends || function() {
1009
+ var extendStatics = Object.setPrototypeOf || {
1010
+ __proto__: []
1011
+ } instanceof Array && function(d, b) {
1012
+ d.__proto__ = b;
1013
+ } || function(d, b) {
1014
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
1015
+ };
1016
+ return function(d, b) {
1017
+ extendStatics(d, b);
1018
+ function __() {
1019
+ this.constructor = d;
1020
+ }
1021
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
1022
+ };
1023
+ }();
1024
+ function blessDefinition(definition) {
1025
+ return {
1026
+ identifier: definition.identifier,
1027
+ controllerConstructor: blessControllerConstructor(definition.controllerConstructor)
1028
+ };
1029
+ }
1030
+ function blessControllerConstructor(controllerConstructor) {
1031
+ var constructor = extend(controllerConstructor);
1032
+ constructor.bless();
1033
+ return constructor;
1034
+ }
1035
+ var extend = function() {
1036
+ function extendWithReflect(constructor) {
1037
+ function Controller() {
1038
+ var _newTarget = this && this instanceof Controller ? this.constructor : void 0;
1039
+ return Reflect.construct(constructor, arguments, _newTarget);
1040
+ }
1041
+ Controller.prototype = Object.create(constructor.prototype, {
1042
+ constructor: {
1043
+ value: Controller
1044
+ }
1045
+ });
1046
+ Reflect.setPrototypeOf(Controller, constructor);
1047
+ return Controller;
1048
+ }
1049
+ function testReflectExtension() {
1050
+ var a = function() {
1051
+ this.a.call(this);
1052
+ };
1053
+ var b = extendWithReflect(a);
1054
+ b.prototype.a = function() {};
1055
+ return new b();
1056
+ }
1057
+ try {
1058
+ testReflectExtension();
1059
+ return extendWithReflect;
1060
+ } catch (error) {
1061
+ return function(constructor) {
1062
+ return function(_super) {
1063
+ __extends$1(Controller, _super);
1064
+ function Controller() {
1065
+ return _super !== null && _super.apply(this, arguments) || this;
1066
+ }
1067
+ return Controller;
1068
+ }(constructor);
1069
+ };
1070
+ }
1071
+ }();
1072
+ var Module = function() {
1073
+ function Module(application, definition) {
1074
+ this.application = application;
1075
+ this.definition = blessDefinition(definition);
1076
+ this.contextsByScope = new WeakMap();
1077
+ this.connectedContexts = new Set();
1078
+ }
1079
+ Object.defineProperty(Module.prototype, "identifier", {
1080
+ get: function() {
1081
+ return this.definition.identifier;
1082
+ },
1083
+ enumerable: true,
1084
+ configurable: true
1085
+ });
1086
+ Object.defineProperty(Module.prototype, "controllerConstructor", {
1087
+ get: function() {
1088
+ return this.definition.controllerConstructor;
1089
+ },
1090
+ enumerable: true,
1091
+ configurable: true
1092
+ });
1093
+ Object.defineProperty(Module.prototype, "contexts", {
1094
+ get: function() {
1095
+ return Array.from(this.connectedContexts);
1096
+ },
1097
+ enumerable: true,
1098
+ configurable: true
1099
+ });
1100
+ Module.prototype.connectContextForScope = function(scope) {
1101
+ var context = this.fetchContextForScope(scope);
1102
+ this.connectedContexts.add(context);
1103
+ context.connect();
1104
+ };
1105
+ Module.prototype.disconnectContextForScope = function(scope) {
1106
+ var context = this.contextsByScope.get(scope);
1107
+ if (context) {
1108
+ this.connectedContexts.delete(context);
1109
+ context.disconnect();
1110
+ }
1111
+ };
1112
+ Module.prototype.fetchContextForScope = function(scope) {
1113
+ var context = this.contextsByScope.get(scope);
1114
+ if (!context) {
1115
+ context = new Context(this, scope);
1116
+ this.contextsByScope.set(scope, context);
1117
+ }
1118
+ return context;
1119
+ };
1120
+ return Module;
1121
+ }();
1122
+ var DataMap = function() {
1123
+ function DataMap(scope) {
1124
+ this.scope = scope;
1125
+ }
1126
+ Object.defineProperty(DataMap.prototype, "element", {
1127
+ get: function() {
1128
+ return this.scope.element;
1129
+ },
1130
+ enumerable: true,
1131
+ configurable: true
1132
+ });
1133
+ Object.defineProperty(DataMap.prototype, "identifier", {
1134
+ get: function() {
1135
+ return this.scope.identifier;
1136
+ },
1137
+ enumerable: true,
1138
+ configurable: true
1139
+ });
1140
+ DataMap.prototype.get = function(key) {
1141
+ key = this.getFormattedKey(key);
1142
+ return this.element.getAttribute(key);
1143
+ };
1144
+ DataMap.prototype.set = function(key, value) {
1145
+ key = this.getFormattedKey(key);
1146
+ this.element.setAttribute(key, value);
1147
+ return this.get(key);
1148
+ };
1149
+ DataMap.prototype.has = function(key) {
1150
+ key = this.getFormattedKey(key);
1151
+ return this.element.hasAttribute(key);
1152
+ };
1153
+ DataMap.prototype.delete = function(key) {
1154
+ if (this.has(key)) {
1155
+ key = this.getFormattedKey(key);
1156
+ this.element.removeAttribute(key);
1157
+ return true;
1158
+ } else {
1159
+ return false;
1160
+ }
1161
+ };
1162
+ DataMap.prototype.getFormattedKey = function(key) {
1163
+ return "data-" + this.identifier + "-" + dasherize(key);
1164
+ };
1165
+ return DataMap;
1166
+ }();
1167
+ function dasherize(value) {
1168
+ return value.replace(/([A-Z])/g, function(_, char) {
1169
+ return "-" + char.toLowerCase();
1170
+ });
1171
+ }
1172
+ function attributeValueContainsToken(attributeName, token) {
1173
+ return "[" + attributeName + '~="' + token + '"]';
1174
+ }
1175
+ var TargetSet = function() {
1176
+ function TargetSet(scope) {
1177
+ this.scope = scope;
1178
+ }
1179
+ Object.defineProperty(TargetSet.prototype, "element", {
1180
+ get: function() {
1181
+ return this.scope.element;
1182
+ },
1183
+ enumerable: true,
1184
+ configurable: true
1185
+ });
1186
+ Object.defineProperty(TargetSet.prototype, "identifier", {
1187
+ get: function() {
1188
+ return this.scope.identifier;
1189
+ },
1190
+ enumerable: true,
1191
+ configurable: true
1192
+ });
1193
+ Object.defineProperty(TargetSet.prototype, "schema", {
1194
+ get: function() {
1195
+ return this.scope.schema;
1196
+ },
1197
+ enumerable: true,
1198
+ configurable: true
1199
+ });
1200
+ TargetSet.prototype.has = function(targetName) {
1201
+ return this.find(targetName) != null;
1202
+ };
1203
+ TargetSet.prototype.find = function() {
1204
+ var targetNames = [];
1205
+ for (var _i = 0; _i < arguments.length; _i++) {
1206
+ targetNames[_i] = arguments[_i];
1207
+ }
1208
+ var selector = this.getSelectorForTargetNames(targetNames);
1209
+ return this.scope.findElement(selector);
1210
+ };
1211
+ TargetSet.prototype.findAll = function() {
1212
+ var targetNames = [];
1213
+ for (var _i = 0; _i < arguments.length; _i++) {
1214
+ targetNames[_i] = arguments[_i];
1215
+ }
1216
+ var selector = this.getSelectorForTargetNames(targetNames);
1217
+ return this.scope.findAllElements(selector);
1218
+ };
1219
+ TargetSet.prototype.getSelectorForTargetNames = function(targetNames) {
1220
+ var _this = this;
1221
+ return targetNames.map(function(targetName) {
1222
+ return _this.getSelectorForTargetName(targetName);
1223
+ }).join(", ");
1224
+ };
1225
+ TargetSet.prototype.getSelectorForTargetName = function(targetName) {
1226
+ var targetDescriptor = this.identifier + "." + targetName;
1227
+ return attributeValueContainsToken(this.schema.targetAttribute, targetDescriptor);
1228
+ };
1229
+ return TargetSet;
1230
+ }();
1231
+ var Scope = function() {
1232
+ function Scope(schema, identifier, element) {
1233
+ this.schema = schema;
1234
+ this.identifier = identifier;
1235
+ this.element = element;
1236
+ this.targets = new TargetSet(this);
1237
+ this.data = new DataMap(this);
1238
+ }
1239
+ Scope.prototype.findElement = function(selector) {
1240
+ return this.findAllElements(selector)[0];
1241
+ };
1242
+ Scope.prototype.findAllElements = function(selector) {
1243
+ var head = this.element.matches(selector) ? [ this.element ] : [];
1244
+ var tail = this.filterElements(Array.from(this.element.querySelectorAll(selector)));
1245
+ return head.concat(tail);
1246
+ };
1247
+ Scope.prototype.filterElements = function(elements) {
1248
+ var _this = this;
1249
+ return elements.filter(function(element) {
1250
+ return _this.containsElement(element);
1251
+ });
1252
+ };
1253
+ Scope.prototype.containsElement = function(element) {
1254
+ return element.closest(this.controllerSelector) === this.element;
1255
+ };
1256
+ Object.defineProperty(Scope.prototype, "controllerSelector", {
1257
+ get: function() {
1258
+ return attributeValueContainsToken(this.schema.controllerAttribute, this.identifier);
1259
+ },
1260
+ enumerable: true,
1261
+ configurable: true
1262
+ });
1263
+ return Scope;
1264
+ }();
1265
+ var ScopeObserver = function() {
1266
+ function ScopeObserver(element, schema, delegate) {
1267
+ this.element = element;
1268
+ this.schema = schema;
1269
+ this.delegate = delegate;
1270
+ this.valueListObserver = new ValueListObserver(this.element, this.controllerAttribute, this);
1271
+ this.scopesByIdentifierByElement = new WeakMap();
1272
+ this.scopeReferenceCounts = new WeakMap();
1273
+ }
1274
+ ScopeObserver.prototype.start = function() {
1275
+ this.valueListObserver.start();
1276
+ };
1277
+ ScopeObserver.prototype.stop = function() {
1278
+ this.valueListObserver.stop();
1279
+ };
1280
+ Object.defineProperty(ScopeObserver.prototype, "controllerAttribute", {
1281
+ get: function() {
1282
+ return this.schema.controllerAttribute;
1283
+ },
1284
+ enumerable: true,
1285
+ configurable: true
1286
+ });
1287
+ ScopeObserver.prototype.parseValueForToken = function(token) {
1288
+ var element = token.element, identifier = token.content;
1289
+ var scopesByIdentifier = this.fetchScopesByIdentifierForElement(element);
1290
+ var scope = scopesByIdentifier.get(identifier);
1291
+ if (!scope) {
1292
+ scope = new Scope(this.schema, identifier, element);
1293
+ scopesByIdentifier.set(identifier, scope);
1294
+ }
1295
+ return scope;
1296
+ };
1297
+ ScopeObserver.prototype.elementMatchedValue = function(element, value) {
1298
+ var referenceCount = (this.scopeReferenceCounts.get(value) || 0) + 1;
1299
+ this.scopeReferenceCounts.set(value, referenceCount);
1300
+ if (referenceCount == 1) {
1301
+ this.delegate.scopeConnected(value);
1302
+ }
1303
+ };
1304
+ ScopeObserver.prototype.elementUnmatchedValue = function(element, value) {
1305
+ var referenceCount = this.scopeReferenceCounts.get(value);
1306
+ if (referenceCount) {
1307
+ this.scopeReferenceCounts.set(value, referenceCount - 1);
1308
+ if (referenceCount == 1) {
1309
+ this.delegate.scopeDisconnected(value);
1310
+ }
1311
+ }
1312
+ };
1313
+ ScopeObserver.prototype.fetchScopesByIdentifierForElement = function(element) {
1314
+ var scopesByIdentifier = this.scopesByIdentifierByElement.get(element);
1315
+ if (!scopesByIdentifier) {
1316
+ scopesByIdentifier = new Map();
1317
+ this.scopesByIdentifierByElement.set(element, scopesByIdentifier);
1318
+ }
1319
+ return scopesByIdentifier;
1320
+ };
1321
+ return ScopeObserver;
1322
+ }();
1323
+ var Router = function() {
1324
+ function Router(application) {
1325
+ this.application = application;
1326
+ this.scopeObserver = new ScopeObserver(this.element, this.schema, this);
1327
+ this.scopesByIdentifier = new Multimap();
1328
+ this.modulesByIdentifier = new Map();
1329
+ }
1330
+ Object.defineProperty(Router.prototype, "element", {
1331
+ get: function() {
1332
+ return this.application.element;
1333
+ },
1334
+ enumerable: true,
1335
+ configurable: true
1336
+ });
1337
+ Object.defineProperty(Router.prototype, "schema", {
1338
+ get: function() {
1339
+ return this.application.schema;
1340
+ },
1341
+ enumerable: true,
1342
+ configurable: true
1343
+ });
1344
+ Object.defineProperty(Router.prototype, "controllerAttribute", {
1345
+ get: function() {
1346
+ return this.schema.controllerAttribute;
1347
+ },
1348
+ enumerable: true,
1349
+ configurable: true
1350
+ });
1351
+ Object.defineProperty(Router.prototype, "modules", {
1352
+ get: function() {
1353
+ return Array.from(this.modulesByIdentifier.values());
1354
+ },
1355
+ enumerable: true,
1356
+ configurable: true
1357
+ });
1358
+ Object.defineProperty(Router.prototype, "contexts", {
1359
+ get: function() {
1360
+ return this.modules.reduce(function(contexts, module) {
1361
+ return contexts.concat(module.contexts);
1362
+ }, []);
1363
+ },
1364
+ enumerable: true,
1365
+ configurable: true
1366
+ });
1367
+ Router.prototype.start = function() {
1368
+ this.scopeObserver.start();
1369
+ };
1370
+ Router.prototype.stop = function() {
1371
+ this.scopeObserver.stop();
1372
+ };
1373
+ Router.prototype.loadDefinition = function(definition) {
1374
+ this.unloadIdentifier(definition.identifier);
1375
+ var module = new Module(this.application, definition);
1376
+ this.connectModule(module);
1377
+ };
1378
+ Router.prototype.unloadIdentifier = function(identifier) {
1379
+ var module = this.modulesByIdentifier.get(identifier);
1380
+ if (module) {
1381
+ this.disconnectModule(module);
1382
+ }
1383
+ };
1384
+ Router.prototype.getContextForElementAndIdentifier = function(element, identifier) {
1385
+ var module = this.modulesByIdentifier.get(identifier);
1386
+ if (module) {
1387
+ return module.contexts.find(function(context) {
1388
+ return context.element == element;
1389
+ });
1390
+ }
1391
+ };
1392
+ Router.prototype.handleError = function(error, message, detail) {
1393
+ this.application.handleError(error, message, detail);
1394
+ };
1395
+ Router.prototype.scopeConnected = function(scope) {
1396
+ this.scopesByIdentifier.add(scope.identifier, scope);
1397
+ var module = this.modulesByIdentifier.get(scope.identifier);
1398
+ if (module) {
1399
+ module.connectContextForScope(scope);
1400
+ }
1401
+ };
1402
+ Router.prototype.scopeDisconnected = function(scope) {
1403
+ this.scopesByIdentifier.delete(scope.identifier, scope);
1404
+ var module = this.modulesByIdentifier.get(scope.identifier);
1405
+ if (module) {
1406
+ module.disconnectContextForScope(scope);
1407
+ }
1408
+ };
1409
+ Router.prototype.connectModule = function(module) {
1410
+ this.modulesByIdentifier.set(module.identifier, module);
1411
+ var scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
1412
+ scopes.forEach(function(scope) {
1413
+ return module.connectContextForScope(scope);
1414
+ });
1415
+ };
1416
+ Router.prototype.disconnectModule = function(module) {
1417
+ this.modulesByIdentifier.delete(module.identifier);
1418
+ var scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
1419
+ scopes.forEach(function(scope) {
1420
+ return module.disconnectContextForScope(scope);
1421
+ });
1422
+ };
1423
+ return Router;
1424
+ }();
1425
+ var defaultSchema = {
1426
+ controllerAttribute: "data-controller",
1427
+ actionAttribute: "data-action",
1428
+ targetAttribute: "data-target"
1429
+ };
1430
+ var __awaiter = undefined && undefined.__awaiter || function(thisArg, _arguments, P, generator) {
1431
+ return new (P || (P = Promise))(function(resolve, reject) {
1432
+ function fulfilled(value) {
1433
+ try {
1434
+ step(generator.next(value));
1435
+ } catch (e) {
1436
+ reject(e);
1437
+ }
1438
+ }
1439
+ function rejected(value) {
1440
+ try {
1441
+ step(generator["throw"](value));
1442
+ } catch (e) {
1443
+ reject(e);
1444
+ }
1445
+ }
1446
+ function step(result) {
1447
+ result.done ? resolve(result.value) : new P(function(resolve) {
1448
+ resolve(result.value);
1449
+ }).then(fulfilled, rejected);
1450
+ }
1451
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1452
+ });
1453
+ };
1454
+ var __generator = undefined && undefined.__generator || function(thisArg, body) {
1455
+ var _ = {
1456
+ label: 0,
1457
+ sent: function() {
1458
+ if (t[0] & 1) throw t[1];
1459
+ return t[1];
1460
+ },
1461
+ trys: [],
1462
+ ops: []
1463
+ }, f, y, t, g;
1464
+ return g = {
1465
+ next: verb(0),
1466
+ throw: verb(1),
1467
+ return: verb(2)
1468
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
1469
+ return this;
1470
+ }), g;
1471
+ function verb(n) {
1472
+ return function(v) {
1473
+ return step([ n, v ]);
1474
+ };
1475
+ }
1476
+ function step(op) {
1477
+ if (f) throw new TypeError("Generator is already executing.");
1478
+ while (_) try {
1479
+ if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
1480
+ if (y = 0, t) op = [ 0, t.value ];
1481
+ switch (op[0]) {
1482
+ case 0:
1483
+ case 1:
1484
+ t = op;
1485
+ break;
1486
+
1487
+ case 4:
1488
+ _.label++;
1489
+ return {
1490
+ value: op[1],
1491
+ done: false
1492
+ };
1493
+
1494
+ case 5:
1495
+ _.label++;
1496
+ y = op[1];
1497
+ op = [ 0 ];
1498
+ continue;
1499
+
1500
+ case 7:
1501
+ op = _.ops.pop();
1502
+ _.trys.pop();
1503
+ continue;
1504
+
1505
+ default:
1506
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
1507
+ _ = 0;
1508
+ continue;
1509
+ }
1510
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
1511
+ _.label = op[1];
1512
+ break;
1513
+ }
1514
+ if (op[0] === 6 && _.label < t[1]) {
1515
+ _.label = t[1];
1516
+ t = op;
1517
+ break;
1518
+ }
1519
+ if (t && _.label < t[2]) {
1520
+ _.label = t[2];
1521
+ _.ops.push(op);
1522
+ break;
1523
+ }
1524
+ if (t[2]) _.ops.pop();
1525
+ _.trys.pop();
1526
+ continue;
1527
+ }
1528
+ op = body.call(thisArg, _);
1529
+ } catch (e) {
1530
+ op = [ 6, e ];
1531
+ y = 0;
1532
+ } finally {
1533
+ f = t = 0;
1534
+ }
1535
+ if (op[0] & 5) throw op[1];
1536
+ return {
1537
+ value: op[0] ? op[1] : void 0,
1538
+ done: true
1539
+ };
1540
+ }
1541
+ };
1542
+ var Application = function() {
1543
+ function Application(element, schema) {
1544
+ if (element === void 0) {
1545
+ element = document.documentElement;
1546
+ }
1547
+ if (schema === void 0) {
1548
+ schema = defaultSchema;
1549
+ }
1550
+ this.element = element;
1551
+ this.schema = schema;
1552
+ this.dispatcher = new Dispatcher(this);
1553
+ this.router = new Router(this);
1554
+ }
1555
+ Application.start = function(element, schema) {
1556
+ var application = new Application(element, schema);
1557
+ application.start();
1558
+ return application;
1559
+ };
1560
+ Application.prototype.start = function() {
1561
+ return __awaiter(this, void 0, void 0, function() {
1562
+ return __generator(this, function(_a) {
1563
+ switch (_a.label) {
1564
+ case 0:
1565
+ return [ 4, domReady() ];
1566
+
1567
+ case 1:
1568
+ _a.sent();
1569
+ this.router.start();
1570
+ this.dispatcher.start();
1571
+ return [ 2 ];
1572
+ }
1573
+ });
1574
+ });
1575
+ };
1576
+ Application.prototype.stop = function() {
1577
+ this.router.stop();
1578
+ this.dispatcher.stop();
1579
+ };
1580
+ Application.prototype.register = function(identifier, controllerConstructor) {
1581
+ this.load({
1582
+ identifier: identifier,
1583
+ controllerConstructor: controllerConstructor
1584
+ });
1585
+ };
1586
+ Application.prototype.load = function(head) {
1587
+ var _this = this;
1588
+ var rest = [];
1589
+ for (var _i = 1; _i < arguments.length; _i++) {
1590
+ rest[_i - 1] = arguments[_i];
1591
+ }
1592
+ var definitions = Array.isArray(head) ? head : [ head ].concat(rest);
1593
+ definitions.forEach(function(definition) {
1594
+ return _this.router.loadDefinition(definition);
1595
+ });
1596
+ };
1597
+ Application.prototype.unload = function(head) {
1598
+ var _this = this;
1599
+ var rest = [];
1600
+ for (var _i = 1; _i < arguments.length; _i++) {
1601
+ rest[_i - 1] = arguments[_i];
1602
+ }
1603
+ var identifiers = Array.isArray(head) ? head : [ head ].concat(rest);
1604
+ identifiers.forEach(function(identifier) {
1605
+ return _this.router.unloadIdentifier(identifier);
1606
+ });
1607
+ };
1608
+ Object.defineProperty(Application.prototype, "controllers", {
1609
+ get: function() {
1610
+ return this.router.contexts.map(function(context) {
1611
+ return context.controller;
1612
+ });
1613
+ },
1614
+ enumerable: true,
1615
+ configurable: true
1616
+ });
1617
+ Application.prototype.getControllerForElementAndIdentifier = function(element, identifier) {
1618
+ var context = this.router.getContextForElementAndIdentifier(element, identifier);
1619
+ return context ? context.controller : null;
1620
+ };
1621
+ Application.prototype.handleError = function(error, message, detail) {
1622
+ console.error("%s\n\n%o\n\n%o", message, error, detail);
1623
+ };
1624
+ return Application;
1625
+ }();
1626
+ function domReady() {
1627
+ return new Promise(function(resolve) {
1628
+ if (document.readyState == "loading") {
1629
+ document.addEventListener("DOMContentLoaded", resolve);
1630
+ } else {
1631
+ resolve();
1632
+ }
1633
+ });
1634
+ }
1635
+ function defineTargetProperties(constructor) {
1636
+ var prototype = constructor.prototype;
1637
+ var targetNames = getTargetNamesForConstructor(constructor);
1638
+ targetNames.forEach(function(name) {
1639
+ var _a;
1640
+ return defineLinkedProperties(prototype, (_a = {}, _a[name + "Target"] = {
1641
+ get: function() {
1642
+ var target = this.targets.find(name);
1643
+ if (target) {
1644
+ return target;
1645
+ } else {
1646
+ throw new Error('Missing target element "' + this.identifier + "." + name + '"');
1647
+ }
1648
+ }
1649
+ }, _a[name + "Targets"] = {
1650
+ get: function() {
1651
+ return this.targets.findAll(name);
1652
+ }
1653
+ }, _a["has" + capitalize(name) + "Target"] = {
1654
+ get: function() {
1655
+ return this.targets.has(name);
1656
+ }
1657
+ }, _a));
1658
+ });
1659
+ }
1660
+ function getTargetNamesForConstructor(constructor) {
1661
+ var ancestors = getAncestorsForConstructor(constructor);
1662
+ return Array.from(ancestors.reduce(function(targetNames, constructor) {
1663
+ getOwnTargetNamesForConstructor(constructor).forEach(function(name) {
1664
+ return targetNames.add(name);
1665
+ });
1666
+ return targetNames;
1667
+ }, new Set()));
1668
+ }
1669
+ function getAncestorsForConstructor(constructor) {
1670
+ var ancestors = [];
1671
+ while (constructor) {
1672
+ ancestors.push(constructor);
1673
+ constructor = Object.getPrototypeOf(constructor);
1674
+ }
1675
+ return ancestors;
1676
+ }
1677
+ function getOwnTargetNamesForConstructor(constructor) {
1678
+ var definition = constructor["targets"];
1679
+ return Array.isArray(definition) ? definition : [];
1680
+ }
1681
+ function defineLinkedProperties(object, properties) {
1682
+ Object.keys(properties).forEach(function(name) {
1683
+ if (!(name in object)) {
1684
+ var descriptor = properties[name];
1685
+ Object.defineProperty(object, name, descriptor);
1686
+ }
1687
+ });
1688
+ }
1689
+ function capitalize(name) {
1690
+ return name.charAt(0).toUpperCase() + name.slice(1);
1691
+ }
1692
+ var Controller = function() {
1693
+ function Controller(context) {
1694
+ this.context = context;
1695
+ }
1696
+ Controller.bless = function() {
1697
+ defineTargetProperties(this);
1698
+ };
1699
+ Object.defineProperty(Controller.prototype, "application", {
1700
+ get: function() {
1701
+ return this.context.application;
1702
+ },
1703
+ enumerable: true,
1704
+ configurable: true
1705
+ });
1706
+ Object.defineProperty(Controller.prototype, "scope", {
1707
+ get: function() {
1708
+ return this.context.scope;
1709
+ },
1710
+ enumerable: true,
1711
+ configurable: true
1712
+ });
1713
+ Object.defineProperty(Controller.prototype, "element", {
1714
+ get: function() {
1715
+ return this.scope.element;
1716
+ },
1717
+ enumerable: true,
1718
+ configurable: true
1719
+ });
1720
+ Object.defineProperty(Controller.prototype, "identifier", {
1721
+ get: function() {
1722
+ return this.scope.identifier;
1723
+ },
1724
+ enumerable: true,
1725
+ configurable: true
1726
+ });
1727
+ Object.defineProperty(Controller.prototype, "targets", {
1728
+ get: function() {
1729
+ return this.scope.targets;
1730
+ },
1731
+ enumerable: true,
1732
+ configurable: true
1733
+ });
1734
+ Object.defineProperty(Controller.prototype, "data", {
1735
+ get: function() {
1736
+ return this.scope.data;
1737
+ },
1738
+ enumerable: true,
1739
+ configurable: true
1740
+ });
1741
+ Controller.prototype.initialize = function() {};
1742
+ Controller.prototype.connect = function() {};
1743
+ Controller.prototype.disconnect = function() {};
1744
+ Controller.targets = [];
1745
+ return Controller;
1746
+ }();
1747
+ exports.Application = Application;
1748
+ exports.Context = Context;
1749
+ exports.Controller = Controller;
1750
+ exports.defaultSchema = defaultSchema;
1751
+ Object.defineProperty(exports, "__esModule", {
1752
+ value: true
1753
+ });
1754
+ });