UNPKG

dropkiq

Version:

Liquid Expressions Simplified

1 lines 4.07 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var constants_1=require("./constants"),DataSchema_1=require("./DataSchema"),CaretTagFinder_1=require("./CaretTagFinder"),Authorizer_1=require("./Authorizer"),OutputAnalyzer_1=require("./OutputAnalyzer"),context_1=require("./liquidjs/src/context/context"),liquid_options_1=require("./liquidjs/src/liquid-options"),tokenizer_1=require("./liquidjs/src/parser/tokenizer"),liquid_1=require("./liquidjs/src/liquid"),parser_1=require("./liquidjs/src/parser/parser"),render_1=require("./liquidjs/src/render/render"),async_1=require("./liquidjs/src/util/async"),DropkiqEngine=function(){function t(t,e,i,r,s,n,o){if(void 0===n&&(n=""),void 0===o&&(o={}),this.opts={},this.options=liquid_options_1.applyDefault(liquid_options_1.normalize(this.opts)),this.liquid=new liquid_1.Liquid(this.opts),this.tokenizer=new tokenizer_1.Tokenizer(this.options),this.parser=new parser_1.default(this.liquid),this.renderer=new render_1.Render,this.optionsFromUI=o,this.allowEmptySchema=this.optionsFromUI.allowEmptySchema||!1,this.suggestionFilter=function(){},"function"==typeof o.suggestionFilter&&(this.suggestionFilter=o.suggestionFilter),!this.allowEmptySchema){if(!i)throw new Error("Dropkiq schema is empty. Please double check you're loading the dropkiq_schema.yaml file correctly.");0===Object.keys(i).length&&console.log("WARNING: Dropkiq schema object is empty.")}this.schema=i,this.context=r,this.scope=s,this.licenseKey=n,this.result={},this.dataSchema=new DataSchema_1.DataSchema(i,r,s),this.authorizer=new Authorizer_1.Authorizer(this.licenseKey),this.authorizer.authorize(),this.setTextBodyAndCaretPositionWithUpdate(t,e)}return t.prototype.update=function(t,e){return this.setTextBodyAndCaretPositionWithUpdate(t,e)},t.prototype.updateScope=function(t){return this.scope=t,this.dataSchema=new DataSchema_1.DataSchema(this.schema,this.context,t),this.setTextBodyAndCaretPositionWithUpdate(this.rawTextBody,this.caretIndex)},t.prototype.registerFilter=function(t,e,i,r,s){this.liquid.registerFilter(t,e);var n={};n[t]={type:"ColumnTypes::Filter",foreign_table_name:null,insertionTemplate:i,selectRange:r,hint:s},this.dataSchema.filterRegistry.registerFilter(n)},t.prototype.setTextBodyAndCaretPositionWithUpdate=function(t,e){this.rawTextBody=t,this.caretIndex=e,this.setRowAndColumnFromIndex();try{this.tokens=this.tokenizer.tokenize(this.rawTextBody)}catch(t){if(this.state=constants_1.ERROR,"TokenizationError"===t.name)return{message:"Liquid is invalid (possibly because of unmatched {{ or {% braces)"};throw t}return this.caretTagFinder=new CaretTagFinder_1.CaretTagFinder(this.tokens,this.caretRow,this.caretColumn),this.state=this.caretTagFinder.findState(),this.caretToken=this.caretTagFinder.findCaretToken(),this.caretLiquidPosition=this.caretTagFinder.findCaretLiquidPosition(),this.findResult()},t.prototype.findResult=function(){if(this.result={renderedDocument:this.renderedDocument()},!this.caretToken)return this.result;switch(this.state){case constants_1.HTML:case constants_1.TAG:this.result;break;case constants_1.OUTPUT:this.templates=this.parser.parse(this.tokens);var t=new OutputAnalyzer_1.OutputAnalyzer(this.templates,this.caretToken,this.caretLiquidPosition,this.dataSchema,this.authorizer,this.suggestionFilter);this.result=Object.assign(this.result,t.analyze());break;default:throw new Error('Unknown Dropkiq state "'+this.state+'"')}return this.result},t.prototype.renderedDocument=function(){var t=this.tokenizer.tokenize(this.rawTextBody),e=this.parser.parse(t),i=new context_1.Context(this.scope,this.options,!0);try{return async_1.toValue(this.renderer.renderTemplates(e,i))}catch(t){if("RenderError"===t.name)return t.message;throw t}},t.prototype.setRowAndColumnFromIndex=function(){this.rawLeftTextBody=this.rawTextBody.slice(0,this.caretIndex),this.rawRightTextBody=this.rawTextBody.slice(this.caretIndex,this.rawTextBody.length);var t=this.rawLeftTextBody.split(/\r?\n/g),e=t.length-1,i=t[e].length;this.caretRow=e,this.caretColumn=i},t}();exports.DropkiqEngine=DropkiqEngine;