stimulus-flatpickr
Advanced tools
Comparing version 1.4.0 to 3.0.0-0
@@ -5,3 +5,2 @@ 'use strict'; | ||
var stimulus = require('stimulus'); | ||
var flatpickr = _interopDefault(require('flatpickr')); | ||
@@ -157,4 +156,446 @@ | ||
/* | ||
Stimulus 3.0.0 | ||
Copyright Β© 2021 Basecamp, LLC | ||
*/ | ||
function camelize(value) { | ||
return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()); | ||
} | ||
function capitalize(value) { | ||
return value.charAt(0).toUpperCase() + value.slice(1); | ||
} | ||
function dasherize(value) { | ||
return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`); | ||
} | ||
function readInheritableStaticArrayValues(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return Array.from(ancestors.reduce((values, constructor) => { | ||
getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name)); | ||
return values; | ||
}, new Set())); | ||
} | ||
function readInheritableStaticObjectPairs(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return ancestors.reduce((pairs, constructor) => { | ||
pairs.push(...getOwnStaticObjectPairs(constructor, propertyName)); | ||
return pairs; | ||
}, []); | ||
} | ||
function getAncestorsForConstructor(constructor) { | ||
const ancestors = []; | ||
while (constructor) { | ||
ancestors.push(constructor); | ||
constructor = Object.getPrototypeOf(constructor); | ||
} | ||
return ancestors.reverse(); | ||
} | ||
function getOwnStaticArrayValues(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return Array.isArray(definition) ? definition : []; | ||
} | ||
function getOwnStaticObjectPairs(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return definition ? Object.keys(definition).map(key => [key, definition[key]]) : []; | ||
} | ||
const extend = (() => { | ||
function extendWithReflect(constructor) { | ||
function extended() { | ||
return Reflect.construct(constructor, arguments, new.target); | ||
} | ||
extended.prototype = Object.create(constructor.prototype, { | ||
constructor: { | ||
value: extended | ||
} | ||
}); | ||
Reflect.setPrototypeOf(extended, constructor); | ||
return extended; | ||
} | ||
function testReflectExtension() { | ||
const a = function () { | ||
this.a.call(this); | ||
}; | ||
const b = extendWithReflect(a); | ||
b.prototype.a = function () {}; | ||
return new b(); | ||
} | ||
try { | ||
testReflectExtension(); | ||
return extendWithReflect; | ||
} catch (error) { | ||
return constructor => class extended extends constructor {}; | ||
} | ||
})(); | ||
function ClassPropertiesBlessing(constructor) { | ||
const classes = readInheritableStaticArrayValues(constructor, "classes"); | ||
return classes.reduce((properties, classDefinition) => { | ||
return Object.assign(properties, propertiesForClassDefinition(classDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForClassDefinition(key) { | ||
return { | ||
[`${key}Class`]: { | ||
get() { | ||
const { | ||
classes | ||
} = this; | ||
if (classes.has(key)) { | ||
return classes.get(key); | ||
} else { | ||
const attribute = classes.getAttributeName(key); | ||
throw new Error(`Missing attribute "${attribute}"`); | ||
} | ||
} | ||
}, | ||
[`${key}Classes`]: { | ||
get() { | ||
return this.classes.getAll(key); | ||
} | ||
}, | ||
[`has${capitalize(key)}Class`]: { | ||
get() { | ||
return this.classes.has(key); | ||
} | ||
} | ||
}; | ||
} | ||
function TargetPropertiesBlessing(constructor) { | ||
const targets = readInheritableStaticArrayValues(constructor, "targets"); | ||
return targets.reduce((properties, targetDefinition) => { | ||
return Object.assign(properties, propertiesForTargetDefinition(targetDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForTargetDefinition(name) { | ||
return { | ||
[`${name}Target`]: { | ||
get() { | ||
const target = this.targets.find(name); | ||
if (target) { | ||
return target; | ||
} else { | ||
throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`); | ||
} | ||
} | ||
}, | ||
[`${name}Targets`]: { | ||
get() { | ||
return this.targets.findAll(name); | ||
} | ||
}, | ||
[`has${capitalize(name)}Target`]: { | ||
get() { | ||
return this.targets.has(name); | ||
} | ||
} | ||
}; | ||
} | ||
function ValuePropertiesBlessing(constructor) { | ||
const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values"); | ||
const propertyDescriptorMap = { | ||
valueDescriptorMap: { | ||
get() { | ||
return valueDefinitionPairs.reduce((result, valueDefinitionPair) => { | ||
const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair); | ||
const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key); | ||
return Object.assign(result, { | ||
[attributeName]: valueDescriptor | ||
}); | ||
}, {}); | ||
} | ||
} | ||
}; | ||
return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => { | ||
return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair)); | ||
}, propertyDescriptorMap); | ||
} | ||
function propertiesForValueDefinitionPair(valueDefinitionPair) { | ||
const definition = parseValueDefinitionPair(valueDefinitionPair); | ||
const { | ||
key, | ||
name, | ||
reader: read, | ||
writer: write | ||
} = definition; | ||
return { | ||
[name]: { | ||
get() { | ||
const value = this.data.get(key); | ||
if (value !== null) { | ||
return read(value); | ||
} else { | ||
return definition.defaultValue; | ||
} | ||
}, | ||
set(value) { | ||
if (value === undefined) { | ||
this.data.delete(key); | ||
} else { | ||
this.data.set(key, write(value)); | ||
} | ||
} | ||
}, | ||
[`has${capitalize(name)}`]: { | ||
get() { | ||
return this.data.has(key) || definition.hasCustomDefaultValue; | ||
} | ||
} | ||
}; | ||
} | ||
function parseValueDefinitionPair([token, typeDefinition]) { | ||
return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition); | ||
} | ||
function parseValueTypeConstant(constant) { | ||
switch (constant) { | ||
case Array: | ||
return "array"; | ||
case Boolean: | ||
return "boolean"; | ||
case Number: | ||
return "number"; | ||
case Object: | ||
return "object"; | ||
case String: | ||
return "string"; | ||
} | ||
} | ||
function parseValueTypeDefault(defaultValue) { | ||
switch (typeof defaultValue) { | ||
case "boolean": | ||
return "boolean"; | ||
case "number": | ||
return "number"; | ||
case "string": | ||
return "string"; | ||
} | ||
if (Array.isArray(defaultValue)) return "array"; | ||
if (Object.prototype.toString.call(defaultValue) === "[object Object]") return "object"; | ||
} | ||
function parseValueTypeObject(typeObject) { | ||
const typeFromObject = parseValueTypeConstant(typeObject.type); | ||
if (typeFromObject) { | ||
const defaultValueType = parseValueTypeDefault(typeObject.default); | ||
if (typeFromObject !== defaultValueType) { | ||
throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`); | ||
} | ||
return typeFromObject; | ||
} | ||
} | ||
function parseValueTypeDefinition(typeDefinition) { | ||
const typeFromObject = parseValueTypeObject(typeDefinition); | ||
const typeFromDefaultValue = parseValueTypeDefault(typeDefinition); | ||
const typeFromConstant = parseValueTypeConstant(typeDefinition); | ||
const type = typeFromObject || typeFromDefaultValue || typeFromConstant; | ||
if (type) return type; | ||
throw new Error(`Unknown value type "${typeDefinition}"`); | ||
} | ||
function defaultValueForDefinition(typeDefinition) { | ||
const constant = parseValueTypeConstant(typeDefinition); | ||
if (constant) return defaultValuesByType[constant]; | ||
const defaultValue = typeDefinition.default; | ||
if (defaultValue !== undefined) return defaultValue; | ||
return typeDefinition; | ||
} | ||
function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) { | ||
const key = `${dasherize(token)}-value`; | ||
const type = parseValueTypeDefinition(typeDefinition); | ||
return { | ||
type, | ||
key, | ||
name: camelize(key), | ||
get defaultValue() { | ||
return defaultValueForDefinition(typeDefinition); | ||
}, | ||
get hasCustomDefaultValue() { | ||
return parseValueTypeDefault(typeDefinition) !== undefined; | ||
}, | ||
reader: readers[type], | ||
writer: writers[type] || writers.default | ||
}; | ||
} | ||
const defaultValuesByType = { | ||
get array() { | ||
return []; | ||
}, | ||
boolean: false, | ||
number: 0, | ||
get object() { | ||
return {}; | ||
}, | ||
string: "" | ||
}; | ||
const readers = { | ||
array(value) { | ||
const array = JSON.parse(value); | ||
if (!Array.isArray(array)) { | ||
throw new TypeError("Expected array"); | ||
} | ||
return array; | ||
}, | ||
boolean(value) { | ||
return !(value == "0" || value == "false"); | ||
}, | ||
number(value) { | ||
return Number(value); | ||
}, | ||
object(value) { | ||
const object = JSON.parse(value); | ||
if (object === null || typeof object != "object" || Array.isArray(object)) { | ||
throw new TypeError("Expected object"); | ||
} | ||
return object; | ||
}, | ||
string(value) { | ||
return value; | ||
} | ||
}; | ||
const writers = { | ||
default: writeString, | ||
array: writeJSON, | ||
object: writeJSON | ||
}; | ||
function writeJSON(value) { | ||
return JSON.stringify(value); | ||
} | ||
function writeString(value) { | ||
return `${value}`; | ||
} | ||
class Controller { | ||
constructor(context) { | ||
this.context = context; | ||
} | ||
static get shouldLoad() { | ||
return true; | ||
} | ||
get application() { | ||
return this.context.application; | ||
} | ||
get scope() { | ||
return this.context.scope; | ||
} | ||
get element() { | ||
return this.scope.element; | ||
} | ||
get identifier() { | ||
return this.scope.identifier; | ||
} | ||
get targets() { | ||
return this.scope.targets; | ||
} | ||
get classes() { | ||
return this.scope.classes; | ||
} | ||
get data() { | ||
return this.scope.data; | ||
} | ||
initialize() {} | ||
connect() {} | ||
disconnect() {} | ||
dispatch(eventName, { | ||
target = this.element, | ||
detail = {}, | ||
prefix = this.identifier, | ||
bubbles = true, | ||
cancelable = true | ||
} = {}) { | ||
const type = prefix ? `${prefix}:${eventName}` : eventName; | ||
const event = new CustomEvent(type, { | ||
detail, | ||
bubbles, | ||
cancelable | ||
}); | ||
target.dispatchEvent(event); | ||
return event; | ||
} | ||
} | ||
Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing]; | ||
Controller.targets = []; | ||
Controller.values = {}; | ||
const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); | ||
const capitalize = string => { | ||
const capitalize$1 = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
@@ -260,3 +701,3 @@ }; | ||
if (this[event]) { | ||
const hook = `on${capitalize(event)}`; | ||
const hook = `on${capitalize$1(event)}`; | ||
this.config[hook] = this[event].bind(this); | ||
@@ -377,3 +818,3 @@ } | ||
return StimulusFlatpickr; | ||
}(stimulus.Controller); | ||
}(Controller); | ||
@@ -380,0 +821,0 @@ _defineProperty(StimulusFlatpickr, "targets", ['instance']); |
@@ -1,2 +0,1 @@ | ||
import { Controller } from 'stimulus'; | ||
import flatpickr from 'flatpickr'; | ||
@@ -152,4 +151,446 @@ | ||
/* | ||
Stimulus 3.0.0 | ||
Copyright Β© 2021 Basecamp, LLC | ||
*/ | ||
function camelize(value) { | ||
return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()); | ||
} | ||
function capitalize(value) { | ||
return value.charAt(0).toUpperCase() + value.slice(1); | ||
} | ||
function dasherize(value) { | ||
return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`); | ||
} | ||
function readInheritableStaticArrayValues(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return Array.from(ancestors.reduce((values, constructor) => { | ||
getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name)); | ||
return values; | ||
}, new Set())); | ||
} | ||
function readInheritableStaticObjectPairs(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return ancestors.reduce((pairs, constructor) => { | ||
pairs.push(...getOwnStaticObjectPairs(constructor, propertyName)); | ||
return pairs; | ||
}, []); | ||
} | ||
function getAncestorsForConstructor(constructor) { | ||
const ancestors = []; | ||
while (constructor) { | ||
ancestors.push(constructor); | ||
constructor = Object.getPrototypeOf(constructor); | ||
} | ||
return ancestors.reverse(); | ||
} | ||
function getOwnStaticArrayValues(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return Array.isArray(definition) ? definition : []; | ||
} | ||
function getOwnStaticObjectPairs(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return definition ? Object.keys(definition).map(key => [key, definition[key]]) : []; | ||
} | ||
const extend = (() => { | ||
function extendWithReflect(constructor) { | ||
function extended() { | ||
return Reflect.construct(constructor, arguments, new.target); | ||
} | ||
extended.prototype = Object.create(constructor.prototype, { | ||
constructor: { | ||
value: extended | ||
} | ||
}); | ||
Reflect.setPrototypeOf(extended, constructor); | ||
return extended; | ||
} | ||
function testReflectExtension() { | ||
const a = function () { | ||
this.a.call(this); | ||
}; | ||
const b = extendWithReflect(a); | ||
b.prototype.a = function () {}; | ||
return new b(); | ||
} | ||
try { | ||
testReflectExtension(); | ||
return extendWithReflect; | ||
} catch (error) { | ||
return constructor => class extended extends constructor {}; | ||
} | ||
})(); | ||
function ClassPropertiesBlessing(constructor) { | ||
const classes = readInheritableStaticArrayValues(constructor, "classes"); | ||
return classes.reduce((properties, classDefinition) => { | ||
return Object.assign(properties, propertiesForClassDefinition(classDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForClassDefinition(key) { | ||
return { | ||
[`${key}Class`]: { | ||
get() { | ||
const { | ||
classes | ||
} = this; | ||
if (classes.has(key)) { | ||
return classes.get(key); | ||
} else { | ||
const attribute = classes.getAttributeName(key); | ||
throw new Error(`Missing attribute "${attribute}"`); | ||
} | ||
} | ||
}, | ||
[`${key}Classes`]: { | ||
get() { | ||
return this.classes.getAll(key); | ||
} | ||
}, | ||
[`has${capitalize(key)}Class`]: { | ||
get() { | ||
return this.classes.has(key); | ||
} | ||
} | ||
}; | ||
} | ||
function TargetPropertiesBlessing(constructor) { | ||
const targets = readInheritableStaticArrayValues(constructor, "targets"); | ||
return targets.reduce((properties, targetDefinition) => { | ||
return Object.assign(properties, propertiesForTargetDefinition(targetDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForTargetDefinition(name) { | ||
return { | ||
[`${name}Target`]: { | ||
get() { | ||
const target = this.targets.find(name); | ||
if (target) { | ||
return target; | ||
} else { | ||
throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`); | ||
} | ||
} | ||
}, | ||
[`${name}Targets`]: { | ||
get() { | ||
return this.targets.findAll(name); | ||
} | ||
}, | ||
[`has${capitalize(name)}Target`]: { | ||
get() { | ||
return this.targets.has(name); | ||
} | ||
} | ||
}; | ||
} | ||
function ValuePropertiesBlessing(constructor) { | ||
const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values"); | ||
const propertyDescriptorMap = { | ||
valueDescriptorMap: { | ||
get() { | ||
return valueDefinitionPairs.reduce((result, valueDefinitionPair) => { | ||
const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair); | ||
const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key); | ||
return Object.assign(result, { | ||
[attributeName]: valueDescriptor | ||
}); | ||
}, {}); | ||
} | ||
} | ||
}; | ||
return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => { | ||
return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair)); | ||
}, propertyDescriptorMap); | ||
} | ||
function propertiesForValueDefinitionPair(valueDefinitionPair) { | ||
const definition = parseValueDefinitionPair(valueDefinitionPair); | ||
const { | ||
key, | ||
name, | ||
reader: read, | ||
writer: write | ||
} = definition; | ||
return { | ||
[name]: { | ||
get() { | ||
const value = this.data.get(key); | ||
if (value !== null) { | ||
return read(value); | ||
} else { | ||
return definition.defaultValue; | ||
} | ||
}, | ||
set(value) { | ||
if (value === undefined) { | ||
this.data.delete(key); | ||
} else { | ||
this.data.set(key, write(value)); | ||
} | ||
} | ||
}, | ||
[`has${capitalize(name)}`]: { | ||
get() { | ||
return this.data.has(key) || definition.hasCustomDefaultValue; | ||
} | ||
} | ||
}; | ||
} | ||
function parseValueDefinitionPair([token, typeDefinition]) { | ||
return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition); | ||
} | ||
function parseValueTypeConstant(constant) { | ||
switch (constant) { | ||
case Array: | ||
return "array"; | ||
case Boolean: | ||
return "boolean"; | ||
case Number: | ||
return "number"; | ||
case Object: | ||
return "object"; | ||
case String: | ||
return "string"; | ||
} | ||
} | ||
function parseValueTypeDefault(defaultValue) { | ||
switch (typeof defaultValue) { | ||
case "boolean": | ||
return "boolean"; | ||
case "number": | ||
return "number"; | ||
case "string": | ||
return "string"; | ||
} | ||
if (Array.isArray(defaultValue)) return "array"; | ||
if (Object.prototype.toString.call(defaultValue) === "[object Object]") return "object"; | ||
} | ||
function parseValueTypeObject(typeObject) { | ||
const typeFromObject = parseValueTypeConstant(typeObject.type); | ||
if (typeFromObject) { | ||
const defaultValueType = parseValueTypeDefault(typeObject.default); | ||
if (typeFromObject !== defaultValueType) { | ||
throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`); | ||
} | ||
return typeFromObject; | ||
} | ||
} | ||
function parseValueTypeDefinition(typeDefinition) { | ||
const typeFromObject = parseValueTypeObject(typeDefinition); | ||
const typeFromDefaultValue = parseValueTypeDefault(typeDefinition); | ||
const typeFromConstant = parseValueTypeConstant(typeDefinition); | ||
const type = typeFromObject || typeFromDefaultValue || typeFromConstant; | ||
if (type) return type; | ||
throw new Error(`Unknown value type "${typeDefinition}"`); | ||
} | ||
function defaultValueForDefinition(typeDefinition) { | ||
const constant = parseValueTypeConstant(typeDefinition); | ||
if (constant) return defaultValuesByType[constant]; | ||
const defaultValue = typeDefinition.default; | ||
if (defaultValue !== undefined) return defaultValue; | ||
return typeDefinition; | ||
} | ||
function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) { | ||
const key = `${dasherize(token)}-value`; | ||
const type = parseValueTypeDefinition(typeDefinition); | ||
return { | ||
type, | ||
key, | ||
name: camelize(key), | ||
get defaultValue() { | ||
return defaultValueForDefinition(typeDefinition); | ||
}, | ||
get hasCustomDefaultValue() { | ||
return parseValueTypeDefault(typeDefinition) !== undefined; | ||
}, | ||
reader: readers[type], | ||
writer: writers[type] || writers.default | ||
}; | ||
} | ||
const defaultValuesByType = { | ||
get array() { | ||
return []; | ||
}, | ||
boolean: false, | ||
number: 0, | ||
get object() { | ||
return {}; | ||
}, | ||
string: "" | ||
}; | ||
const readers = { | ||
array(value) { | ||
const array = JSON.parse(value); | ||
if (!Array.isArray(array)) { | ||
throw new TypeError("Expected array"); | ||
} | ||
return array; | ||
}, | ||
boolean(value) { | ||
return !(value == "0" || value == "false"); | ||
}, | ||
number(value) { | ||
return Number(value); | ||
}, | ||
object(value) { | ||
const object = JSON.parse(value); | ||
if (object === null || typeof object != "object" || Array.isArray(object)) { | ||
throw new TypeError("Expected object"); | ||
} | ||
return object; | ||
}, | ||
string(value) { | ||
return value; | ||
} | ||
}; | ||
const writers = { | ||
default: writeString, | ||
array: writeJSON, | ||
object: writeJSON | ||
}; | ||
function writeJSON(value) { | ||
return JSON.stringify(value); | ||
} | ||
function writeString(value) { | ||
return `${value}`; | ||
} | ||
class Controller { | ||
constructor(context) { | ||
this.context = context; | ||
} | ||
static get shouldLoad() { | ||
return true; | ||
} | ||
get application() { | ||
return this.context.application; | ||
} | ||
get scope() { | ||
return this.context.scope; | ||
} | ||
get element() { | ||
return this.scope.element; | ||
} | ||
get identifier() { | ||
return this.scope.identifier; | ||
} | ||
get targets() { | ||
return this.scope.targets; | ||
} | ||
get classes() { | ||
return this.scope.classes; | ||
} | ||
get data() { | ||
return this.scope.data; | ||
} | ||
initialize() {} | ||
connect() {} | ||
disconnect() {} | ||
dispatch(eventName, { | ||
target = this.element, | ||
detail = {}, | ||
prefix = this.identifier, | ||
bubbles = true, | ||
cancelable = true | ||
} = {}) { | ||
const type = prefix ? `${prefix}:${eventName}` : eventName; | ||
const event = new CustomEvent(type, { | ||
detail, | ||
bubbles, | ||
cancelable | ||
}); | ||
target.dispatchEvent(event); | ||
return event; | ||
} | ||
} | ||
Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing]; | ||
Controller.targets = []; | ||
Controller.values = {}; | ||
const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); | ||
const capitalize = string => { | ||
const capitalize$1 = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
@@ -255,3 +696,3 @@ }; | ||
if (this[event]) { | ||
const hook = `on${capitalize(event)}`; | ||
const hook = `on${capitalize$1(event)}`; | ||
this.config[hook] = this[event].bind(this); | ||
@@ -258,0 +699,0 @@ } |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('stimulus'), require('flatpickr')) : | ||
typeof define === 'function' && define.amd ? define(['stimulus', 'flatpickr'], factory) : | ||
(global = global || self, global.StimulusFlatpikcr = factory(global.Stimulus, global.Flatpickr)); | ||
}(this, (function (stimulus, flatpickr) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('flatpickr')) : | ||
typeof define === 'function' && define.amd ? define(['flatpickr'], factory) : | ||
(global = global || self, global.StimulusFlatpickr = factory(global.Flatpickr)); | ||
}(this, (function (flatpickr) { 'use strict'; | ||
@@ -157,4 +157,446 @@ flatpickr = flatpickr && Object.prototype.hasOwnProperty.call(flatpickr, 'default') ? flatpickr['default'] : flatpickr; | ||
/* | ||
Stimulus 3.0.0 | ||
Copyright Β© 2021 Basecamp, LLC | ||
*/ | ||
function camelize(value) { | ||
return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()); | ||
} | ||
function capitalize(value) { | ||
return value.charAt(0).toUpperCase() + value.slice(1); | ||
} | ||
function dasherize(value) { | ||
return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`); | ||
} | ||
function readInheritableStaticArrayValues(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return Array.from(ancestors.reduce((values, constructor) => { | ||
getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name)); | ||
return values; | ||
}, new Set())); | ||
} | ||
function readInheritableStaticObjectPairs(constructor, propertyName) { | ||
const ancestors = getAncestorsForConstructor(constructor); | ||
return ancestors.reduce((pairs, constructor) => { | ||
pairs.push(...getOwnStaticObjectPairs(constructor, propertyName)); | ||
return pairs; | ||
}, []); | ||
} | ||
function getAncestorsForConstructor(constructor) { | ||
const ancestors = []; | ||
while (constructor) { | ||
ancestors.push(constructor); | ||
constructor = Object.getPrototypeOf(constructor); | ||
} | ||
return ancestors.reverse(); | ||
} | ||
function getOwnStaticArrayValues(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return Array.isArray(definition) ? definition : []; | ||
} | ||
function getOwnStaticObjectPairs(constructor, propertyName) { | ||
const definition = constructor[propertyName]; | ||
return definition ? Object.keys(definition).map(key => [key, definition[key]]) : []; | ||
} | ||
const extend = (() => { | ||
function extendWithReflect(constructor) { | ||
function extended() { | ||
return Reflect.construct(constructor, arguments, new.target); | ||
} | ||
extended.prototype = Object.create(constructor.prototype, { | ||
constructor: { | ||
value: extended | ||
} | ||
}); | ||
Reflect.setPrototypeOf(extended, constructor); | ||
return extended; | ||
} | ||
function testReflectExtension() { | ||
const a = function () { | ||
this.a.call(this); | ||
}; | ||
const b = extendWithReflect(a); | ||
b.prototype.a = function () {}; | ||
return new b(); | ||
} | ||
try { | ||
testReflectExtension(); | ||
return extendWithReflect; | ||
} catch (error) { | ||
return constructor => class extended extends constructor {}; | ||
} | ||
})(); | ||
function ClassPropertiesBlessing(constructor) { | ||
const classes = readInheritableStaticArrayValues(constructor, "classes"); | ||
return classes.reduce((properties, classDefinition) => { | ||
return Object.assign(properties, propertiesForClassDefinition(classDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForClassDefinition(key) { | ||
return { | ||
[`${key}Class`]: { | ||
get() { | ||
const { | ||
classes | ||
} = this; | ||
if (classes.has(key)) { | ||
return classes.get(key); | ||
} else { | ||
const attribute = classes.getAttributeName(key); | ||
throw new Error(`Missing attribute "${attribute}"`); | ||
} | ||
} | ||
}, | ||
[`${key}Classes`]: { | ||
get() { | ||
return this.classes.getAll(key); | ||
} | ||
}, | ||
[`has${capitalize(key)}Class`]: { | ||
get() { | ||
return this.classes.has(key); | ||
} | ||
} | ||
}; | ||
} | ||
function TargetPropertiesBlessing(constructor) { | ||
const targets = readInheritableStaticArrayValues(constructor, "targets"); | ||
return targets.reduce((properties, targetDefinition) => { | ||
return Object.assign(properties, propertiesForTargetDefinition(targetDefinition)); | ||
}, {}); | ||
} | ||
function propertiesForTargetDefinition(name) { | ||
return { | ||
[`${name}Target`]: { | ||
get() { | ||
const target = this.targets.find(name); | ||
if (target) { | ||
return target; | ||
} else { | ||
throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`); | ||
} | ||
} | ||
}, | ||
[`${name}Targets`]: { | ||
get() { | ||
return this.targets.findAll(name); | ||
} | ||
}, | ||
[`has${capitalize(name)}Target`]: { | ||
get() { | ||
return this.targets.has(name); | ||
} | ||
} | ||
}; | ||
} | ||
function ValuePropertiesBlessing(constructor) { | ||
const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values"); | ||
const propertyDescriptorMap = { | ||
valueDescriptorMap: { | ||
get() { | ||
return valueDefinitionPairs.reduce((result, valueDefinitionPair) => { | ||
const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair); | ||
const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key); | ||
return Object.assign(result, { | ||
[attributeName]: valueDescriptor | ||
}); | ||
}, {}); | ||
} | ||
} | ||
}; | ||
return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => { | ||
return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair)); | ||
}, propertyDescriptorMap); | ||
} | ||
function propertiesForValueDefinitionPair(valueDefinitionPair) { | ||
const definition = parseValueDefinitionPair(valueDefinitionPair); | ||
const { | ||
key, | ||
name, | ||
reader: read, | ||
writer: write | ||
} = definition; | ||
return { | ||
[name]: { | ||
get() { | ||
const value = this.data.get(key); | ||
if (value !== null) { | ||
return read(value); | ||
} else { | ||
return definition.defaultValue; | ||
} | ||
}, | ||
set(value) { | ||
if (value === undefined) { | ||
this.data.delete(key); | ||
} else { | ||
this.data.set(key, write(value)); | ||
} | ||
} | ||
}, | ||
[`has${capitalize(name)}`]: { | ||
get() { | ||
return this.data.has(key) || definition.hasCustomDefaultValue; | ||
} | ||
} | ||
}; | ||
} | ||
function parseValueDefinitionPair([token, typeDefinition]) { | ||
return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition); | ||
} | ||
function parseValueTypeConstant(constant) { | ||
switch (constant) { | ||
case Array: | ||
return "array"; | ||
case Boolean: | ||
return "boolean"; | ||
case Number: | ||
return "number"; | ||
case Object: | ||
return "object"; | ||
case String: | ||
return "string"; | ||
} | ||
} | ||
function parseValueTypeDefault(defaultValue) { | ||
switch (typeof defaultValue) { | ||
case "boolean": | ||
return "boolean"; | ||
case "number": | ||
return "number"; | ||
case "string": | ||
return "string"; | ||
} | ||
if (Array.isArray(defaultValue)) return "array"; | ||
if (Object.prototype.toString.call(defaultValue) === "[object Object]") return "object"; | ||
} | ||
function parseValueTypeObject(typeObject) { | ||
const typeFromObject = parseValueTypeConstant(typeObject.type); | ||
if (typeFromObject) { | ||
const defaultValueType = parseValueTypeDefault(typeObject.default); | ||
if (typeFromObject !== defaultValueType) { | ||
throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`); | ||
} | ||
return typeFromObject; | ||
} | ||
} | ||
function parseValueTypeDefinition(typeDefinition) { | ||
const typeFromObject = parseValueTypeObject(typeDefinition); | ||
const typeFromDefaultValue = parseValueTypeDefault(typeDefinition); | ||
const typeFromConstant = parseValueTypeConstant(typeDefinition); | ||
const type = typeFromObject || typeFromDefaultValue || typeFromConstant; | ||
if (type) return type; | ||
throw new Error(`Unknown value type "${typeDefinition}"`); | ||
} | ||
function defaultValueForDefinition(typeDefinition) { | ||
const constant = parseValueTypeConstant(typeDefinition); | ||
if (constant) return defaultValuesByType[constant]; | ||
const defaultValue = typeDefinition.default; | ||
if (defaultValue !== undefined) return defaultValue; | ||
return typeDefinition; | ||
} | ||
function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) { | ||
const key = `${dasherize(token)}-value`; | ||
const type = parseValueTypeDefinition(typeDefinition); | ||
return { | ||
type, | ||
key, | ||
name: camelize(key), | ||
get defaultValue() { | ||
return defaultValueForDefinition(typeDefinition); | ||
}, | ||
get hasCustomDefaultValue() { | ||
return parseValueTypeDefault(typeDefinition) !== undefined; | ||
}, | ||
reader: readers[type], | ||
writer: writers[type] || writers.default | ||
}; | ||
} | ||
const defaultValuesByType = { | ||
get array() { | ||
return []; | ||
}, | ||
boolean: false, | ||
number: 0, | ||
get object() { | ||
return {}; | ||
}, | ||
string: "" | ||
}; | ||
const readers = { | ||
array(value) { | ||
const array = JSON.parse(value); | ||
if (!Array.isArray(array)) { | ||
throw new TypeError("Expected array"); | ||
} | ||
return array; | ||
}, | ||
boolean(value) { | ||
return !(value == "0" || value == "false"); | ||
}, | ||
number(value) { | ||
return Number(value); | ||
}, | ||
object(value) { | ||
const object = JSON.parse(value); | ||
if (object === null || typeof object != "object" || Array.isArray(object)) { | ||
throw new TypeError("Expected object"); | ||
} | ||
return object; | ||
}, | ||
string(value) { | ||
return value; | ||
} | ||
}; | ||
const writers = { | ||
default: writeString, | ||
array: writeJSON, | ||
object: writeJSON | ||
}; | ||
function writeJSON(value) { | ||
return JSON.stringify(value); | ||
} | ||
function writeString(value) { | ||
return `${value}`; | ||
} | ||
class Controller { | ||
constructor(context) { | ||
this.context = context; | ||
} | ||
static get shouldLoad() { | ||
return true; | ||
} | ||
get application() { | ||
return this.context.application; | ||
} | ||
get scope() { | ||
return this.context.scope; | ||
} | ||
get element() { | ||
return this.scope.element; | ||
} | ||
get identifier() { | ||
return this.scope.identifier; | ||
} | ||
get targets() { | ||
return this.scope.targets; | ||
} | ||
get classes() { | ||
return this.scope.classes; | ||
} | ||
get data() { | ||
return this.scope.data; | ||
} | ||
initialize() {} | ||
connect() {} | ||
disconnect() {} | ||
dispatch(eventName, { | ||
target = this.element, | ||
detail = {}, | ||
prefix = this.identifier, | ||
bubbles = true, | ||
cancelable = true | ||
} = {}) { | ||
const type = prefix ? `${prefix}:${eventName}` : eventName; | ||
const event = new CustomEvent(type, { | ||
detail, | ||
bubbles, | ||
cancelable | ||
}); | ||
target.dispatchEvent(event); | ||
return event; | ||
} | ||
} | ||
Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing]; | ||
Controller.targets = []; | ||
Controller.values = {}; | ||
const kebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase(); | ||
const capitalize = string => { | ||
const capitalize$1 = string => { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
@@ -260,3 +702,3 @@ }; | ||
if (this[event]) { | ||
const hook = `on${capitalize(event)}`; | ||
const hook = `on${capitalize$1(event)}`; | ||
this.config[hook] = this[event].bind(this); | ||
@@ -377,3 +819,3 @@ } | ||
return StimulusFlatpickr; | ||
}(stimulus.Controller); | ||
}(Controller); | ||
@@ -380,0 +822,0 @@ _defineProperty(StimulusFlatpickr, "targets", ['instance']); |
{ | ||
"name": "stimulus-flatpickr", | ||
"version": "1.4.0", | ||
"version": "3.0.0-0", | ||
"description": "A Stimulus Wrapper for Flatpickr library", | ||
@@ -17,3 +17,3 @@ "keywords": [ | ||
"license": "MIT", | ||
"external": "stimulus, flatpickr", | ||
"external": "@hotwired/stimulus, flatpickr", | ||
"scripts": { | ||
@@ -30,4 +30,3 @@ "start:playground": "webpack-dev-server --mode development --open --config playground/webpack.config.js --hot", | ||
"peerDependencies": { | ||
"flatpickr": ">=4.6.2", | ||
"stimulus": ">=1.1.1" | ||
"flatpickr": ">=4.6.2" | ||
}, | ||
@@ -69,3 +68,3 @@ "devDependencies": { | ||
"mocha": "^5.2.0", | ||
"np": "^5.1.3", | ||
"np": "^7.5.0", | ||
"npm-run-all": "^4.1.3", | ||
@@ -79,3 +78,2 @@ "rollup": "^1.20.3", | ||
"size-plugin": "^1.0.1", | ||
"stimulus": "^1.1.1", | ||
"style-loader": "^0.23.0", | ||
@@ -93,3 +91,6 @@ "webpack": "^4.39.3", | ||
"minimist": "^1.2.5" | ||
}, | ||
"dependencies": { | ||
"@hotwired/stimulus": "^3.0.0" | ||
} | ||
} |
@@ -6,8 +6,8 @@ <h1 align="center">π Stimulus-Flatpickr Wrapper</h1> | ||
</a> | ||
<a href="https://circleci.com/gh/adrienpoly/stimulus-flatpickr" rel="nofollow"> | ||
<img src="https://circleci.com/gh/adrienpoly/stimulus-flatpickr/tree/master.svg?style=svg" alt="CircleCi build status"> | ||
</a> | ||
<a href="https://codecov.io/gh/adrienpoly/stimulus-flatpickr"> | ||
<img src="https://codecov.io/gh/adrienpoly/stimulus-flatpickr/branch/master/graph/badge.svg" alt="Coverage"/> | ||
<a href="https://circleci.com/gh/adrienpoly/stimulus-flatpickr" rel="nofollow"> | ||
<img src="https://circleci.com/gh/adrienpoly/stimulus-flatpickr/tree/master.svg?style=svg" alt="CircleCi build status"> | ||
</a> | ||
<a href="https://codecov.io/gh/adrienpoly/stimulus-flatpickr"> | ||
<img src="https://codecov.io/gh/adrienpoly/stimulus-flatpickr/branch/master/graph/badge.svg" alt="Coverage"/> | ||
</a> | ||
</p> | ||
@@ -76,2 +76,3 @@ | ||
``` | ||
or | ||
@@ -81,4 +82,5 @@ | ||
$ npm i flatpickr | ||
$ npm i stimulus-flatpickr | ||
$ npm i stimulus-flatpickr | ||
``` | ||
Note: Do not use both `yarn` and `npm` to install packages, this might lead to an error: `...It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files` | ||
@@ -96,4 +98,4 @@ | ||
// ./packs/application.js | ||
import { Application } from 'stimulus' | ||
import { definitionsFromContext } from 'stimulus/webpack-helpers' | ||
import { Application } from '@hotwired/stimulus' | ||
import { definitionsFromContext } from '@hotwired/stimulus-webpack-helpers' | ||
@@ -108,3 +110,3 @@ const application = Application.start() | ||
// Import style for flatpickr | ||
require("flatpickr/dist/flatpickr.css") | ||
require('flatpickr/dist/flatpickr.css') | ||
@@ -114,8 +116,9 @@ // Manually register Flatpickr as a stimulus controller | ||
``` | ||
Note: | ||
* **Setup**: By Manually registering Flatpickr controller, you don't need to create a `flatpickr_controller.js` file. However, To add custom behavior you will have to create the `flatpickr_controller.js` file. Read more details about it below. | ||
* **Style**: You can always choose different theme for calender by requiring different `.css` file. You can find them inside your app's root directory `node_modules/flatpickr/dist/themes` | ||
* **Deployment**: In Production environment, include `<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>` in your `application.html.erb` file in order to load the calendar style. | ||
Note: | ||
- **Setup**: By Manually registering Flatpickr controller, you don't need to create a `flatpickr_controller.js` file. However, To add custom behavior you will have to create the `flatpickr_controller.js` file. Read more details about it below. | ||
- **Style**: You can always choose different theme for calender by requiring different `.css` file. You can find them inside your app's root directory `node_modules/flatpickr/dist/themes` | ||
- **Deployment**: In Production environment, include `<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>` in your `application.html.erb` file in order to load the calendar style. | ||
### Using it with Rails | ||
@@ -351,3 +354,3 @@ | ||
If you need to wrap the Flatpickr controller arround custom elements you can use the predefined target `instance` to specify the input element to attach the date picker to. | ||
If you want to display additional information on the calendar, you can wrap the Flatpickr controller arround custom elements. You can use the predefined target `instance` to attach the input element to the date picker. | ||
@@ -358,6 +361,20 @@ Example: | ||
<div data-controller="flatpickr"> | ||
<input type="text" placeholder="Select Date.." data-target="flatpickr.instance" /> | ||
<!-- the flatpicker instance --> | ||
<input type="text" placeholder="Select Date.." data-flatpickr-target="instance" /> | ||
<!-- the custom element --> | ||
<input type="text" data-flatpickr-target="custom" /> | ||
</div> | ||
``` | ||
In the stimulus controller, add the target: | ||
```js | ||
static targets = ['custom'] | ||
yourFunction () { | ||
//... | ||
this.customTarget | ||
} | ||
``` | ||
### Getters | ||
@@ -364,0 +381,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { Controller } from 'stimulus' | ||
import { Controller } from '@hotwired/stimulus' | ||
import flatpickr from 'flatpickr' | ||
@@ -3,0 +3,0 @@ import { kebabCase, capitalize } from './utils' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
359009
48
2275
462
2
+ Added@hotwired/stimulus@^3.0.0
- Removed@hotwired/stimulus-webpack-helpers@1.0.1(transitive)
- Removedstimulus@3.2.2(transitive)