@hammerstone/refine-stimulus
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -1,2 +0,2 @@ | ||
import{Controller as t}from"@hotwired/stimulus";import{delegate as e,abnegate as i}from"jquery-events-to-dom-events";import{debounce as n}from"lodash";function r(t){const e=(t.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(e)return e.replace(/_/g,"-").replace(/\//g,"--")}class s extends t{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}addHiddenInput(t,e){const i=document.createElement("input");return i.type="hidden",i.name=t,i.value=e||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class a extends s{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}a.values={previousCriterionId:Number};class l extends t{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}}l.values={criterionId:Number,input:Object};class o extends s{criterion(){const{state:t,criterionIdValue:e}=this;t.deleteCriterion(e),this.startUpdate()}}o.values={criterionId:Number};class d extends t{connect(){const t=new URLSearchParams(window.location.search);this.existingParams=t,this.existingParams.delete("stable_id")}search(t){t.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const t=this,{blueprint:e}=t.stateController;return Promise.resolve(t.stabilizeFilterController.validateBlueprint(e)).then(function(i){i.stableId?t.redirectToStableId(i.stableId):t.fetchAndRenderInvalidFilter(e)})}catch(t){return Promise.reject(t)}}addHiddenInput(t){let{name:e,value:i}=t;const n=document.createElement("input");n.type="hidden",n.name=e,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(t){const e=new URLSearchParams;t&&e.append("stable_id",t);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(e)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(t){try{var e;const i=this;return Promise.resolve(fetch(i.submitUrlValue,{headers:{Accept:"application/json","Content-Type":"application/json","X-CSRF-Token":null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content},method:"POST",body:JSON.stringify({filter:i.stateController.filterName,blueprint:JSON.stringify(t),id_suffix:i.stateController.idSuffix})})).then(function(t){return Promise.resolve(t.json()).then(function(t){document.getElementById(t.target).outerHTML=t.template})})}catch(t){return Promise.reject(t)}}}d.values={submitUrl:String},d.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function t(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),i}t.prototype=window.Event.prototype,window.CustomEvent=t}();const u=(t,e,i,n)=>{const r=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:e,filterName:i,initialLoad:n}});t.dispatchEvent(r)},c=(t,e)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});t.dispatchEvent(i)},h=(t,e,i)=>{const{meta:n,refinements:r,component:s}=i,{clauses:a,options:l}=n;let o={clause:a[0].id,selected:"option-condition"===s?[l[0].id]:void 0};return r.forEach(t=>{var e;const{meta:i,component:n}=t,{clauses:r,options:s}=i;o[t.id]={clause:r[0].id,selected:"option-condition"===n?[null==(e=s[0])?void 0:e.id]:void 0}}),{depth:e,type:"criterion",condition_id:t,input:o}};class p extends t{connect(){this.element.refineStateController=this,this.changeDelegate=e("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((t,e)=>(t[e.id]=e,t),{}),this.loadingTimeout=null,c(this.element,this.blueprint)}disconnect(){i("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(t){return this.conditionsLookup[t]}updateStableId(t){t!==this.stableId&&(this.stableId=t)}addGroup(){const{conditions:t}=this,e=t[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(h(e.id,1,e)),c(this.element,this.blueprint)}addCriterion(t){const{blueprint:e,conditions:i}=this,n=i[0];var r;e.splice(t+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},h(n.id,1,n)),c(this.element,this.blueprint)}deleteCriterion(t){const{blueprint:e}=this,i=e[t-1],n=e[t+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,a=r||!i,l=a&&s;i||n?e.splice(l&&r?t-1:l&&!i||a&&!s?t:t-1,2):this.blueprint=[],c(this.element,this.blueprint)}replaceCriterion(t,e,i){const n=this.blueprint[t];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(h));this.blueprint[t]=h(e,n.depth,i),c(this.element,this.blueprint)}updateInput(t,e,i){const{blueprint:n}=this,r=n[t],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...e}:r[i]={...r[i],...e},c(this.element,this.blueprint)}}p.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},p.targets=["loading"];class b extends t{connect(){this.idValue&&(t=>{const e=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(e)})()}updateStableIdField(t){if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:e}=t,{stableId:i}=e;this.stableIdFieldTarget.value=i}}activateSaveLink(t){const{detail:e}=t,{stableId:i,initialLoad:n}=e;if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget&&!n){const t=new URL(this.enabledSaveLinkTarget.href);t.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",t),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}b.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],b.values={id:Number,stableId:String,filterName:String};class m extends t{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(t){try{const e=this;(t=>{const e=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});window.dispatchEvent(e)})(e.blueprint);const i=t.detail.blueprint;return Promise.resolve(e.validateBlueprint(i)).then(function(t){if(t.stableId)e.stableIdValue=t.stableId,u(e.element,e.stableIdValue,e.filterNameValue),u(window,e.stableIdValue,e.filterNameValue);else{const{errors:e}=t;(t=>{let{blueprint:e,errors:i}=t;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:e,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:e})}})}catch(t){return Promise.reject(t)}}validateBlueprint(t){try{var e;const i=this;let n=JSON.stringify({blueprint:t,filter:i.filterNameValue}),r=null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(t){return Promise.resolve(t.json()).then(function(e){return t.ok?{stableId:e.filter_id}:{errors:e.errors}})})}catch(t){return Promise.reject(t)}}}m.values={stableId:String,updateStableIdUrl:String,filterName:String},m.targets=[];class f extends s{initialize(){this.updateBlueprint=n((t,e,i)=>{this.value(t,e,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){s.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{id:t.target.value},t.target.dataset.inputId),this.submitForm()}clause(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{clause:t.target.value},t.target.dataset.inputId),this.submitForm()}selected(t){const{target:e}=t,i=Array.prototype.slice.call(e.options).filter(t=>t.selected).map(t=>t.value);this.value(t,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(t,e,i){const{criterionIdValue:n,state:r}=this,s=t.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:e=e||t.target.value},s.inputId)}date(t){const{picker:e}=t.detail,i=e.startDate.format("YYYY-MM-DD");this.value(t,i),this.submitForm()}createStableId(t,e){var i;const{state:n}=this;let r=JSON.stringify({blueprint:t,filter:e}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(t){n.updateStableId(t.filter_id)}})}condition(t){const{criterionIdValue:e,state:i}=this,n=t.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(e,n,r),this.submitForm()}cancelEnter(t){"Enter"===t.code&&(t.preventDefault(),t.stopPropagation())}}f.values={criterionId:Number,stableId:String};const v=[[a,"refine/add-controller.js"],[l,"refine/defaults-controller.js"],[o,"refine/delete-controller.js"],[s,"refine/form-controller.js"],[d,"refine/search-filter-controller.js"],[p,"refine/state-controller.js"],[b,"refine/stored-filter-controller.js"],[m,"refine/stabilize-filter-controller.js"],[f,"refine/update-controller.js"]].map(function(t){const e=t[0];return{identifier:r(t[1]),controllerConstructor:e}});export{a as AddController,l as DefaultsController,o as DeleteController,s as FormController,d as SearchFilterController,m as StabilizeFilterController,p as StateController,b as StoredFilterController,f as UpdateController,v as controllerDefinitions}; | ||
import{Controller as t}from"@hotwired/stimulus";import{FetchRequest as e}from"@rails/request.js";import{delegate as i,abnegate as n}from"jquery-events-to-dom-events";import{debounce as r}from"lodash";function s(t){const e=(t.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(e)return e.replace(/_/g,"-").replace(/\//g,"--")}class a extends t{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}addHiddenInput(t,e){const i=document.createElement("input");return i.type="hidden",i.name=t,i.value=e||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class l extends a{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}l.values={previousCriterionId:Number};class o extends t{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}}o.values={criterionId:Number,input:Object};class d extends a{criterion(){const{state:t,criterionIdValue:e}=this;t.deleteCriterion(e),this.startUpdate()}}d.values={criterionId:Number};class u extends t{connect(){const t=new URLSearchParams(window.location.search);this.existingParams=t,this.existingParams.delete("stable_id")}search(t){t.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const t=this,{blueprint:e}=t.stateController;return Promise.resolve(t.stabilizeFilterController.validateBlueprint(e)).then(function(i){i.stableId?t.redirectToStableId(i.stableId):t.fetchAndRenderInvalidFilter(e)})}catch(t){return Promise.reject(t)}}addHiddenInput(t){let{name:e,value:i}=t;const n=document.createElement("input");n.type="hidden",n.name=e,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(t){const e=new URLSearchParams;t&&e.append("stable_id",t);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(e)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(t){try{const i=this,n=new e("POST",i.submitUrlValue,{responseKind:"turbo-stream",body:JSON.stringify({filter:i.stateController.filterName,blueprint:JSON.stringify(t),id_suffix:i.stateController.idSuffix})});return Promise.resolve(n.perform()).then(function(){})}catch(t){return Promise.reject(t)}}}u.values={submitUrl:String},u.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function t(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),i}t.prototype=window.Event.prototype,window.CustomEvent=t}();const c=(t,e,i)=>{const n=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:e,filterName:i}});t.dispatchEvent(n)},h=(t,e)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});t.dispatchEvent(i)},p=(t,e,i)=>{var n;const r=null==i?void 0:i.component,s=(null==i?void 0:i.meta)||{clauses:[],options:{}},a=(null==i?void 0:i.refinements)||[],{clauses:l,options:o}=s;let d={clause:null==(n=l[0])?void 0:n.id,selected:"option-condition"===r?[o[0].id]:void 0};return a.forEach(t=>{var e;const{meta:i,component:n}=t,{clauses:r,options:s}=i;d[t.id]={clause:r[0].id,selected:"option-condition"===n?[null==(e=s[0])?void 0:e.id]:void 0}}),{depth:e,type:"criterion",condition_id:t,input:d}};class b extends t{connect(){this.element.refineStateController=this,this.changeDelegate=i("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((t,e)=>(t[e.id]=e,t),{}),this.loadingTimeout=null,h(this.element,this.blueprint)}disconnect(){n("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(t){return this.conditionsLookup[t]}updateStableId(t){t!==this.stableId&&(this.stableId=t)}addGroup(){const{conditions:t}=this,e=t[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(p(e.id,1,e)),h(this.element,this.blueprint)}addCriterion(t){const{blueprint:e,conditions:i}=this,n=i[0];var r;e.splice(t+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},p(n.id,1,n)),h(this.element,this.blueprint)}deleteCriterion(t){const{blueprint:e}=this,i=e[t-1],n=e[t+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,a=r||!i,l=a&&s;i||n?e.splice(l&&r?t-1:l&&!i||a&&!s?t:t-1,2):this.blueprint=[],h(this.element,this.blueprint)}replaceCriterion(t,e,i){const n=this.blueprint[t];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(p));this.blueprint[t]=p(e,n.depth,i),h(this.element,this.blueprint)}updateInput(t,e,i){const{blueprint:n}=this,r=n[t],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...e}:r[i]={...r[i],...e},h(this.element,this.blueprint)}}b.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},b.targets=["loading"];class m extends t{connect(){this.idValue&&(t=>{const e=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(e)})()}updateStableIdField(t){if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:e}=t,{stableId:i}=e;this.stableIdFieldTarget.value=i}}activateSaveLink(t){const{detail:e}=t,{stableId:i}=e;if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget){const t=new URL(this.enabledSaveLinkTarget.href);t.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",t),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}m.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],m.values={id:Number,stableId:String,filterName:String};class f extends t{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(t){try{const e=this;(t=>{const e=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});window.dispatchEvent(e)})(e.blueprint);const i=t.detail.blueprint;return Promise.resolve(e.validateBlueprint(i)).then(function(t){if(t.stableId)e.stableIdValue=t.stableId,c(e.element,e.stableIdValue,e.filterNameValue),c(window,e.stableIdValue,e.filterNameValue);else{const{errors:e}=t;(t=>{let{blueprint:e,errors:i}=t;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:e,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:e})}})}catch(t){return Promise.reject(t)}}validateBlueprint(t){try{var e;const i=this;let n=JSON.stringify({blueprint:t,filter:i.filterNameValue}),r=null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(t){return Promise.resolve(t.json()).then(function(e){return t.ok?{stableId:e.filter_id}:{errors:e.errors}})})}catch(t){return Promise.reject(t)}}}f.values={stableId:String,updateStableIdUrl:String,filterName:String},f.targets=[];class v extends a{initialize(){this.updateBlueprint=r((t,e,i)=>{this.value(t,e,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){a.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{id:t.target.value},t.target.dataset.inputId),this.submitForm()}clause(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{clause:t.target.value},t.target.dataset.inputId),this.submitForm()}selected(t){const{target:e}=t,i=Array.prototype.slice.call(e.options).filter(t=>t.selected).map(t=>t.value);this.value(t,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(t,e,i){const{criterionIdValue:n,state:r}=this,s=t.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:e=e||t.target.value},s.inputId)}date(t){const{picker:e}=t.detail,i=e.startDate.format("YYYY-MM-DD");this.value(t,i),this.submitForm()}createStableId(t,e){var i;const{state:n}=this;let r=JSON.stringify({blueprint:t,filter:e}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(t){n.updateStableId(t.filter_id)}})}condition(t){const{criterionIdValue:e,state:i}=this,n=t.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(e,n,r),this.submitForm()}cancelEnter(t){"Enter"===t.code&&(t.preventDefault(),t.stopPropagation())}}v.values={criterionId:Number,stableId:String};const g=[[l,"refine/add-controller.js"],[o,"refine/defaults-controller.js"],[d,"refine/delete-controller.js"],[a,"refine/form-controller.js"],[u,"refine/search-filter-controller.js"],[b,"refine/state-controller.js"],[m,"refine/stored-filter-controller.js"],[f,"refine/stabilize-filter-controller.js"],[v,"refine/update-controller.js"]].map(function(t){const e=t[0];return{identifier:s(t[1]),controllerConstructor:e}});export{l as AddController,o as DefaultsController,d as DeleteController,a as FormController,u as SearchFilterController,f as StabilizeFilterController,b as StateController,m as StoredFilterController,v as UpdateController,g as controllerDefinitions}; | ||
//# sourceMappingURL=refine-stimulus.esm.js.map |
@@ -1,2 +0,2 @@ | ||
var t=require("@hotwired/stimulus"),e=require("jquery-events-to-dom-events"),i=require("lodash");function n(t){const e=(t.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(e)return e.replace(/_/g,"-").replace(/\//g,"--")}class r extends t.Controller{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}addHiddenInput(t,e){const i=document.createElement("input");return i.type="hidden",i.name=t,i.value=e||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class s extends r{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}s.values={previousCriterionId:Number};class l extends t.Controller{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}}l.values={criterionId:Number,input:Object};class a extends r{criterion(){const{state:t,criterionIdValue:e}=this;t.deleteCriterion(e),this.startUpdate()}}a.values={criterionId:Number};class o extends t.Controller{connect(){const t=new URLSearchParams(window.location.search);this.existingParams=t,this.existingParams.delete("stable_id")}search(t){t.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const t=this,{blueprint:e}=t.stateController;return Promise.resolve(t.stabilizeFilterController.validateBlueprint(e)).then(function(i){i.stableId?t.redirectToStableId(i.stableId):t.fetchAndRenderInvalidFilter(e)})}catch(t){return Promise.reject(t)}}addHiddenInput(t){let{name:e,value:i}=t;const n=document.createElement("input");n.type="hidden",n.name=e,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(t){const e=new URLSearchParams;t&&e.append("stable_id",t);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(e)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(t){try{var e;const i=this;return Promise.resolve(fetch(i.submitUrlValue,{headers:{Accept:"application/json","Content-Type":"application/json","X-CSRF-Token":null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content},method:"POST",body:JSON.stringify({filter:i.stateController.filterName,blueprint:JSON.stringify(t),id_suffix:i.stateController.idSuffix})})).then(function(t){return Promise.resolve(t.json()).then(function(t){document.getElementById(t.target).outerHTML=t.template})})}catch(t){return Promise.reject(t)}}}o.values={submitUrl:String},o.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function t(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),i}t.prototype=window.Event.prototype,window.CustomEvent=t}();const d=(t,e,i,n)=>{const r=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:e,filterName:i,initialLoad:n}});t.dispatchEvent(r)},u=(t,e)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});t.dispatchEvent(i)},c=(t,e,i)=>{const{meta:n,refinements:r,component:s}=i,{clauses:l,options:a}=n;let o={clause:l[0].id,selected:"option-condition"===s?[a[0].id]:void 0};return r.forEach(t=>{var e;const{meta:i,component:n}=t,{clauses:r,options:s}=i;o[t.id]={clause:r[0].id,selected:"option-condition"===n?[null==(e=s[0])?void 0:e.id]:void 0}}),{depth:e,type:"criterion",condition_id:t,input:o}};class h extends t.Controller{connect(){this.element.refineStateController=this,this.changeDelegate=e.delegate("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((t,e)=>(t[e.id]=e,t),{}),this.loadingTimeout=null,u(this.element,this.blueprint)}disconnect(){e.abnegate("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(t){return this.conditionsLookup[t]}updateStableId(t){t!==this.stableId&&(this.stableId=t)}addGroup(){const{conditions:t}=this,e=t[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(c(e.id,1,e)),u(this.element,this.blueprint)}addCriterion(t){const{blueprint:e,conditions:i}=this,n=i[0];var r;e.splice(t+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},c(n.id,1,n)),u(this.element,this.blueprint)}deleteCriterion(t){const{blueprint:e}=this,i=e[t-1],n=e[t+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,l=r||!i,a=l&&s;i||n?e.splice(a&&r?t-1:a&&!i||l&&!s?t:t-1,2):this.blueprint=[],u(this.element,this.blueprint)}replaceCriterion(t,e,i){const n=this.blueprint[t];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(c));this.blueprint[t]=c(e,n.depth,i),u(this.element,this.blueprint)}updateInput(t,e,i){const{blueprint:n}=this,r=n[t],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...e}:r[i]={...r[i],...e},u(this.element,this.blueprint)}}h.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},h.targets=["loading"];class p extends t.Controller{connect(){this.idValue&&(t=>{const e=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(e)})()}updateStableIdField(t){if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:e}=t,{stableId:i}=e;this.stableIdFieldTarget.value=i}}activateSaveLink(t){const{detail:e}=t,{stableId:i,initialLoad:n}=e;if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget&&!n){const t=new URL(this.enabledSaveLinkTarget.href);t.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",t),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}p.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],p.values={id:Number,stableId:String,filterName:String};class b extends t.Controller{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(t){try{const e=this;(t=>{const e=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});window.dispatchEvent(e)})(e.blueprint);const i=t.detail.blueprint;return Promise.resolve(e.validateBlueprint(i)).then(function(t){if(t.stableId)e.stableIdValue=t.stableId,d(e.element,e.stableIdValue,e.filterNameValue),d(window,e.stableIdValue,e.filterNameValue);else{const{errors:e}=t;(t=>{let{blueprint:e,errors:i}=t;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:e,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:e})}})}catch(t){return Promise.reject(t)}}validateBlueprint(t){try{var e;const i=this;let n=JSON.stringify({blueprint:t,filter:i.filterNameValue}),r=null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(t){return Promise.resolve(t.json()).then(function(e){return t.ok?{stableId:e.filter_id}:{errors:e.errors}})})}catch(t){return Promise.reject(t)}}}b.values={stableId:String,updateStableIdUrl:String,filterName:String},b.targets=[];class m extends r{initialize(){this.updateBlueprint=i.debounce((t,e,i)=>{this.value(t,e,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){r.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{id:t.target.value},t.target.dataset.inputId),this.submitForm()}clause(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{clause:t.target.value},t.target.dataset.inputId),this.submitForm()}selected(t){const{target:e}=t,i=Array.prototype.slice.call(e.options).filter(t=>t.selected).map(t=>t.value);this.value(t,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(t,e,i){const{criterionIdValue:n,state:r}=this,s=t.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:e=e||t.target.value},s.inputId)}date(t){const{picker:e}=t.detail,i=e.startDate.format("YYYY-MM-DD");this.value(t,i),this.submitForm()}createStableId(t,e){var i;const{state:n}=this;let r=JSON.stringify({blueprint:t,filter:e}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(t){n.updateStableId(t.filter_id)}})}condition(t){const{criterionIdValue:e,state:i}=this,n=t.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(e,n,r),this.submitForm()}cancelEnter(t){"Enter"===t.code&&(t.preventDefault(),t.stopPropagation())}}m.values={criterionId:Number,stableId:String};const f=[[s,"refine/add-controller.js"],[l,"refine/defaults-controller.js"],[a,"refine/delete-controller.js"],[r,"refine/form-controller.js"],[o,"refine/search-filter-controller.js"],[h,"refine/state-controller.js"],[p,"refine/stored-filter-controller.js"],[b,"refine/stabilize-filter-controller.js"],[m,"refine/update-controller.js"]].map(function(t){const e=t[0];return{identifier:n(t[1]),controllerConstructor:e}});exports.AddController=s,exports.DefaultsController=l,exports.DeleteController=a,exports.FormController=r,exports.SearchFilterController=o,exports.StabilizeFilterController=b,exports.StateController=h,exports.StoredFilterController=p,exports.UpdateController=m,exports.controllerDefinitions=f; | ||
var e=require("@hotwired/stimulus"),t=require("@rails/request.js"),i=require("jquery-events-to-dom-events"),n=require("lodash");function r(e){const t=(e.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(t)return t.replace(/_/g,"-").replace(/\//g,"--")}class s extends e.Controller{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}addHiddenInput(e,t){const i=document.createElement("input");return i.type="hidden",i.name=e,i.value=t||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class l extends s{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}l.values={previousCriterionId:Number};class a extends e.Controller{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}}a.values={criterionId:Number,input:Object};class o extends s{criterion(){const{state:e,criterionIdValue:t}=this;e.deleteCriterion(t),this.startUpdate()}}o.values={criterionId:Number};class d extends e.Controller{connect(){const e=new URLSearchParams(window.location.search);this.existingParams=e,this.existingParams.delete("stable_id")}search(e){e.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const e=this,{blueprint:t}=e.stateController;return Promise.resolve(e.stabilizeFilterController.validateBlueprint(t)).then(function(i){i.stableId?e.redirectToStableId(i.stableId):e.fetchAndRenderInvalidFilter(t)})}catch(e){return Promise.reject(e)}}addHiddenInput(e){let{name:t,value:i}=e;const n=document.createElement("input");n.type="hidden",n.name=t,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(e){const t=new URLSearchParams;e&&t.append("stable_id",e);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(t)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(e){try{const i=this,n=new t.FetchRequest("POST",i.submitUrlValue,{responseKind:"turbo-stream",body:JSON.stringify({filter:i.stateController.filterName,blueprint:JSON.stringify(e),id_suffix:i.stateController.idSuffix})});return Promise.resolve(n.perform()).then(function(){})}catch(e){return Promise.reject(e)}}}d.values={submitUrl:String},d.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),i}e.prototype=window.Event.prototype,window.CustomEvent=e}();const u=(e,t,i)=>{const n=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:t,filterName:i}});e.dispatchEvent(n)},c=(e,t)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});e.dispatchEvent(i)},h=(e,t,i)=>{var n;const r=null==i?void 0:i.component,s=(null==i?void 0:i.meta)||{clauses:[],options:{}},l=(null==i?void 0:i.refinements)||[],{clauses:a,options:o}=s;let d={clause:null==(n=a[0])?void 0:n.id,selected:"option-condition"===r?[o[0].id]:void 0};return l.forEach(e=>{var t;const{meta:i,component:n}=e,{clauses:r,options:s}=i;d[e.id]={clause:r[0].id,selected:"option-condition"===n?[null==(t=s[0])?void 0:t.id]:void 0}}),{depth:t,type:"criterion",condition_id:e,input:d}};class p extends e.Controller{connect(){this.element.refineStateController=this,this.changeDelegate=i.delegate("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((e,t)=>(e[t.id]=t,e),{}),this.loadingTimeout=null,c(this.element,this.blueprint)}disconnect(){i.abnegate("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(e){return this.conditionsLookup[e]}updateStableId(e){e!==this.stableId&&(this.stableId=e)}addGroup(){const{conditions:e}=this,t=e[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(h(t.id,1,t)),c(this.element,this.blueprint)}addCriterion(e){const{blueprint:t,conditions:i}=this,n=i[0];var r;t.splice(e+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},h(n.id,1,n)),c(this.element,this.blueprint)}deleteCriterion(e){const{blueprint:t}=this,i=t[e-1],n=t[e+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,l=r||!i,a=l&&s;i||n?t.splice(a&&r?e-1:a&&!i||l&&!s?e:e-1,2):this.blueprint=[],c(this.element,this.blueprint)}replaceCriterion(e,t,i){const n=this.blueprint[e];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(h));this.blueprint[e]=h(t,n.depth,i),c(this.element,this.blueprint)}updateInput(e,t,i){const{blueprint:n}=this,r=n[e],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...t}:r[i]={...r[i],...t},c(this.element,this.blueprint)}}p.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},p.targets=["loading"];class b extends e.Controller{connect(){this.idValue&&(e=>{const t=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(t)})()}updateStableIdField(e){if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:t}=e,{stableId:i}=t;this.stableIdFieldTarget.value=i}}activateSaveLink(e){const{detail:t}=e,{stableId:i}=t;if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget){const e=new URL(this.enabledSaveLinkTarget.href);e.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",e),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}b.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],b.values={id:Number,stableId:String,filterName:String};class m extends e.Controller{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(e){try{const t=this;(e=>{const t=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});window.dispatchEvent(t)})(t.blueprint);const i=e.detail.blueprint;return Promise.resolve(t.validateBlueprint(i)).then(function(e){if(e.stableId)t.stableIdValue=e.stableId,u(t.element,t.stableIdValue,t.filterNameValue),u(window,t.stableIdValue,t.filterNameValue);else{const{errors:t}=e;(e=>{let{blueprint:t,errors:i}=e;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:t,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:t})}})}catch(e){return Promise.reject(e)}}validateBlueprint(e){try{var t;const i=this;let n=JSON.stringify({blueprint:e,filter:i.filterNameValue}),r=null==(t=document.querySelector("meta[name='csrf-token']"))?void 0:t.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(e){return Promise.resolve(e.json()).then(function(t){return e.ok?{stableId:t.filter_id}:{errors:t.errors}})})}catch(e){return Promise.reject(e)}}}m.values={stableId:String,updateStableIdUrl:String,filterName:String},m.targets=[];class f extends s{initialize(){this.updateBlueprint=n.debounce((e,t,i)=>{this.value(e,t,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){s.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{id:e.target.value},e.target.dataset.inputId),this.submitForm()}clause(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{clause:e.target.value},e.target.dataset.inputId),this.submitForm()}selected(e){const{target:t}=e,i=Array.prototype.slice.call(t.options).filter(e=>e.selected).map(e=>e.value);this.value(e,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(e,t,i){const{criterionIdValue:n,state:r}=this,s=e.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:t=t||e.target.value},s.inputId)}date(e){const{picker:t}=e.detail,i=t.startDate.format("YYYY-MM-DD");this.value(e,i),this.submitForm()}createStableId(e,t){var i;const{state:n}=this;let r=JSON.stringify({blueprint:e,filter:t}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(e){n.updateStableId(e.filter_id)}})}condition(e){const{criterionIdValue:t,state:i}=this,n=e.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(t,n,r),this.submitForm()}cancelEnter(e){"Enter"===e.code&&(e.preventDefault(),e.stopPropagation())}}f.values={criterionId:Number,stableId:String};const v=[[l,"refine/add-controller.js"],[a,"refine/defaults-controller.js"],[o,"refine/delete-controller.js"],[s,"refine/form-controller.js"],[d,"refine/search-filter-controller.js"],[p,"refine/state-controller.js"],[b,"refine/stored-filter-controller.js"],[m,"refine/stabilize-filter-controller.js"],[f,"refine/update-controller.js"]].map(function(e){const t=e[0];return{identifier:r(e[1]),controllerConstructor:t}});exports.AddController=l,exports.DefaultsController=a,exports.DeleteController=o,exports.FormController=s,exports.SearchFilterController=d,exports.StabilizeFilterController=m,exports.StateController=p,exports.StoredFilterController=b,exports.UpdateController=f,exports.controllerDefinitions=v; | ||
//# sourceMappingURL=refine-stimulus.js.map |
@@ -1,2 +0,2 @@ | ||
import{Controller as t}from"@hotwired/stimulus";import{delegate as e,abnegate as i}from"jquery-events-to-dom-events";import{debounce as n}from"lodash";function a(t){const e=(t.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(e)return e.replace(/_/g,"-").replace(/\//g,"--")}class s extends t{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}addHiddenInput(t,e){const i=document.createElement("input");return i.type="hidden",i.name=t,i.value=e||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class r extends s{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}r.values={previousCriterionId:Number};class l extends t{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}}l.values={criterionId:Number,input:Object};class o extends s{criterion(){const{state:t,criterionIdValue:e}=this;t.deleteCriterion(e),this.startUpdate()}}function d(){return d=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t},d.apply(this,arguments)}o.values={criterionId:Number};class u extends t{connect(){const t=new URLSearchParams(window.location.search);this.existingParams=t,this.existingParams.delete("stable_id")}search(t){t.preventDefault(),this.submitFilter(),document.activeElement.blur()}async submitFilter(){const{blueprint:t}=this.stateController,e=await this.stabilizeFilterController.validateBlueprint(t);e.stableId?this.redirectToStableId(e.stableId):this.fetchAndRenderInvalidFilter(t)}addHiddenInput({name:t,value:e}){const i=document.createElement("input");i.type="hidden",i.name=t,i.value=e,this.submissionFormTarget.appendChild(i)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(t){const e=new URLSearchParams;t&&e.append("stable_id",t);const i=new URLSearchParams(d({},Object.fromEntries(this.existingParams),Object.fromEntries(e))).toString(),n=`${window.location.pathname}?${i}`;history.pushState({},document.title,n),window.location.reload()}async fetchAndRenderInvalidFilter(t){var e;const i=await fetch(this.submitUrlValue,{headers:{Accept:"application/json","Content-Type":"application/json","X-CSRF-Token":null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content},method:"POST",body:JSON.stringify({filter:this.stateController.filterName,blueprint:JSON.stringify(t),id_suffix:this.stateController.idSuffix})}),n=await i.json();document.getElementById(n.target).outerHTML=n.template}}u.values={submitUrl:String},u.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function t(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),i}t.prototype=window.Event.prototype,window.CustomEvent=t}();const c=(t,e,i,n)=>{const a=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:e,filterName:i,initialLoad:n}});t.dispatchEvent(a)},h=(t,e)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});t.dispatchEvent(i)},p=(t,e,i)=>{const{meta:n,refinements:a,component:s}=i,{clauses:r,options:l}=n;let o={clause:r[0].id,selected:"option-condition"===s?[l[0].id]:void 0};return a.forEach(t=>{var e;const{meta:i,component:n}=t,{clauses:a,options:s}=i;o[t.id]={clause:a[0].id,selected:"option-condition"===n?[null==(e=s[0])?void 0:e.id]:void 0}}),{depth:e,type:"criterion",condition_id:t,input:o}};class b extends t{connect(){this.element.refineStateController=this,this.changeDelegate=e("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((t,e)=>(t[e.id]=e,t),{}),this.loadingTimeout=null,h(this.element,this.blueprint)}disconnect(){i("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(t){return this.conditionsLookup[t]}updateStableId(t){t!==this.stableId&&(this.stableId=t)}addGroup(){const{conditions:t}=this,e=t[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(p(e.id,1,e)),h(this.element,this.blueprint)}addCriterion(t){const{blueprint:e,conditions:i}=this,n=i[0];var a;e.splice(t+1,0,{depth:a=void 0===a?1:a,type:"conjunction",word:"and"},p(n.id,1,n)),h(this.element,this.blueprint)}deleteCriterion(t){const{blueprint:e}=this,i=e[t-1],n=e[t+1],a=i&&"or"===i.word,s=n&&"or"===n.word||!n,r=a||!i,l=r&&s;i||n?e.splice(l&&a?t-1:l&&!i||r&&!s?t:t-1,2):this.blueprint=[],h(this.element,this.blueprint)}replaceCriterion(t,e,i){const n=this.blueprint[t];if("criterion"!==n.type)throw new Error(`You can't call updateConditionId on a non-criterion type. Trying to update ${JSON.stringify(p)}`);this.blueprint[t]=p(e,n.depth,i),h(this.element,this.blueprint)}updateInput(t,e,i){const{blueprint:n}=this,a=n[t],s=(i=i||"input").split(", ");s.length>1?a[s[0]][s[1]]=d({},a[s[0]][s[1]],e):a[i]=d({},a[i],e),h(this.element,this.blueprint)}}b.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},b.targets=["loading"];class m extends t{connect(){this.idValue&&(t=>{const e=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(e)})()}updateStableIdField(t){if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:e}=t,{stableId:i}=e;this.stableIdFieldTarget.value=i}}activateSaveLink(t){const{detail:e}=t,{stableId:i,initialLoad:n}=e;if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget&&!n){const t=new URL(this.enabledSaveLinkTarget.href);t.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",t),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}m.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],m.values={id:Number,stableId:String,filterName:String};class f extends t{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}async updateStableId(t){(t=>{const e=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:this.blueprint}});window.dispatchEvent(e)})();const e=t.detail.blueprint,i=await this.validateBlueprint(e);if(i.stableId)this.stableIdValue=i.stableId,c(this.element,this.stableIdValue,this.filterNameValue),c(window,this.stableIdValue,this.filterNameValue);else{const{errors:t}=i;(({blueprint:t,errors:e})=>{const i=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:t,errors:e}});window.dispatchEvent(i)})({blueprint:e,errors:t})}}async validateBlueprint(t){var e;let i=JSON.stringify({blueprint:t,filter:this.filterNameValue}),n=null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content;const a=await fetch(this.updateStableIdUrlValue,{method:"PUT",body:i,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":n}}),s=await a.json();return a.ok?{stableId:s.filter_id}:{errors:s.errors}}}f.values={stableId:String,updateStableIdUrl:String,filterName:String},f.targets=[];class v extends s{initialize(){this.updateBlueprint=n((t,e,i)=>{this.value(t,e,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){s.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{id:t.target.value},t.target.dataset.inputId),this.submitForm()}clause(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{clause:t.target.value},t.target.dataset.inputId),this.submitForm()}selected(t){const{target:e}=t,i=Array.prototype.slice.call(e.options).filter(t=>t.selected).map(t=>t.value);this.value(t,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(t,e,i){const{criterionIdValue:n,state:a}=this,s=t.target.dataset;a.updateInput(n,{[i=i||s.inputKey||"value"]:e=e||t.target.value},s.inputId)}date(t){const{picker:e}=t.detail,i=e.startDate.format("YYYY-MM-DD");this.value(t,i),this.submitForm()}createStableId(t,e){var i;const{state:n}=this;let a=JSON.stringify({blueprint:t,filter:e}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:a,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(t){n.updateStableId(t.filter_id)}})}condition(t){const{criterionIdValue:e,state:i}=this,n=t.target.value,a=this.state.conditionConfigFor(n);i.replaceCriterion(e,n,a),this.submitForm()}cancelEnter(t){"Enter"===t.code&&(t.preventDefault(),t.stopPropagation())}}v.values={criterionId:Number,stableId:String};const g=[[r,"refine/add-controller.js"],[l,"refine/defaults-controller.js"],[o,"refine/delete-controller.js"],[s,"refine/form-controller.js"],[u,"refine/search-filter-controller.js"],[b,"refine/state-controller.js"],[m,"refine/stored-filter-controller.js"],[f,"refine/stabilize-filter-controller.js"],[v,"refine/update-controller.js"]].map(function(t){const e=t[0];return{identifier:a(t[1]),controllerConstructor:e}});export{r as AddController,l as DefaultsController,o as DeleteController,s as FormController,u as SearchFilterController,f as StabilizeFilterController,b as StateController,m as StoredFilterController,v as UpdateController,g as controllerDefinitions}; | ||
import{Controller as t}from"@hotwired/stimulus";import{FetchRequest as e}from"@rails/request.js";import{delegate as i,abnegate as n}from"jquery-events-to-dom-events";import{debounce as s}from"lodash";function r(t){const e=(t.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(e)return e.replace(/_/g,"-").replace(/\//g,"--")}class a extends t{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}addHiddenInput(t,e){const i=document.createElement("input");return i.type="hidden",i.name=t,i.value=e||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class l extends a{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}l.values={previousCriterionId:Number};class o extends t{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let t=this.element;for(;t!==document.body;){const e=this.application.getControllerForElementAndIdentifier(t,"refine--state");if(e)return e;t=t.parentNode}return null}}o.values={criterionId:Number,input:Object};class d extends a{criterion(){const{state:t,criterionIdValue:e}=this;t.deleteCriterion(e),this.startUpdate()}}function u(){return u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t},u.apply(this,arguments)}d.values={criterionId:Number};class c extends t{connect(){const t=new URLSearchParams(window.location.search);this.existingParams=t,this.existingParams.delete("stable_id")}search(t){t.preventDefault(),this.submitFilter(),document.activeElement.blur()}async submitFilter(){const{blueprint:t}=this.stateController,e=await this.stabilizeFilterController.validateBlueprint(t);e.stableId?this.redirectToStableId(e.stableId):this.fetchAndRenderInvalidFilter(t)}addHiddenInput({name:t,value:e}){const i=document.createElement("input");i.type="hidden",i.name=t,i.value=e,this.submissionFormTarget.appendChild(i)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(t){const e=new URLSearchParams;t&&e.append("stable_id",t);const i=new URLSearchParams(u({},Object.fromEntries(this.existingParams),Object.fromEntries(e))).toString(),n=`${window.location.pathname}?${i}`;history.pushState({},document.title,n),window.location.reload()}async fetchAndRenderInvalidFilter(t){const i=new e("POST",this.submitUrlValue,{responseKind:"turbo-stream",body:JSON.stringify({filter:this.stateController.filterName,blueprint:JSON.stringify(t),id_suffix:this.stateController.idSuffix})});await i.perform()}}c.values={submitUrl:String},c.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function t(t,e){e=e||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),i}t.prototype=window.Event.prototype,window.CustomEvent=t}();const h=(t,e,i)=>{const n=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:e,filterName:i}});t.dispatchEvent(n)},p=(t,e)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});t.dispatchEvent(i)},b=(t,e,i)=>{var n;const s=null==i?void 0:i.component,r=(null==i?void 0:i.meta)||{clauses:[],options:{}},a=(null==i?void 0:i.refinements)||[],{clauses:l,options:o}=r;let d={clause:null==(n=l[0])?void 0:n.id,selected:"option-condition"===s?[o[0].id]:void 0};return a.forEach(t=>{var e;const{meta:i,component:n}=t,{clauses:s,options:r}=i;d[t.id]={clause:s[0].id,selected:"option-condition"===n?[null==(e=r[0])?void 0:e.id]:void 0}}),{depth:e,type:"criterion",condition_id:t,input:d}};class m extends t{connect(){this.element.refineStateController=this,this.changeDelegate=i("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((t,e)=>(t[e.id]=e,t),{}),this.loadingTimeout=null,p(this.element,this.blueprint)}disconnect(){n("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(t){return this.conditionsLookup[t]}updateStableId(t){t!==this.stableId&&(this.stableId=t)}addGroup(){const{conditions:t}=this,e=t[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(b(e.id,1,e)),p(this.element,this.blueprint)}addCriterion(t){const{blueprint:e,conditions:i}=this,n=i[0];var s;e.splice(t+1,0,{depth:s=void 0===s?1:s,type:"conjunction",word:"and"},b(n.id,1,n)),p(this.element,this.blueprint)}deleteCriterion(t){const{blueprint:e}=this,i=e[t-1],n=e[t+1],s=i&&"or"===i.word,r=n&&"or"===n.word||!n,a=s||!i,l=a&&r;i||n?e.splice(l&&s?t-1:l&&!i||a&&!r?t:t-1,2):this.blueprint=[],p(this.element,this.blueprint)}replaceCriterion(t,e,i){const n=this.blueprint[t];if("criterion"!==n.type)throw new Error(`You can't call updateConditionId on a non-criterion type. Trying to update ${JSON.stringify(b)}`);this.blueprint[t]=b(e,n.depth,i),p(this.element,this.blueprint)}updateInput(t,e,i){const{blueprint:n}=this,s=n[t],r=(i=i||"input").split(", ");r.length>1?s[r[0]][r[1]]=u({},s[r[0]][r[1]],e):s[i]=u({},s[i],e),p(this.element,this.blueprint)}}m.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},m.targets=["loading"];class f extends t{connect(){this.idValue&&(t=>{const e=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(e)})()}updateStableIdField(t){if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:e}=t,{stableId:i}=e;this.stableIdFieldTarget.value=i}}activateSaveLink(t){const{detail:e}=t,{stableId:i}=e;if(t.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget){const t=new URL(this.enabledSaveLinkTarget.href);t.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",t),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}f.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],f.values={id:Number,stableId:String,filterName:String};class v extends t{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}async updateStableId(t){(t=>{const e=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:this.blueprint}});window.dispatchEvent(e)})();const e=t.detail.blueprint,i=await this.validateBlueprint(e);if(i.stableId)this.stableIdValue=i.stableId,h(this.element,this.stableIdValue,this.filterNameValue),h(window,this.stableIdValue,this.filterNameValue);else{const{errors:t}=i;(({blueprint:t,errors:e})=>{const i=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:t,errors:e}});window.dispatchEvent(i)})({blueprint:e,errors:t})}}async validateBlueprint(t){var e;let i=JSON.stringify({blueprint:t,filter:this.filterNameValue}),n=null==(e=document.querySelector("meta[name='csrf-token']"))?void 0:e.content;const s=await fetch(this.updateStableIdUrlValue,{method:"PUT",body:i,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":n}}),r=await s.json();return s.ok?{stableId:r.filter_id}:{errors:r.errors}}}v.values={stableId:String,updateStableIdUrl:String,filterName:String},v.targets=[];class g extends a{initialize(){this.updateBlueprint=s((t,e,i)=>{this.value(t,e,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){a.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{id:t.target.value},t.target.dataset.inputId),this.submitForm()}clause(t){const{criterionIdValue:e,state:i}=this;i.updateInput(e,{clause:t.target.value},t.target.dataset.inputId),this.submitForm()}selected(t){const{target:e}=t,i=Array.prototype.slice.call(e.options).filter(t=>t.selected).map(t=>t.value);this.value(t,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(t,e,i){const{criterionIdValue:n,state:s}=this,r=t.target.dataset;s.updateInput(n,{[i=i||r.inputKey||"value"]:e=e||t.target.value},r.inputId)}date(t){const{picker:e}=t.detail,i=e.startDate.format("YYYY-MM-DD");this.value(t,i),this.submitForm()}createStableId(t,e){var i;const{state:n}=this;let s=JSON.stringify({blueprint:t,filter:e}),r=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:s,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r},success:function(t){n.updateStableId(t.filter_id)}})}condition(t){const{criterionIdValue:e,state:i}=this,n=t.target.value,s=this.state.conditionConfigFor(n);i.replaceCriterion(e,n,s),this.submitForm()}cancelEnter(t){"Enter"===t.code&&(t.preventDefault(),t.stopPropagation())}}g.values={criterionId:Number,stableId:String};const I=[[l,"refine/add-controller.js"],[o,"refine/defaults-controller.js"],[d,"refine/delete-controller.js"],[a,"refine/form-controller.js"],[c,"refine/search-filter-controller.js"],[m,"refine/state-controller.js"],[f,"refine/stored-filter-controller.js"],[v,"refine/stabilize-filter-controller.js"],[g,"refine/update-controller.js"]].map(function(t){const e=t[0];return{identifier:r(t[1]),controllerConstructor:e}});export{l as AddController,o as DefaultsController,d as DeleteController,a as FormController,c as SearchFilterController,v as StabilizeFilterController,m as StateController,f as StoredFilterController,g as UpdateController,I as controllerDefinitions}; | ||
//# sourceMappingURL=refine-stimulus.modern.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@hotwired/stimulus"),require("jquery-events-to-dom-events"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@hotwired/stimulus","jquery-events-to-dom-events","lodash"],t):t((e||self).refineStimulus={},e.Stimulus,e.jqueryEventsToDomEvents,e.lodash)}(this,function(e,t,i,n){function r(e){const t=(e.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(t)return t.replace(/_/g,"-").replace(/\//g,"--")}class s extends t.Controller{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}addHiddenInput(e,t){const i=document.createElement("input");return i.type="hidden",i.name=e,i.value=t||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class l extends s{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}l.values={previousCriterionId:Number};class a extends t.Controller{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}}a.values={criterionId:Number,input:Object};class o extends s{criterion(){const{state:e,criterionIdValue:t}=this;e.deleteCriterion(t),this.startUpdate()}}o.values={criterionId:Number};class d extends t.Controller{connect(){const e=new URLSearchParams(window.location.search);this.existingParams=e,this.existingParams.delete("stable_id")}search(e){e.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const e=this,{blueprint:t}=e.stateController;return Promise.resolve(e.stabilizeFilterController.validateBlueprint(t)).then(function(i){i.stableId?e.redirectToStableId(i.stableId):e.fetchAndRenderInvalidFilter(t)})}catch(e){return Promise.reject(e)}}addHiddenInput(e){let{name:t,value:i}=e;const n=document.createElement("input");n.type="hidden",n.name=t,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(e){const t=new URLSearchParams;e&&t.append("stable_id",e);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(t)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(e){try{var t;const i=this;return Promise.resolve(fetch(i.submitUrlValue,{headers:{Accept:"application/json","Content-Type":"application/json","X-CSRF-Token":null==(t=document.querySelector("meta[name='csrf-token']"))?void 0:t.content},method:"POST",body:JSON.stringify({filter:i.stateController.filterName,blueprint:JSON.stringify(e),id_suffix:i.stateController.idSuffix})})).then(function(e){return Promise.resolve(e.json()).then(function(e){document.getElementById(e.target).outerHTML=e.template})})}catch(e){return Promise.reject(e)}}}d.values={submitUrl:String},d.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),i}e.prototype=window.Event.prototype,window.CustomEvent=e}();const u=(e,t,i,n)=>{const r=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:t,filterName:i,initialLoad:n}});e.dispatchEvent(r)},c=(e,t)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});e.dispatchEvent(i)},h=(e,t,i)=>{const{meta:n,refinements:r,component:s}=i,{clauses:l,options:a}=n;let o={clause:l[0].id,selected:"option-condition"===s?[a[0].id]:void 0};return r.forEach(e=>{var t;const{meta:i,component:n}=e,{clauses:r,options:s}=i;o[e.id]={clause:r[0].id,selected:"option-condition"===n?[null==(t=s[0])?void 0:t.id]:void 0}}),{depth:t,type:"criterion",condition_id:e,input:o}};class p extends t.Controller{connect(){this.element.refineStateController=this,this.changeDelegate=i.delegate("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((e,t)=>(e[t.id]=t,e),{}),this.loadingTimeout=null,c(this.element,this.blueprint)}disconnect(){i.abnegate("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(e){return this.conditionsLookup[e]}updateStableId(e){e!==this.stableId&&(this.stableId=e)}addGroup(){const{conditions:e}=this,t=e[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(h(t.id,1,t)),c(this.element,this.blueprint)}addCriterion(e){const{blueprint:t,conditions:i}=this,n=i[0];var r;t.splice(e+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},h(n.id,1,n)),c(this.element,this.blueprint)}deleteCriterion(e){const{blueprint:t}=this,i=t[e-1],n=t[e+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,l=r||!i,a=l&&s;i||n?t.splice(a&&r?e-1:a&&!i||l&&!s?e:e-1,2):this.blueprint=[],c(this.element,this.blueprint)}replaceCriterion(e,t,i){const n=this.blueprint[e];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(h));this.blueprint[e]=h(t,n.depth,i),c(this.element,this.blueprint)}updateInput(e,t,i){const{blueprint:n}=this,r=n[e],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...t}:r[i]={...r[i],...t},c(this.element,this.blueprint)}}p.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},p.targets=["loading"];class b extends t.Controller{connect(){this.idValue&&(e=>{const t=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(t)})()}updateStableIdField(e){if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:t}=e,{stableId:i}=t;this.stableIdFieldTarget.value=i}}activateSaveLink(e){const{detail:t}=e,{stableId:i,initialLoad:n}=t;if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget&&!n){const e=new URL(this.enabledSaveLinkTarget.href);e.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",e),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}b.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],b.values={id:Number,stableId:String,filterName:String};class m extends t.Controller{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(e){try{const t=this;(e=>{const t=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});window.dispatchEvent(t)})(t.blueprint);const i=e.detail.blueprint;return Promise.resolve(t.validateBlueprint(i)).then(function(e){if(e.stableId)t.stableIdValue=e.stableId,u(t.element,t.stableIdValue,t.filterNameValue),u(window,t.stableIdValue,t.filterNameValue);else{const{errors:t}=e;(e=>{let{blueprint:t,errors:i}=e;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:t,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:t})}})}catch(e){return Promise.reject(e)}}validateBlueprint(e){try{var t;const i=this;let n=JSON.stringify({blueprint:e,filter:i.filterNameValue}),r=null==(t=document.querySelector("meta[name='csrf-token']"))?void 0:t.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(e){return Promise.resolve(e.json()).then(function(t){return e.ok?{stableId:t.filter_id}:{errors:t.errors}})})}catch(e){return Promise.reject(e)}}}m.values={stableId:String,updateStableIdUrl:String,filterName:String},m.targets=[];class f extends s{initialize(){this.updateBlueprint=n.debounce((e,t,i)=>{this.value(e,t,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){s.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{id:e.target.value},e.target.dataset.inputId),this.submitForm()}clause(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{clause:e.target.value},e.target.dataset.inputId),this.submitForm()}selected(e){const{target:t}=e,i=Array.prototype.slice.call(t.options).filter(e=>e.selected).map(e=>e.value);this.value(e,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(e,t,i){const{criterionIdValue:n,state:r}=this,s=e.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:t=t||e.target.value},s.inputId)}date(e){const{picker:t}=e.detail,i=t.startDate.format("YYYY-MM-DD");this.value(e,i),this.submitForm()}createStableId(e,t){var i;const{state:n}=this;let r=JSON.stringify({blueprint:e,filter:t}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(e){n.updateStableId(e.filter_id)}})}condition(e){const{criterionIdValue:t,state:i}=this,n=e.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(t,n,r),this.submitForm()}cancelEnter(e){"Enter"===e.code&&(e.preventDefault(),e.stopPropagation())}}f.values={criterionId:Number,stableId:String};const v=[[l,"refine/add-controller.js"],[a,"refine/defaults-controller.js"],[o,"refine/delete-controller.js"],[s,"refine/form-controller.js"],[d,"refine/search-filter-controller.js"],[p,"refine/state-controller.js"],[b,"refine/stored-filter-controller.js"],[m,"refine/stabilize-filter-controller.js"],[f,"refine/update-controller.js"]].map(function(e){const t=e[0];return{identifier:r(e[1]),controllerConstructor:t}});e.AddController=l,e.DefaultsController=a,e.DeleteController=o,e.FormController=s,e.SearchFilterController=d,e.StabilizeFilterController=m,e.StateController=p,e.StoredFilterController=b,e.UpdateController=f,e.controllerDefinitions=v}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@hotwired/stimulus"),require("@rails/request.js"),require("jquery-events-to-dom-events"),require("lodash")):"function"==typeof define&&define.amd?define(["exports","@hotwired/stimulus","@rails/request.js","jquery-events-to-dom-events","lodash"],t):t((e||self).refineStimulus={},e.Stimulus,e.request_js,e.jqueryEventsToDomEvents,e.lodash)}(this,function(e,t,i,n,r){function s(e){const t=(e.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/)||[])[1];if(t)return t.replace(/_/g,"-").replace(/\//g,"--")}class l extends t.Controller{connect(){this.state=this.getStateController(),this.blueprintInput=this.addHiddenInput("blueprint"),this.addHiddenInput("filter",this.state.filterName),this.addHiddenInput("id_suffix",this.state.idSuffix),this.finishUpdate()}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}addHiddenInput(e,t){const i=document.createElement("input");return i.type="hidden",i.name=e,i.value=t||"",this.element.appendChild(i),i}finishUpdate(){this.state.finishUpdate()}startUpdate(){this.blueprintInput.value=JSON.stringify(this.state.blueprint),this.state.startUpdate()}submitForm(){this.startUpdate(),this.element.requestSubmit()}}class a extends l{criterion(){this.state.addCriterion(this.previousCriterionIdValue),this.startUpdate()}group(){this.state.addGroup(),this.startUpdate()}}a.values={previousCriterionId:Number};class o extends t.Controller{connect(){this.state=this.getStateController(),this.state.updateInput(this.criterionIdValue,this.inputValue)}getStateController(){let e=this.element;for(;e!==document.body;){const t=this.application.getControllerForElementAndIdentifier(e,"refine--state");if(t)return t;e=e.parentNode}return null}}o.values={criterionId:Number,input:Object};class d extends l{criterion(){const{state:e,criterionIdValue:t}=this;e.deleteCriterion(t),this.startUpdate()}}d.values={criterionId:Number};class u extends t.Controller{connect(){const e=new URLSearchParams(window.location.search);this.existingParams=e,this.existingParams.delete("stable_id")}search(e){e.preventDefault(),this.submitFilter(),document.activeElement.blur()}submitFilter(){try{const e=this,{blueprint:t}=e.stateController;return Promise.resolve(e.stabilizeFilterController.validateBlueprint(t)).then(function(i){i.stableId?e.redirectToStableId(i.stableId):e.fetchAndRenderInvalidFilter(t)})}catch(e){return Promise.reject(e)}}addHiddenInput(e){let{name:t,value:i}=e;const n=document.createElement("input");n.type="hidden",n.name=t,n.value=i,this.submissionFormTarget.appendChild(n)}get stateController(){return this.element.querySelector('[data-controller~="refine--state"]').refineStateController}get stabilizeFilterController(){return this.element.stabilizeFilterController}redirectToStableId(e){const t=new URLSearchParams;e&&t.append("stable_id",e);const i=new URLSearchParams({...Object.fromEntries(this.existingParams),...Object.fromEntries(t)}).toString(),n=window.location.pathname+"?"+i;history.pushState({},document.title,n),window.location.reload()}fetchAndRenderInvalidFilter(e){try{const t=this,n=new i.FetchRequest("POST",t.submitUrlValue,{responseKind:"turbo-stream",body:JSON.stringify({filter:t.stateController.filterName,blueprint:JSON.stringify(e),id_suffix:t.stateController.idSuffix})});return Promise.resolve(n.perform()).then(function(){})}catch(e){return Promise.reject(e)}}}u.values={submitUrl:String},u.targets=[],function(){if("function"==typeof window.CustomEvent)return!1;function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),i}e.prototype=window.Event.prototype,window.CustomEvent=e}();const c=(e,t,i)=>{const n=new CustomEvent("filter-stabilized",{bubbles:!0,cancelable:!0,detail:{stableId:t,filterName:i}});e.dispatchEvent(n)},h=(e,t)=>{const i=new CustomEvent("blueprint-updated",{bubbles:!0,cancelable:!0,detail:{blueprint:t}});e.dispatchEvent(i)},p=(e,t,i)=>{var n;const r=null==i?void 0:i.component,s=(null==i?void 0:i.meta)||{clauses:[],options:{}},l=(null==i?void 0:i.refinements)||[],{clauses:a,options:o}=s;let d={clause:null==(n=a[0])?void 0:n.id,selected:"option-condition"===r?[o[0].id]:void 0};return l.forEach(e=>{var t;const{meta:i,component:n}=e,{clauses:r,options:s}=i;d[e.id]={clause:r[0].id,selected:"option-condition"===n?[null==(t=s[0])?void 0:t.id]:void 0}}),{depth:t,type:"criterion",condition_id:e,input:d}};class b extends t.Controller{connect(){this.element.refineStateController=this,this.changeDelegate=n.delegate("change",["event","picker"]),this.blueprint=this.blueprintValue,this.conditions=this.conditionsValue,this.filterName=this.classNameValue,this.idSuffix=this.idSuffixValue,this.stableId=this.stableIdValue,this.conditionsLookup=this.conditions.reduce((e,t)=>(e[t.id]=t,e),{}),this.loadingTimeout=null,h(this.element,this.blueprint)}disconnect(){n.abnegate("change",this.changeDelegate)}startUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTimeout=window.setTimeout(()=>{document.activeElement.blur(),this.loadingTarget.classList.remove("hidden")},1e3)}finishUpdate(){this.loadingTimeout&&window.clearTimeout(this.loadingTimeout),this.loadingTarget.classList.add("hidden")}conditionConfigFor(e){return this.conditionsLookup[e]}updateStableId(e){e!==this.stableId&&(this.stableId=e)}addGroup(){const{conditions:e}=this,t=e[0];var i;this.blueprint.length>0&&this.blueprint.push({depth:i=void 0===i?0:i,type:"conjunction",word:"or"}),this.blueprint.push(p(t.id,1,t)),h(this.element,this.blueprint)}addCriterion(e){const{blueprint:t,conditions:i}=this,n=i[0];var r;t.splice(e+1,0,{depth:r=void 0===r?1:r,type:"conjunction",word:"and"},p(n.id,1,n)),h(this.element,this.blueprint)}deleteCriterion(e){const{blueprint:t}=this,i=t[e-1],n=t[e+1],r=i&&"or"===i.word,s=n&&"or"===n.word||!n,l=r||!i,a=l&&s;i||n?t.splice(a&&r?e-1:a&&!i||l&&!s?e:e-1,2):this.blueprint=[],h(this.element,this.blueprint)}replaceCriterion(e,t,i){const n=this.blueprint[e];if("criterion"!==n.type)throw new Error("You can't call updateConditionId on a non-criterion type. Trying to update "+JSON.stringify(p));this.blueprint[e]=p(t,n.depth,i),h(this.element,this.blueprint)}updateInput(e,t,i){const{blueprint:n}=this,r=n[e],s=(i=i||"input").split(", ");s.length>1?r[s[0]][s[1]]={...r[s[0]][s[1]],...t}:r[i]={...r[i],...t},h(this.element,this.blueprint)}}b.values={blueprint:Array,conditions:Array,className:String,stableId:String,idSuffix:String},b.targets=["loading"];class m extends t.Controller{connect(){this.idValue&&(e=>{const t=new CustomEvent("filter-stored",{bubbles:!0,cancelable:!0,detail:{storedFilterId:this.idValue}});window.dispatchEvent(t)})()}updateStableIdField(e){if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasStableIdFieldTarget){const{detail:t}=e,{stableId:i}=t;this.stableIdFieldTarget.value=i}}activateSaveLink(e){const{detail:t}=e,{stableId:i}=t;if(e.detail.filterName!=this.filterNameValue)return null;if(this.hasEnabledSaveLinkTarget&&this.hasDisabledSaveLinkTarget){const e=new URL(this.enabledSaveLinkTarget.href);e.searchParams.set("stable_id",i),this.enabledSaveLinkTarget.setAttribute("href",e),this.disabledSaveLinkTarget.classList.add("hidden"),this.enabledSaveLinkTarget.classList.remove("hidden")}}}m.targets=["enabledSaveLink","disabledSaveLink","stableIdField"],m.values={id:Number,stableId:String,filterName:String};class f extends t.Controller{connect(){this.element.stabilizeFilterController=this,this.stableIdValue=new URLSearchParams(window.location.search).get("stable_id")}updateStableId(e){try{const t=this;(e=>{const t=new CustomEvent("filter-unstable",{bubbles:!0,cancelable:!0,detail:{blueprint:e}});window.dispatchEvent(t)})(t.blueprint);const i=e.detail.blueprint;return Promise.resolve(t.validateBlueprint(i)).then(function(e){if(e.stableId)t.stableIdValue=e.stableId,c(t.element,t.stableIdValue,t.filterNameValue),c(window,t.stableIdValue,t.filterNameValue);else{const{errors:t}=e;(e=>{let{blueprint:t,errors:i}=e;const n=new CustomEvent("filter-invalid",{bubbles:!0,cancelable:!0,detail:{blueprint:t,errors:i}});window.dispatchEvent(n)})({blueprint:i,errors:t})}})}catch(e){return Promise.reject(e)}}validateBlueprint(e){try{var t;const i=this;let n=JSON.stringify({blueprint:e,filter:i.filterNameValue}),r=null==(t=document.querySelector("meta[name='csrf-token']"))?void 0:t.content;return Promise.resolve(fetch(i.updateStableIdUrlValue,{method:"PUT",body:n,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":r}})).then(function(e){return Promise.resolve(e.json()).then(function(t){return e.ok?{stableId:t.filter_id}:{errors:t.errors}})})}catch(e){return Promise.reject(e)}}}f.values={stableId:String,updateStableIdUrl:String,filterName:String},f.targets=[];class v extends l{initialize(){this.updateBlueprint=r.debounce((e,t,i)=>{this.value(e,t,i),this.createStableId(this.state.blueprint,this.state.filterName)},500)}connect(){l.prototype.connect.apply(this),this.state.updateStableId(this.stableIdValue)}refinedFilter(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{id:e.target.value},e.target.dataset.inputId),this.submitForm()}clause(e){const{criterionIdValue:t,state:i}=this;i.updateInput(t,{clause:e.target.value},e.target.dataset.inputId),this.submitForm()}selected(e){const{target:t}=e,i=Array.prototype.slice.call(t.options).filter(e=>e.selected).map(e=>e.value);this.value(e,i,"selected"),this.createStableId(this.state.blueprint,this.state.filterName)}value(e,t,i){const{criterionIdValue:n,state:r}=this,s=e.target.dataset;r.updateInput(n,{[i=i||s.inputKey||"value"]:t=t||e.target.value},s.inputId)}date(e){const{picker:t}=e.detail,i=t.startDate.format("YYYY-MM-DD");this.value(e,i),this.submitForm()}createStableId(e,t){var i;const{state:n}=this;let r=JSON.stringify({blueprint:e,filter:t}),s=null==(i=document.querySelector("meta[name='csrf-token']"))?void 0:i.content;$.ajax({type:"PUT",url:"/hammerstone/update_stable_id",data:r,headers:{accept:"application/json","content-type":"application/json","X-CSRF-Token":s},success:function(e){n.updateStableId(e.filter_id)}})}condition(e){const{criterionIdValue:t,state:i}=this,n=e.target.value,r=this.state.conditionConfigFor(n);i.replaceCriterion(t,n,r),this.submitForm()}cancelEnter(e){"Enter"===e.code&&(e.preventDefault(),e.stopPropagation())}}v.values={criterionId:Number,stableId:String};const g=[[a,"refine/add-controller.js"],[o,"refine/defaults-controller.js"],[d,"refine/delete-controller.js"],[l,"refine/form-controller.js"],[u,"refine/search-filter-controller.js"],[b,"refine/state-controller.js"],[m,"refine/stored-filter-controller.js"],[f,"refine/stabilize-filter-controller.js"],[v,"refine/update-controller.js"]].map(function(e){const t=e[0];return{identifier:s(e[1]),controllerConstructor:t}});e.AddController=a,e.DefaultsController=o,e.DeleteController=d,e.FormController=l,e.SearchFilterController=u,e.StabilizeFilterController=f,e.StateController=b,e.StoredFilterController=m,e.UpdateController=v,e.controllerDefinitions=g}); | ||
//# sourceMappingURL=refine-stimulus.umd.js.map |
import { Controller } from "@hotwired/stimulus" | ||
import { FetchRequest } from '@rails/request.js' | ||
export default class extends Controller { | ||
@@ -68,20 +70,16 @@ static values = { | ||
async fetchAndRenderInvalidFilter(blueprint) { | ||
const response = await fetch(this.submitUrlValue, { | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'X-CSRF-Token': document.querySelector("meta[name='csrf-token']")?.content | ||
}, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
filter: this.stateController.filterName, | ||
blueprint: JSON.stringify(blueprint), | ||
id_suffix: this.stateController.idSuffix | ||
}) | ||
}) | ||
const responseData = await response.json() | ||
const element = document.getElementById(responseData.target) | ||
element.outerHTML = responseData.template | ||
const request = new FetchRequest( | ||
"POST", | ||
this.submitUrlValue, | ||
{ | ||
responseKind: "turbo-stream", | ||
body: JSON.stringify({ | ||
filter: this.stateController.filterName, | ||
blueprint: JSON.stringify(blueprint), | ||
id_suffix: this.stateController.idSuffix | ||
}) | ||
} | ||
) | ||
await request.perform() | ||
} | ||
} |
@@ -6,3 +6,5 @@ import { Controller } from "@hotwired/stimulus" | ||
const criterion = (id, depth, condition) => { | ||
const { meta, refinements, component } = condition | ||
const component = condition?.component | ||
const meta = condition?.meta || { clauses: [], options: {}} | ||
const refinements = condition?.refinements || [] | ||
const { clauses, options } = meta | ||
@@ -13,3 +15,3 @@ const isOptionCondition = component === 'option-condition' | ||
let newInput = { | ||
clause: clauses[0].id, | ||
clause: clauses[0]?.id, | ||
selected: isOptionCondition ? [options[0].id] : undefined, | ||
@@ -16,0 +18,0 @@ } |
@@ -25,6 +25,6 @@ import { Controller } from "@hotwired/stimulus" | ||
const { detail } = event | ||
const { stableId, initialLoad } = detail | ||
const { stableId } = detail | ||
if (event.detail.filterName != this.filterNameValue) { return null } | ||
if (this.hasEnabledSaveLinkTarget && this.hasDisabledSaveLinkTarget && !initialLoad) { | ||
if (this.hasEnabledSaveLinkTarget && this.hasDisabledSaveLinkTarget) { | ||
const saveUrl = new URL(this.enabledSaveLinkTarget.href) | ||
@@ -31,0 +31,0 @@ saveUrl.searchParams.set('stable_id', stableId) |
@@ -21,3 +21,3 @@ // Polyfill for custom events in IE9-11 | ||
export const filterStabilizedEvent = (element, stableId, filterName, initialLoad) => { | ||
export const filterStabilizedEvent = (element, stableId, filterName) => { | ||
const event = new CustomEvent('filter-stabilized', { | ||
@@ -29,3 +29,2 @@ bubbles: true, | ||
filterName, | ||
initialLoad, | ||
}, | ||
@@ -32,0 +31,0 @@ }) |
@@ -0,1 +1,8 @@ | ||
### 2.2.2 2022-09-07 | ||
* bug-fix | ||
* show both error messages if neither date is filled in when using "betweens" | ||
* Use turbo streams instead of json in create action https://github.com/hammerstonedev/refine-rails/pull/48 | ||
* Add rake task so customer can add gem to `tmp/gems` (must be run by customer) to pick up gem styling | ||
* Use local partial | ||
### 2.2.1 2022-08-30 | ||
@@ -2,0 +9,0 @@ * bug-fix |
{ | ||
"name": "@hammerstone/refine-stimulus", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "Refine is a flexible query builder for your apps. It lets your users filter down to exactly what they're looking for. Completely configured on the backend.", | ||
@@ -59,2 +59,3 @@ "browserslist": [ | ||
"@hotwired/stimulus": "^3.1.0", | ||
"@rails/request.js": "^0.0.6", | ||
"jquery-events-to-dom-events": "^1.1.0", | ||
@@ -61,0 +62,0 @@ "lodash": "^4.0.8" |
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
221510
4
813
+ Added@rails/request.js@^0.0.6
+ Added@rails/request.js@0.0.6(transitive)