UNPKG

dropkiq

Version:

Liquid Expressions Simplified

1 lines 5.86 kB
"use strict";var __read=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var i,s,n=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(i=n.next()).done;)a.push(i.value)}catch(e){s={error:e}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(s)throw s.error}}return a},__spread=this&&this.__spread||function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(__read(arguments[t]));return e};Object.defineProperty(exports,"__esModule",{value:!0});var ScopeCapture_1=require("./ScopeCapture"),LivePreviewer_1=require("./LivePreviewer"),OutputAnalyzer=function(){function e(e,t,r,i,s,n){void 0===n&&(n=function(){}),this.templates=e,this.caretToken=t,this.caretLiquidPosition=r,this.dataSchema=i,this.authorizer=s,this.suggestionFilter=n,this.rawText=this.caretToken.raw,this.leftCaretText=this.rawText.slice(0,this.caretLiquidPosition),this.rightCaretText=this.rawText.slice(this.caretLiquidPosition,this.rawText.length)}return e.prototype.analyze=function(){var e=this,t={preview:"",suggestions:[],prefix:""},r=new ScopeCapture_1.ScopeCapture(__spread(this.templates),this.caretToken,this.dataSchema);r.capture(this.dataSchema.scope);var i=r.getCapturedRendered();if(i&&(t.preview=i),this.canMakeSuggestion()){var s=this.getPathFromRawText(),n=s.pop(),a=[];this.dataSchema.setCurrentPath(s);var o=this.getContextWithScopeOptions();this.dataSchema.buildPathSchema(a,o);var p=a[a.length-1];if(p&&"ColumnTypes::HasOne"!==p.type)throw new Error("End of Liquid expression reached");var u=void 0;if(p&&!this.authorizer.authorized()){var h=this.dataSchema.nextOptions(a,o);u={dropkiq:{type:"ColumnTypes::String",foreign_table_name:null,hint:Object.keys(h).length+" "+p.name+" Liquid methods are hidden in the free version of Dropkiq."}}}else u=this.dataSchema.nextOptions(a,o);u=u||{},n&&(u=this.filterResults(n,u)),this.livePreviewer=new LivePreviewer_1.LivePreviewer(s,u,this.templates,this.caretToken,this.dataSchema,this.caretLiquidPosition),u=this.livePreviewer.suggestionsWithPreviews(),t.pathSchema=a,t.prefix=n||"",t.suggestions=u||{},t.suggestionsArray=Object.keys(t.suggestions).map((function(r){var i=t.suggestions[r];return Object.assign({},i,{name:r,prefix:n,nameWithoutPrefix:e.nameWithoutPrefix(r,t.prefix),iconImageURLForSuggestion:e.iconImageURLForSuggestion(i.type)})})),this.suggestionFilter(t.suggestionsArray)}else if(this.canMakeFilterSuggestion()&&this.authorizer.authorized()){var c=this.getFilterPrefix();u=this.dataSchema.filterRegistry.registry;c&&(u=this.filterResults(c,u)),this.livePreviewer=new LivePreviewer_1.LivePreviewer([],u,this.templates,this.caretToken,this.dataSchema,this.caretLiquidPosition),u=this.livePreviewer.suggestionsWithPreviews(),t.pathSchema=[],t.prefix=c||"",t.suggestions=u||{},t.suggestionsArray=Object.keys(t.suggestions).map((function(r){var i=t.suggestions[r];return Object.assign({},i,{name:r,prefix:c,nameWithoutPrefix:e.nameWithoutPrefix(r,t.prefix),iconImageURLForSuggestion:e.iconImageURLForSuggestion(i.type),template:i.template,selectRange:i.selectRange})})),this.suggestionFilter(t.suggestionsArray)}return t},e.prototype.canMakeSuggestion=function(){return!this.cursorIsInBraces()&&this.cursorIsLeftOfFilters()&&this.pastRightMostPeriod()&&!this.spaceExistsAfterContent()&&this.rightCharacterIsEligible()},e.prototype.canMakeFilterSuggestion=function(){return!this.cursorIsInBraces()&&this.cursorIsRightOfFilters()&&!this.spaceOrColonExistsAfterContent()&&this.rightCharacterIsEligible()},e.prototype.cursorIsInBraces=function(){return!this.leftCaretText.includes("{{")||!this.rightCaretText.includes("}}")},e.prototype.cursorIsLeftOfFilters=function(){return!this.leftCaretText.includes("|")},e.prototype.cursorIsRightOfFilters=function(){return!this.cursorIsLeftOfFilters()},e.prototype.pastRightMostPeriod=function(){return!this.rightCaretText.includes(".")},e.prototype.rightCharacterIsEligible=function(){var e=this.rightCaretText[0];return" "===e||e==String.fromCharCode(160)||"|"===e||"}"===e},e.prototype.spaceExistsAfterContent=function(){var e=this.leftCaretText.replace(/\{+/,"");return new RegExp(/\S+\s+/).test(e)},e.prototype.spaceOrColonExistsAfterContent=function(){var e=this.leftCaretText.replace(/\{+.*\|/,"");return new RegExp(/(\s+)?\S+(\s+|\:)/).test(e)},e.prototype.getFilterPrefix=function(){var e=this.leftCaretText.replace(/\{+.*\|/,""),t=new RegExp(/\s+?(\S+)/),r=e.match(t);return r?r[1]:""},e.prototype.getPathFromRawText=function(){var e=this.rawText.replace("{{","").replace("}}","").split("|")[0].trim();return 0===e.length?[]:e.split(".")},e.prototype.filterResults=function(e,t){var r=Object.assign({},t);return Object.keys(r).forEach((function(t){r[t];t===e&&delete r[t],t.startsWith(e)||delete r[t]})),r},e.prototype.getContextWithScopeOptions=function(){var e=new ScopeCapture_1.ScopeCapture(this.templates,this.caretToken,this.dataSchema).optionsFromScope(),t=JSON.parse(JSON.stringify(this.dataSchema.context));return this.authorizer.authorized()?Object.assign(t,e):Object.assign(t,{})},e.prototype.nameWithoutPrefix=function(e,t){return e.slice(t.length,e.length)},e.prototype.iconImageURLForSuggestion=function(e){switch(e){case"ColumnTypes::Filter":return"https://app.dropkiq.com/plugin/filter1.png";case"ColumnTypes::HasMany":return"https://app.dropkiq.com/plugin/array.png";case"ColumnTypes::HasOne":case"ColumnTypes::YAML":return"https://app.dropkiq.com/plugin/object.png";case"ColumnTypes::String":return"https://app.dropkiq.com/plugin/string.png";case"ColumnTypes::Numeric":return"https://app.dropkiq.com/plugin/numeric.png";case"ColumnTypes::Boolean":return"https://app.dropkiq.com/plugin/boolean.png";case"ColumnTypes::DateTime":return"https://app.dropkiq.com/plugin/date.png";case"ColumnTypes::Text":return"https://app.dropkiq.com/plugin/string.png"}},e}();exports.OutputAnalyzer=OutputAnalyzer;