UNPKG

@kanety/stimulus-static-actions

Version:

A stimulus extension to define actions in controller

1 lines 7.68 kB
{"version":3,"file":"index.module.mjs","sources":["../src/action.js","../src/action_set.js","../src/inheritable_statics.js","../src/index.js"],"sourcesContent":["export default class Action {\n constructor(identifier, definition) {\n this.identifier = identifier;\n this.name = definition[0];\n this.descriptor = definition[1];\n this.options = definition[2] || {};\n };\n\n get description() {\n let descriptor = this.descriptor;\n if (descriptor.match(/^:/)) {\n descriptor = descriptor.replace(/^:/, `${this.identifier}:`)\n }\n if (descriptor.includes('->')) {\n return descriptor.replace(/->/, `->${this.identifier}#`);\n } else {\n return `${this.identifier}#${descriptor}`;\n }\n }\n}\n","import Action from './action';\nimport { readInheritableStaticArrayValues } from './inheritable_statics';\n\nexport default class ActionSet {\n constructor(context) {\n this.context = context;\n this.controller = context.controller;\n this.actions = [];\n }\n\n run() {\n this.add(readInheritableStaticArrayValues(this.controller.constructor, 'actions'));\n }\n\n add(definitions) {\n let actions = definitions.map((definition) => new Action(this.controller.identifier, definition));\n\n actions.forEach((action) => {\n let targets = this.resolveTargets(action);\n targets.forEach((target) => {\n this.addAction(target, action);\n });\n this.actions.push(action);\n });\n }\n\n remove(definitions) {\n let actions = definitions.map((definition) => new Action(this.controller.identifier, definition));\n\n actions.forEach((action) => {\n let targets = this.resolveTargets(action);\n targets.forEach((target) => {\n this.removeAction(target, action);\n });\n this.actions = this.actions.filter((a) => a.description != action.description);\n });\n }\n\n resolveTargets(action) {\n let targets = this.findTargets(action.name);\n\n if (action.options.if) {\n return this.filterTargets(targets, action.options.if);\n } else {\n return targets;\n }\n }\n\n findTargets(name) {\n if (name == 'element') {\n return [this.controller.element];\n } else if (this.controller[`${name}Targets`]) {\n return [].concat(this.controller[`${name}Targets`]);\n } else {\n return [];\n }\n }\n\n filterTargets(targets, ifvalue) {\n return targets.filter((target) => {\n let property = this.controller[ifvalue];\n if (typeof(property) == 'function') {\n property = property.apply(this.controller, target);\n }\n return property != false\n });\n }\n\n addAction(target, action) {\n let description = action.description;\n let currentDescriptions = (target.dataset['action'] || '').split(' ');\n if (!currentDescriptions.some((currentDescription) => currentDescription == description)) {\n currentDescriptions.push(description);\n }\n target.dataset['action'] = currentDescriptions.join(' ').trim();\n }\n\n removeAction(target, action) {\n let description = action.description;\n let currentDescriptions = (target.dataset['action'] || '').split(' ');\n currentDescriptions = currentDescriptions.filter((currentDescription) => currentDescription != description);\n target.dataset['action'] = currentDescriptions.join(' ').trim();\n }\n}\n","export function readInheritableStaticArrayValues(constructor, propertyName) {\n let ancestors = getAncestorsForConstructor(constructor);\n return Array.from(ancestors.reduce((values, constructor) => {\n getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name));\n return values;\n }, new Set));\n}\n\nfunction getAncestorsForConstructor(constructor) {\n const ancestors = [];\n while (constructor) {\n ancestors.push(constructor);\n constructor = Object.getPrototypeOf(constructor);\n }\n return ancestors.reverse();\n}\n\nfunction getOwnStaticArrayValues(constructor, propertyName) {\n const definition = constructor[propertyName];\n return Array.isArray(definition) ? definition : [];\n}\n","import { Context } from '@hotwired/stimulus';\nimport ActionSet from './action_set';\n\nlet oConnect = Context.prototype.connect;\nlet oTargetConnected = Context.prototype.targetConnected;\n\nContext.prototype.connect = function() {\n this.actionSet = new ActionSet(this);\n this.actionSet.run();\n return oConnect.apply(this, arguments);\n};\n\nContext.prototype.targetConnected = function() {\n this.actionSet.run();\n return oTargetConnected.apply(this, arguments);\n}\n"],"names":["Action","constructor","identifier","definition","this","name","descriptor","options","description","match","replace","includes","ActionSet","context","controller","actions","run","ancestors","add","push","Object","getPrototypeOf","reverse","getAncestorsForConstructor","Array","from","reduce","values","propertyName","isArray","getOwnStaticArrayValues","forEach","Set","definitions","map","action","resolveTargets","target","addAction","remove","removeAction","filter","a","targets","findTargets","if","filterTargets","element","concat","ifvalue","property","apply","currentDescriptions","dataset","split","some","currentDescription","join","trim","oConnect","Context","prototype","connect","oTargetConnected","targetConnected","actionSet","arguments"],"mappings":"6CAAe,MAAMA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