You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stimulus.js 58KB

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