From 43145fea669890b1390be3d598f3178fd5766be1 Mon Sep 17 00:00:00 2001 From: Edoardo Conti Date: Thu, 15 Apr 2021 12:53:07 -0500 Subject: [PATCH] Update refinery.js and minify --- static/js/refinery.js | 17 ++++++++++------- static/minified/refinery.min.js | 1 + templates/home.html | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/static/js/refinery.js b/static/js/refinery.js index 724f6a4..51a9956 100644 --- a/static/js/refinery.js +++ b/static/js/refinery.js @@ -2,19 +2,19 @@ window.refinery = window.refinery || {}; refinery.RefineryAPI = function (config = {}) { this.storage = window.localStorage; - this.refineryApikey = this.getApiKeyFromScriptTag(); + this.refineryAppToken = this.getAppTokenFromScriptTag(); let defaultConfig = { debugMode: false, - refineryHostUrl: "https://app.banditml.com/api/v2", + refineryHostUrl: "https://app.banditml.com/api/v2/", }; this.config = Object.assign(defaultConfig, config); this.refineryLiveRefinementEndpoint = `${this.config.refineryHostUrl}changesets`; }; -refinery.RefineryAPI.prototype.getApiKeyFromScriptTag = function () { - return document.currentScript.getAttribute('apiKey'); +refinery.RefineryAPI.prototype.getAppTokenFromScriptTag = function () { + return document.currentScript.getAttribute('appToken'); }; refinery.RefineryAPI.prototype.asyncGetRequest = async function( @@ -53,7 +53,7 @@ refinery.RefineryAPI.prototype.getLiveRefinement = function() { url = self.refineryLiveRefinementEndpoint, params = {}, headers = { - "x-chrome-installation": `${self.refineryApikey}` + "x-app-token": `${self.refineryAppToken}` } ); return changesPromise.then(response => { @@ -67,8 +67,11 @@ refinery.RefineryAPI.prototype.applyChanges = function() { const self = this; self.getLiveRefinement().then(response => { // right now we assume 1 changeset only, so grab the first i.e. [0] - let changes = response[0].changes; - changes.forEach(change => { + let changeset = response[0]; + let changes; + (changeset.publish) ? changes = changeset.changes : changes = []; + + changes.filter(change => !change.deleted).forEach(change => { if (window.location.href === change.href) { let elem = document.querySelector(change.domPath); if (elem.innerHTML !== change.beforeHtml) { diff --git a/static/minified/refinery.min.js b/static/minified/refinery.min.js index e69de29..e1aab5d 100644 --- a/static/minified/refinery.min.js +++ b/static/minified/refinery.min.js @@ -0,0 +1 @@ +window.refinery=window.refinery||{},refinery.RefineryAPI=function(e={}){this.storage=window.localStorage,this.refineryAppToken=this.getAppTokenFromScriptTag();this.config=Object.assign({debugMode:!1,refineryHostUrl:"https://app.banditml.com/api/v2/"},e),this.refineryLiveRefinementEndpoint=`${this.config.refineryHostUrl}changesets`},refinery.RefineryAPI.prototype.getAppTokenFromScriptTag=function(){return document.currentScript.getAttribute("appToken")},refinery.RefineryAPI.prototype.asyncGetRequest=async function(e,n={},t={}){n&&Object.keys(n).length&&(e+="?");for(const t in n){let r=n[t];if(null!=t&&null!=r){const n=typeof r;let i;e+=`${t}=${i="number"===n||"string"===n?r:encodeURIComponent(JSON.stringify(r))}&`}}const r=await fetch(e,{method:"GET",headers:t});return await r.json()},refinery.RefineryAPI.prototype.getLiveRefinement=function(){return this.asyncGetRequest(url=this.refineryLiveRefinementEndpoint,params={},headers={"x-app-token":`${this.refineryAppToken}`}).then(e=>e.results).catch(e=>{console.error("Failed to fetch changes",e)})},refinery.RefineryAPI.prototype.applyChanges=function(){this.getLiveRefinement().then(e=>{let n,t=e[0];(n=t.publish?t.changes:[]).filter(e=>!e.deleted).forEach(e=>{if(window.location.href===e.href){let n=document.querySelector(e.domPath);n.innerHTML!==e.beforeHtml?console.warn(`Can't apply change on '${e.beforeHtml}' because copy doesn't match. '${e.beforeHtml}' vs. '${n.innerHTML}'.`):n.innerHTML=e.afterHtml}else console.warn(`\n Can't apply change on '${e.beforeHtml}' because href's don't match. '${e.href}' vs. '${window.location.href}'.`)})})};let refineryAPI=new refinery.RefineryAPI;refineryAPI.applyChanges(); diff --git a/templates/home.html b/templates/home.html index b882cd1..afaeeb8 100644 --- a/templates/home.html +++ b/templates/home.html @@ -4,7 +4,7 @@ Welcome - +