abyme 0.5.1 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/abyme.gemspec CHANGED
@@ -26,6 +26,9 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
28
 
29
+ # SimpleForm dependency is only required for test purposes and optional behaviour
30
+ spec.add_development_dependency 'simple_form'
31
+
29
32
  spec.add_development_dependency "bundler", "~> 2.0"
30
33
  spec.add_development_dependency "rake", "~> 13.0"
31
34
  # Tests
@@ -38,8 +41,9 @@ Gem::Specification.new do |spec|
38
41
 
39
42
  # Dummy app
40
43
  spec.add_development_dependency "sqlite3"
41
- spec.add_development_dependency 'rails'
42
- spec.add_development_dependency 'pry-rails'
44
+ spec.add_development_dependency 'rails', "~> 6.0.3.6"
45
+ spec.add_development_dependency 'pry-byebug'
46
+ spec.add_development_dependency 'byebug'
43
47
  spec.add_development_dependency 'web-console'
44
48
 
45
49
  spec.add_development_dependency 'puma'
data/dist/abyme.js ADDED
@@ -0,0 +1,2 @@
1
+ function e(t,n){return(e=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(t,n)}function t(e,n,i){if(!e.s){if(i instanceof r){if(!i.s)return void(i.o=t.bind(null,e,n));1&n&&(n=i.s),i=i.v}if(i&&i.then)return void i.then(t.bind(null,e,n),t.bind(null,e,2));e.s=n,e.v=i;var o=e.o;o&&o(e)}}var n=function(n){var o,a;function s(){return n.apply(this,arguments)||this}a=n,(o=s).prototype=Object.create(a.prototype),o.prototype.constructor=o,e(o,a);var c,u=s.prototype;return u.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},u.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},u.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},u.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},u.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},u.abymeBeforeAdd=function(e){},u.abymeAfterAdd=function(e){},u.abymeBeforeRemove=function(e){},u.abymeAfterRemove=function(e){},u.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},u.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},u.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},u.add_default_associations=function(){try{var e=this,n=0,o=function(e,n,o){for(var a;;){var s=e();if(i(s)&&(s=s.v),!s)return c;if(s.then){a=0;break}var c=o();if(c&&c.then){if(!i(c)){a=1;break}c=c.s}}var u=new r,f=t.bind(null,u,2);return(0===a?s.then(h):1===a?c.then(d):(void 0).then(function(){(s=e())?s.then?s.then(h).then(void 0,f):h(s):t(u,1,c)})).then(void 0,f),u;function d(n){c=n;do{if(!(s=e())||i(s)&&!s.v)return void t(u,1,c);if(s.then)return void s.then(h).then(void 0,f);i(c=o())&&(c=c.v)}while(!c||!c.then);c.then(d).then(void 0,f)}function h(e){e?(c=o())&&c.then?c.then(d).then(void 0,f):d(c):t(u,1,c)}}(function(){return n<e.count},0,function(){return e.add_association(),n++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},u.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(c=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(s.prototype,c),s}(require("stimulus").Controller),r=function(){function e(){}return e.prototype.then=function(n,r){var i=new e,o=this.s;if(o){var a=1&o?n:r;if(a){try{t(i,1,a(this.v))}catch(e){t(i,2,e)}return i}return this}return this.o=function(e){try{var o=e.v;1&e.s?t(i,1,n?n(o):o):r?t(i,1,r(o)):t(i,2,o)}catch(e){t(i,2,e)}},i},e}();function i(e){return e instanceof r&&1&e.s}n.targets=["template","associations","fields","newFields"],exports.AbymeController=n;
2
+ //# sourceMappingURL=abyme.js.map
data/dist/abyme.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"4FAuCO,SAASA,EAAQC,EAAMC,EAAOC,GACpC,IAAKF,EAAKG,EAAG,CACZ,GAAID,aAAiBE,EAAO,CAC3B,IAAIF,EAAMC,EAOT,YADAD,EAAMG,EAAIN,EAAQO,KAAK,KAAMN,EAAMC,IALvB,EAARA,IACHA,EAAQC,EAAMC,GAEfD,EAAQA,EAAMK,EAMhB,GAAIL,GAASA,EAAMM,KAElB,YADAN,EAAMM,KAAKT,EAAQO,KAAK,KAAMN,EAAMC,GAAQF,EAAQO,KAAK,KAAMN,EAAM,IAGtEA,EAAKG,EAAIF,EACTD,EAAKO,EAAIL,EACT,IAAMO,EAAWT,EAAKK,EAClBI,GACHA,EAAST,6LAnDVU,QAAA,WACEC,QAAQC,IAAI,mBAERC,KAAKC,OAIPD,KAAKE,8BAoCTC,gBAAA,SAAgBC,GAKd,GAJIA,GACFA,EAAMC,iBAGJL,KAAKM,QAAQC,QAAQC,OAASR,KAAKS,cAErC,OADAT,KAAKU,aAAa,oBAIpB,IAAMC,EAAOX,KAAKY,aAClBZ,KAAKU,aAAa,cAClBV,KAAKa,mBAAmBC,mBAAmBd,KAAKe,SAAUJ,GAC1DX,KAAKU,aAAa,gBAepBM,mBAAA,SAAmBZ,GACjBA,EAAMC,iBAENL,KAAKU,aAAa,iBAClBV,KAAKiB,iBAAiBb,GACtBJ,KAAKU,aAAa,mBAWpBA,aAAA,SAAaQ,EAAOP,YAAAA,IAAAA,EAAO,MACzB,IAAMP,EAAQ,IAAIe,qBAAqBD,EAAS,CAAEE,OAAQ,CAACC,WAAYrB,KAAMsB,QAASX,KACtFX,KAAKM,QAAQiB,cAAcnB,GAE3BJ,KAAKwB,SAASpB,EAAOc,MAIvBM,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C,EAC3C,GAAIC,EAAU,CACb,IACCrF,EAAQqE,EAAQ,EAAGgB,EAASvE,KAAKN,IAChC,MAAO8E,GACRtF,EAAQqE,EAAQ,EAAGiB,GAEpB,OAAOjB,EAEP,YAiBF,OAdAvD,KAAKR,EAAI,SAASiF,GACjB,IACC,IAAMpF,EAAQoF,EAAM/E,EACN,EAAV+E,EAAMnF,EACTJ,EAAQqE,EAAQ,EAAGc,EAAcA,EAAYhF,GAASA,GAC5CiF,EACVpF,EAAQqE,EAAQ,EAAGe,EAAWjF,IAE9BH,EAAQqE,EAAQ,EAAGlE,GAEnB,MAAOmF,GACRtF,EAAQqE,EAAQ,EAAGiB,KAGdjB,KAhC0B,GAgE5B,WAAwBmB,GAC9B,OAAOA,gBAA0C,EAAbA,EAASpF,IA3DrCqF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import{Controller as e}from"stimulus";class t extends e{connect(){console.log("Abyme Connected"),this.count&&this.add_default_associations()}get count(){return this.element.dataset.minCount||0}get position(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}add_association(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;const t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")}remove_association(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")}create_event(e,t=null){const a=new CustomEvent(`abyme:${e}`,{detail:{controller:this,content:t}});this.element.dispatchEvent(a),this.dispatch(a,e)}dispatch(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)}abymeBeforeAdd(e){}abymeAfterAdd(e){}abymeBeforeRemove(e){}abymeAfterRemove(e){}build_html(){let e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){const t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e}mark_for_destroy(e){let t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")}limit_check(){return this.newFieldsTargets.filter(e=>!e.classList.contains("abyme--marked-for-destroy")).length>=parseInt(this.element.dataset.limit)}async add_default_associations(){let e=0;for(;e<this.count;)this.add_association(),e++,await this.sleep(1)}sleep(e){return new Promise(t=>setTimeout(t,e))}}t.targets=["template","associations","fields","newFields"];export{t as AbymeController};
2
+ //# sourceMappingURL=abyme.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.modern.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["Controller","connect","console","log","this","count","add_default_associations","element","dataset","minCount","position","associationsTarget","abymePosition","add_association","event","preventDefault","limit","limit_check","create_event","html","build_html","insertAdjacentHTML","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","value","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","[object Object]","i","sleep","ms","Promise","resolve","setTimeout","targets"],"mappings":"sDAE6BA,EAO3BC,UACEC,QAAQC,IAAI,mBAERC,KAAKC,OAIPD,KAAKE,2BAMAD,YACP,YAAYE,QAAQC,QA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KG,cAAc,2BAA2BC,MAAQ,EACtDJ,EAAKK,MAAMC,QAAU,OACrBN,EAAKO,UAAUC,IAAI,6BAUrBjC,cACE,YAAakC,iBACAC,OAAOV,IAASA,EAAKO,UAAUI,SAAS,8BAA+BC,QACrEC,SAASnD,KAAKG,QAAQC,QAAQQ,OAQjBwC,iCAC5B,IAAIC,EAAI,EACR,KAAOA,EAAIrD,KAAKC,OACdD,KAAKS,kBACL4C,eACWC,MAAM,GAIrBA,MAAMC,GACJ,WAAWC,QAAQC,GAAWC,WAAWD,EAASF,OAxL7CI,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import{Controller as e}from"stimulus";function t(e,n){return(t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,n)}function n(e,t,r){if(!e.s){if(r instanceof i){if(!r.s)return void(r.o=n.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(n.bind(null,e,t),n.bind(null,e,2));e.s=t,e.v=r;var o=e.o;o&&o(e)}}var r=function(e){var r,a;function s(){return e.apply(this,arguments)||this}a=e,(r=s).prototype=Object.create(a.prototype),r.prototype.constructor=r,t(r,a);var c,f=s.prototype;return f.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},f.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},f.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},f.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},f.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},f.abymeBeforeAdd=function(e){},f.abymeAfterAdd=function(e){},f.abymeBeforeRemove=function(e){},f.abymeAfterRemove=function(e){},f.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},f.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},f.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},f.add_default_associations=function(){try{var e=this,t=0,r=function(e,t,r){for(var a;;){var s=e();if(o(s)&&(s=s.v),!s)return c;if(s.then){a=0;break}var c=r();if(c&&c.then){if(!o(c)){a=1;break}c=c.s}}var f=new i,u=n.bind(null,f,2);return(0===a?s.then(h):1===a?c.then(d):(void 0).then(function(){(s=e())?s.then?s.then(h).then(void 0,u):h(s):n(f,1,c)})).then(void 0,u),f;function d(t){c=t;do{if(!(s=e())||o(s)&&!s.v)return void n(f,1,c);if(s.then)return void s.then(h).then(void 0,u);o(c=r())&&(c=c.v)}while(!c||!c.then);c.then(d).then(void 0,u)}function h(e){e?(c=r())&&c.then?c.then(d).then(void 0,u):d(c):n(f,1,c)}}(function(){return t<e.count},0,function(){return e.add_association(),t++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(c=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(s.prototype,c),s}(e),i=function(){function e(){}return e.prototype.then=function(t,r){var i=new e,o=this.s;if(o){var a=1&o?t:r;if(a){try{n(i,1,a(this.v))}catch(e){n(i,2,e)}return i}return this}return this.o=function(e){try{var o=e.v;1&e.s?n(i,1,t?t(o):o):r?n(i,1,r(o)):n(i,2,o)}catch(e){n(i,2,e)}},i},e}();function o(e){return e instanceof i&&1&e.s}r.targets=["template","associations","fields","newFields"];export{r as AbymeController};
2
+ //# sourceMappingURL=abyme.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.module.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","Controller","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"kIAuCO,SAASA,EAAQC,EAAMC,EAAOC,GACpC,IAAKF,EAAKG,EAAG,CACZ,GAAID,a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AAQ,EAAGe,EAAcA,EAAYjF,GAASA,GAC5CkF,EACVrF,EAAQqE,EAAQ,EAAGgB,EAAWlF,IAE9BH,EAAQqE,EAAQ,EAAGlE,GAEnB,MAAOoF,GACRvF,EAAQqE,EAAQ,EAAGkB,KAGdlB,KAhC0B,GAgE5B,WAAwBoB,GAC9B,OAAOA,gBAA0C,EAAbA,EAASrF,IA3DrCsF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
data/dist/abyme.umd.js ADDED
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("stimulus")):"function"==typeof define&&define.amd?define(["exports","stimulus"],t):t((e||self).abyme={},e.Stimulus)}(this,function(e,t){function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t,n){if(!e.s){if(n instanceof o){if(!n.s)return void(n.o=i.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(i.bind(null,e,t),i.bind(null,e,2));e.s=t,e.v=n;var r=e.o;r&&r(e)}}var r=function(e){var t,r;function s(){return e.apply(this,arguments)||this}r=e,(t=s).prototype=Object.create(r.prototype),t.prototype.constructor=t,n(t,r);var f,u=s.prototype;return u.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},u.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},u.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},u.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},u.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},u.abymeBeforeAdd=function(e){},u.abymeAfterAdd=function(e){},u.abymeBeforeRemove=function(e){},u.abymeAfterRemove=function(e){},u.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},u.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},u.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},u.add_default_associations=function(){try{var e=this,t=0,n=function(e,t,n){for(var r;;){var s=e();if(a(s)&&(s=s.v),!s)return f;if(s.then){r=0;break}var f=n();if(f&&f.then){if(!a(f)){r=1;break}f=f.s}}var u=new o,c=i.bind(null,u,2);return(0===r?s.then(l):1===r?f.then(d):(void 0).then(function(){(s=e())?s.then?s.then(l).then(void 0,c):l(s):i(u,1,f)})).then(void 0,c),u;function d(t){f=t;do{if(!(s=e())||a(s)&&!s.v)return void i(u,1,f);if(s.then)return void s.then(l).then(void 0,c);a(f=n())&&(f=f.v)}while(!f||!f.then);f.then(d).then(void 0,c)}function l(e){e?(f=n())&&f.then?f.then(d).then(void 0,c):d(f):i(u,1,f)}}(function(){return t<e.count},0,function(){return e.add_association(),t++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},u.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(f=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}(s.prototype,f),s}(t.Controller),o=function(){function e(){}return e.prototype.then=function(t,n){var r=new e,o=this.s;if(o){var a=1&o?t:n;if(a){try{i(r,1,a(this.v))}catch(e){i(r,2,e)}return r}return this}return this.o=function(e){try{var o=e.v;1&e.s?i(r,1,t?t(o):o):n?i(r,1,n(o)):i(r,2,o)}catch(e){i(r,2,e)}},r},e}();function a(e){return e instanceof o&&1&e.s}r.targets=["template","associations","fields","newFields"],e.AbymeController=r});
2
+ //# sourceMappingURL=abyme.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.umd.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","Controller","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"uWAuCO,SAASA,EAAQC,EAAMC,EAAOC,GACpC,IAAKF,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F,EAAMpF,EACTJ,EAAQqE,EAAQ,EAAGe,EAAcA,EAAYjF,GAASA,GAC5CkF,EACVrF,EAAQqE,EAAQ,EAAGgB,EAAWlF,IAE9BH,EAAQqE,EAAQ,EAAGlE,GAEnB,MAAOoF,GACRvF,EAAQqE,EAAQ,EAAGkB,KAGdlB,KAhC0B,GAgE5B,WAAwBoB,GAC9B,OAAOA,gBAA0C,EAAbA,EAASrF,IA3DrCsF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import '@babel/polyfill'
2
+ import 'mutationobserver-shim'
@@ -2,11 +2,11 @@ module Abyme
2
2
  class AbymeBuilder < ActionView::Base
3
3
  include ActionView
4
4
 
5
- # If a block is given to the #abymize helper
6
- # it will instanciate a new AbymeBuilder
5
+ # If a block is given to the #abymize helper
6
+ # it will instanciate a new AbymeBuilder
7
7
  # and pass to it the association name (Symbol)
8
8
  # the form object, lookup_context optionaly a partial path
9
- # then yield itself to the block
9
+ # then yield itself to the block
10
10
 
11
11
  def initialize(association:, form:, context:, partial:, &block)
12
12
  @association = association
@@ -14,14 +14,14 @@ module Abyme
14
14
  @context = context
15
15
  @lookup_context = context.lookup_context
16
16
  @partial = partial
17
- yield(self) if block_given?
17
+ yield(self) if block
18
18
  end
19
19
 
20
20
  # RECORDS
21
21
 
22
22
  # calls the #persisted_records_for helper method
23
23
  # passing association, form and options to it
24
-
24
+
25
25
  def records(options = {})
26
26
  persisted_records_for(@association, @form, options) do |fields_for_association|
27
27
  render_association_partial(@association, fields_for_association, @partial, @context)
@@ -32,12 +32,10 @@ module Abyme
32
32
 
33
33
  # calls the #new_records_for helper method
34
34
  # passing association, form and options to it
35
-
36
35
  def new_records(options = {}, &block)
37
36
  new_records_for(@association, @form, options) do |fields_for_association|
38
37
  render_association_partial(@association, fields_for_association, @partial, @context)
39
38
  end
40
39
  end
41
-
42
40
  end
43
- end
41
+ end
data/lib/abyme/model.rb CHANGED
@@ -1,52 +1,52 @@
1
1
  module Abyme
2
2
  module Model
3
3
  module ClassMethods
4
- def abymize(association, permit: nil, reject: nil, **options)
4
+ def abymize(association, permit: nil, reject: nil, class_name: nil, **options)
5
5
  default_options = {reject_if: :all_blank, allow_destroy: true}
6
6
  nested_attributes_options = default_options.merge(options)
7
7
  accepts_nested_attributes_for association, nested_attributes_options
8
8
  # Save allow_destroy value for this model/association for later
9
9
  save_destroy_option(association, nested_attributes_options[:allow_destroy])
10
- Abyme::Model.permit_attributes(self.name, association, permit || reject, permit.present?) if permit.present? || reject.present?
10
+ Abyme::Model.permit_attributes(name, association, permit || reject, permit.present?, class_name) if permit.present? || reject.present?
11
11
  end
12
12
 
13
- alias :abyme_for :abymize
13
+ alias_method :abyme_for, :abymize
14
14
 
15
15
  def abyme_attributes
16
- Abyme::Model.instance_variable_get(:@permitted_attributes)[self.name]
16
+ Abyme::Model.instance_variable_get(:@permitted_attributes)[name]
17
17
  end
18
18
 
19
19
  private
20
20
 
21
21
  def save_destroy_option(association, value)
22
- Abyme::Model.instance_variable_get(:@allow_destroy)[self.name][association] = value
22
+ Abyme::Model.instance_variable_get(:@allow_destroy)[name][association] = value
23
23
  end
24
24
  end
25
25
 
26
26
  @permitted_attributes ||= {}
27
- @allow_destroy ||= {}
27
+ @allow_destroy ||= {}
28
28
 
29
29
  attr_accessor :allow_destroy
30
- attr_reader :permitted_attributes
30
+ attr_reader :permitted_attributes
31
31
 
32
- def self.permit_attributes(class_name, association, attributes, permit)
33
- @permitted_attributes[class_name]["#{association}_attributes".to_sym] = AttributesBuilder.new(class_name, association, attributes, permit)
34
- .build_attributes
32
+ def self.permit_attributes(class_name, association, attributes, permit, association_class_name = nil)
33
+ @permitted_attributes[class_name]["#{association}_attributes".to_sym] = AttributesBuilder.new(class_name, association, attributes, permit, association_class_name)
34
+ .build_attributes
35
35
  end
36
36
 
37
37
  def self.included(klass)
38
38
  @permitted_attributes[klass.name] ||= {}
39
- @allow_destroy[klass.name] ||= {}
39
+ @allow_destroy[klass.name] ||= {}
40
40
  klass.extend ClassMethods
41
41
  end
42
42
 
43
43
  class AttributesBuilder
44
- def initialize(model, association, attributes, permit = true)
44
+ def initialize(model, association, attributes, permit = true, association_class_name = nil)
45
45
  @model = model
46
46
  @association = association
47
47
  @attributes_list = attributes
48
48
  @permit = permit
49
- @association_class = @association.to_s.classify.constantize
49
+ @association_class = association_class_name&.safe_constantize || @association.to_s.classify.constantize
50
50
  end
51
51
 
52
52
  def build_attributes
@@ -55,7 +55,7 @@ module Abyme
55
55
  if @permit && @attributes_list == :all_attributes
56
56
  authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
57
57
  elsif @permit
58
- @attributes_list << nested_attributes unless (nested_attributes.blank? || @attributes_list.include?(nested_attributes))
58
+ @attributes_list << nested_attributes unless nested_attributes.blank? || @attributes_list.include?(nested_attributes)
59
59
  authorized_attributes += @attributes_list
60
60
  else
61
61
  authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
@@ -74,7 +74,7 @@ module Abyme
74
74
 
75
75
  def build_all_attributes(authorized_attributes, nested_attributes)
76
76
  authorized_attributes += add_all_attributes
77
- authorized_attributes << nested_attributes unless (nested_attributes.blank? || authorized_attributes.include?(nested_attributes))
77
+ authorized_attributes << nested_attributes unless nested_attributes.blank? || authorized_attributes.include?(nested_attributes)
78
78
  authorized_attributes
79
79
  end
80
80
 
@@ -85,4 +85,4 @@ module Abyme
85
85
  end
86
86
  end
87
87
  end
88
- end
88
+ end
data/lib/abyme/version.rb CHANGED
@@ -2,8 +2,8 @@
2
2
  module Abyme
3
3
  module VERSION
4
4
  MAJOR = 0
5
- MINOR = 5
6
- PATCH = 1
5
+ MINOR = 6
6
+ PATCH = 4
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH].join(".")
9
9
  end
@@ -2,7 +2,6 @@ require_relative "abyme_builder"
2
2
 
3
3
  module Abyme
4
4
  module ViewHelpers
5
-
6
5
  # ABYME_FOR
7
6
 
8
7
  # this helper will generate the top level wrapper markup
@@ -23,7 +22,7 @@ module Abyme
23
22
  # set the default number of blank fields to display
24
23
 
25
24
  # - partial (String)
26
- # to customize the partial path by default #abyme_for will expect
25
+ # to customize the partial path by default #abyme_for will expect
27
26
  # a partial to bbe present in views/abyme
28
27
 
29
28
  # - Exemple
@@ -39,22 +38,24 @@ module Abyme
39
38
  # </div>
40
39
 
41
40
  def abyme_for(association, form, options = {}, &block)
42
- content_tag(:div, data: { controller: 'abyme', limit: options[:limit], min_count: options[:min_count] }, id: "abyme--#{association}") do
43
- if block_given?
44
- yield(Abyme::AbymeBuilder.new(
45
- association: association, form: form, context: self, partial: options[:partial]
41
+ content_tag(:div, data: {controller: "abyme", limit: options[:limit], min_count: options[:min_count]}, id: "abyme--#{association}") do
42
+ if block
43
+ yield(
44
+ Abyme::AbymeBuilder.new(
45
+ association: association, form: form, context: self, partial: options[:partial]
46
46
  )
47
47
  )
48
48
  else
49
- model = association.to_s.singularize.classify.constantize
49
+ # model = association.to_s.singularize.classify.constantize
50
+ model = association.to_s.singularize
50
51
  concat(persisted_records_for(association, form, options))
51
- concat(new_records_for(association, form, options))
52
+ concat(new_records_for(association, form, options))
52
53
  concat(add_associated_record(content: options[:button_text] || "Add #{model}"))
53
54
  end
54
55
  end
55
56
  end
56
57
 
57
- alias :abymize :abyme_for
58
+ alias_method :abymize, :abyme_for
58
59
 
59
60
  # NEW_RECORDS_FOR
60
61
 
@@ -72,9 +73,9 @@ module Abyme
72
73
  # will output this html
73
74
 
74
75
  # <div data-target="abyme.associations" data-association="tasks" data-abyme-position="end">
75
- # <template class="abyme--task_template" data-target="abyme.template">
76
+ # <template class="abyme--task_template" data-target="abyme.template">
76
77
  # <div data-target="abyme.fields abyme.newFields" class="abyme--fields task-fields">
77
- # ... partial html goes here
78
+ # ... partial html goes here
78
79
  # </div>
79
80
  # </template>
80
81
  # ... new rendered fields goes here
@@ -82,41 +83,41 @@ module Abyme
82
83
 
83
84
  # == Options
84
85
  # - position (:start, :end)
85
- # allows you to specify whether new fields added dynamically
86
- # should go at the top or at the bottom
86
+ # allows you to specify whether new fields added dynamically
87
+ # should go at the top or at the bottom
87
88
  # :end is the default value
88
89
 
89
90
  # - partial (String)
90
- # to customize the partial path by default #abyme_for will expect
91
+ # to customize the partial path by default #abyme_for will expect
91
92
  # a partial to bbe present in views/abyme
92
93
 
93
94
  # - fields_html (Hash)
94
95
  # allows you to pass any html attributes to each fields wrapper
95
96
 
96
97
  # - wrapper_html (Hash)
97
- # allows you to pass any html attributes to the the html element
98
+ # allows you to pass any html attributes to the the html element
98
99
  # wrapping all the fields
99
100
 
100
101
  def new_records_for(association, form, options = {}, &block)
101
102
  options[:wrapper_html] ||= {}
102
103
 
103
- wrapper_default = {
104
- data: {
105
- abyme_target: 'associations',
106
- association: association,
107
- abyme_position: options[:position] || :end
108
- }
104
+ wrapper_default = {
105
+ data: {
106
+ abyme_target: "associations",
107
+ association: association,
108
+ abyme_position: options[:position] || :end
109
+ }
109
110
  }
110
111
 
111
- fields_default = { data: { target: 'abyme.fields abyme.newFields' } }
112
+ fields_default = {data: {target: "abyme.fields abyme.newFields"}}
112
113
 
113
114
  content_tag(:div, build_attributes(wrapper_default, options[:wrapper_html])) do
114
- content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: { abyme_target: 'template' }) do
115
- form.fields_for association, association.to_s.classify.constantize.new, child_index: 'NEW_RECORD' do |f|
115
+ content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: {abyme_target: "template"}) do
116
+ fields_for_builder form, association, association.to_s.classify.constantize.new, child_index: "NEW_RECORD" do |f|
116
117
  content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
117
118
  # Here, if a block is passed, we're passing the association fields to it, rather than the form itself
118
119
  # block_given? ? yield(f) : render(options[:partial] || "abyme/#{association.to_s.singularize}_fields", f: f)
119
- block_given? ? yield(f) : render_association_partial(association, f, options[:partial])
120
+ block ? yield(f) : render_association_partial(association, f, options[:partial])
120
121
  end
121
122
  end
122
123
  end
@@ -154,21 +155,23 @@ module Abyme
154
155
  # ex: order: { created_at: :desc }
155
156
 
156
157
  # - partial (String)
157
- # to customize the partial path by default #abyme_for will expect
158
+ # to customize the partial path by default #abyme_for will expect
158
159
  # a partial to bbe present in views/abyme
159
160
 
160
161
  # - fields_html (Hash)
161
162
  # allows you to pass any html attributes to each fields wrapper
162
163
 
163
164
  # - wrapper_html (Hash)
164
- # allows you to pass any html attributes to the the html element
165
+ # allows you to pass any html attributes to the the html element
165
166
  # wrapping all the fields
166
-
167
+
167
168
  def persisted_records_for(association, form, options = {})
168
169
  records = options[:collection] || form.object.send(association)
170
+ # return if records.empty?
171
+
169
172
  options[:wrapper_html] ||= {}
170
- fields_default = { data: { abyme_target: 'fields' } }
171
-
173
+ fields_default = {data: {abyme_target: "fields"}}
174
+
172
175
  if options[:order].present?
173
176
  records = records.order(options[:order])
174
177
  # by calling the order method on the AR collection
@@ -176,10 +179,10 @@ module Abyme
176
179
  # so we have to get them back with the 2 lines below
177
180
  invalids = form.object.send(association).reject(&:persisted?)
178
181
  records = records.to_a.concat(invalids) if invalids.any?
179
- end
180
-
182
+ end
183
+
181
184
  content_tag(:div, options[:wrapper_html]) do
182
- form.fields_for(association, records) do |f|
185
+ fields_for_builder(form, association, records) do |f|
183
186
  content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
184
187
  block_given? ? yield(f) : render_association_partial(association, f, options[:partial])
185
188
  end
@@ -189,27 +192,40 @@ module Abyme
189
192
 
190
193
  # ADD & REMOVE ASSOCIATION
191
194
 
192
- # these helpers will call the #create_button method
195
+ # these helpers will call the #create_button method
193
196
  # to generate the buttons for add and remove associations
194
197
  # with the right action and a default content text for each button
195
-
198
+
196
199
  def add_associated_record(options = {}, &block)
197
- action = 'click->abyme#add_association'
198
- options[:content] ||= 'Add Association'
200
+ action = "click->abyme#add_association"
201
+ options[:content] ||= "Add Association"
199
202
  create_button(action, options, &block)
200
203
  end
201
-
204
+
202
205
  def remove_associated_record(options = {}, &block)
203
- action = 'click->abyme#remove_association'
204
- options[:content] ||= 'Remove Association'
206
+ action = "click->abyme#remove_association"
207
+ options[:content] ||= "Remove Association"
205
208
  create_button(action, options, &block)
206
209
  end
207
210
 
208
- alias :add_association :add_associated_record
209
- alias :remove_association :remove_associated_record
211
+ alias_method :add_association, :add_associated_record
212
+ alias_method :remove_association, :remove_associated_record
210
213
 
211
214
  private
212
215
 
216
+ # FORM BUILDER SELECTION
217
+
218
+ # If form builder inherits from SimpleForm, we should use its fields_for helper to keep the wrapper options
219
+ # :nocov:
220
+ def fields_for_builder(form, association, records, options = {}, &block)
221
+ if defined?(SimpleForm) && form.instance_of?(SimpleForm::FormBuilder)
222
+ form.simple_fields_for(association, records, options, &block)
223
+ else
224
+ form.fields_for(association, records, options, &block)
225
+ end
226
+ end
227
+ # :nocov:
228
+
213
229
  # CREATE_BUTTON
214
230
 
215
231
  # this helper is call by either add_associated_record or remove_associated_record
@@ -225,17 +241,17 @@ module Abyme
225
241
 
226
242
  # - html (Hash)
227
243
  # to pass any html attributes you want.
228
-
244
+
229
245
  def create_button(action, options, &block)
230
246
  options[:html] ||= {}
231
247
  options[:tag] ||= :button
232
-
233
- if block_given?
234
- content_tag(options[:tag], { data: { action: action } }.merge(options[:html])) do
248
+
249
+ if block
250
+ content_tag(options[:tag], {data: {action: action}}.merge(options[:html])) do
235
251
  capture(&block)
236
252
  end
237
253
  else
238
- content_tag(options[:tag], options[:content], { data: { action: action } }.merge(options[:html]))
254
+ content_tag(options[:tag], options[:content], {data: {action: action}}.merge(options[:html]))
239
255
  end
240
256
  end
241
257
 
@@ -246,7 +262,7 @@ module Abyme
246
262
 
247
263
  def basic_fields_markup(html, association = nil)
248
264
  if html && html[:class]
249
- html[:class] = "abyme--fields #{association.to_s.singularize}-fields #{html[:class]}"
265
+ html[:class] = "abyme--fields #{association.to_s.singularize}-fields #{html[:class]}"
250
266
  else
251
267
  html ||= {}
252
268
  html[:class] = "abyme--fields #{association.to_s.singularize}-fields"
@@ -274,13 +290,12 @@ module Abyme
274
290
 
275
291
  # RENDER PARTIAL
276
292
 
277
- # renders a partial based on the passed path, or will expect a partial to be found in the views/abyme directory.
293
+ # renders a partial based on the passed path, or will expect a partial to be found in the views/abyme directory.
278
294
 
279
295
  def render_association_partial(association, form, partial = nil, context = nil)
280
- partial_path = partial ||"abyme/#{association.to_s.singularize}_fields"
296
+ partial_path = partial || "abyme/#{association.to_s.singularize}_fields"
281
297
  context ||= self
282
298
  context.render(partial: partial_path, locals: {f: form})
283
299
  end
284
-
285
300
  end
286
- end
301
+ end