api and utility migrations

This commit is contained in:
Stewart Allen 2022-02-18 10:52:49 -05:00
commit 72dcb2368e
6 changed files with 78 additions and 74 deletions

View file

@ -7,7 +7,7 @@
gapp.register('data.local'); gapp.register('data.local');
let data = self.data = self.data || {}; let data = self.data = self.data || {};
if (data.Local) return; if (data.local) return;
function Local() { function Local() {
this.__data__ = {}; this.__data__ = {};
@ -34,13 +34,14 @@ LS.clear = function() {
}; };
try { try {
let local = data.Local = self.localStorage; // deprecate 'Local' at some point
let local = data.local =self.localStorage;
let testkey = '__test'; let testkey = '__test';
local.setItem(testkey, 1); local.setItem(testkey, 1);
local.getItem(testkey); local.getItem(testkey);
local.removeItem(testkey); local.removeItem(testkey);
} catch (e) { } catch (e) {
data.Local = new Local(); data.local = new Local();
let msg = "localStorage disabled: application may not function properly"; let msg = "localStorage disabled: application may not function properly";
console.log(msg); console.log(msg);
// alert(msg); // alert(msg);

View file

@ -8,10 +8,11 @@
BASE = self.base, BASE = self.base,
UTIL = BASE.util, UTIL = BASE.util,
POLY = BASE.polygons, POLY = BASE.polygons,
PATH = BASE.paths,
FDM = KIRI.driver.FDM, FDM = KIRI.driver.FDM,
newPoint = BASE.newPoint, { newPoint, newPolygon } = BASE,
newPolygon = BASE.newPolygon, { getRangeParameters } = FDM,
getRangeParameters = FDM.getRangeParameters, { poly2polyEmit } = PATH,
debug = false; debug = false;
/** /**
@ -185,7 +186,7 @@
// output brim points // output brim points
let brimStart = offset < nozzle * 2 ? newPoint(-bedWidth, -bedDepth, 0) : printPoint; let brimStart = offset < nozzle * 2 ? newPoint(-bedWidth, -bedDepth, 0) : printPoint;
printPoint = print.poly2polyEmit(polys, brimStart, (poly, index, count, startPoint) => { printPoint = poly2polyEmit(polys, brimStart, (poly, index, count, startPoint) => {
return print.polyPrintPath(poly, startPoint, preout, { return print.polyPrintPath(poly, startPoint, preout, {
rate: firstLayerRate, rate: firstLayerRate,
onfirst: function(point) { onfirst: function(point) {
@ -596,7 +597,7 @@
print.setType('shield'); print.setType('shield');
shield = POLY.setZ(shield.clone(), printPoint.z); shield = POLY.setZ(shield.clone(), printPoint.z);
let preout = []; let preout = [];
printPoint = print.poly2polyEmit(shield, printPoint, (poly, index, count, startPoint) => { printPoint = poly2polyEmit(shield, printPoint, (poly, index, count, startPoint) => {
return print.polyPrintPath(poly, startPoint, preout, { return print.polyPrintPath(poly, startPoint, preout, {
onfirst: function(point) { onfirst: function(point) {
if (preout.length && point.distTo2D(startPoint) > 2) { if (preout.length && point.distTo2D(startPoint) > 2) {

View file

@ -4,8 +4,10 @@
(function () { (function () {
const { kiri } = self; const { data, kiri } = self;
const { consts } = kiri; const { consts, utils } = kiri;
const { areEqual, parseOpt, encodeOpt, ajax, o2js, js2o } = utils;
const { LISTS } = consts;
const feature = { const feature = {
seed: true, // seed profiles on first use seed: true, // seed profiles on first use
@ -26,8 +28,40 @@ const feature = {
pmode: consts.PMODES.SPEED // preview modes pmode: consts.PMODES.SPEED // preview modes
}; };
const devel = {
xray: (layers, raw) => {
let proc = api.conf.get().process;
let size = proc.sliceHeight || proc.slaSlice || 1;
layers = Array.isArray(layers) ? layers : [ layers ];
proc.xray = layers.map(l => raw ? l : l * size + size / 2);
api.function.slice();
}
};
const tweak = {
line_precision(v) { api.work.config({base:{clipperClean: v}}) },
gcode_decimals(v) { api.work.config({base:{gcode_decimals: v}}) }
};
const api = kiri.api = { const api = kiri.api = {
feature clone: Object.clone,
sdb: data.local,
ajax: ajax,
js2o: js2o,
o2js: o2js,
lists: LISTS,
doit: {
undo: noop, // set in do.js
redo: noop // set in do.js
},
var: {
layer_lo: 0,
layer_hi: 0,
layer_max: 0
},
feature,
devel,
tweak,
}; };
})(); })();

View file

@ -5,10 +5,10 @@
(function () { (function () {
const { base, data, load, kiri, moto } = self; const { base, data, load, kiri, moto } = self;
const { api, consts, lang, Widget, newWidget } = kiri; const { api, consts, lang, Widget, newWidget, utils } = kiri;
const { areEqual, parseOpt, encodeOpt, ajax, o2js, js2o } = utils;
const { feature, platform } = api; const { feature, platform } = api;
const { COLOR, LISTS, PMODES, MODES, VIEWS } = kiri.consts; const { COLOR, MODES, PMODES, VIEWS } = consts;
const { parseOpt, encodeOpt, ajax, o2js, js2o } = kiri.utils;
const LANG = lang.current, const LANG = lang.current,
WIN = self.window, WIN = self.window,
@ -19,7 +19,7 @@
SECURE = isSecure(LOC.protocol), SECURE = isSecure(LOC.protocol),
LOCAL = self.debug && !SETUP.remote, LOCAL = self.debug && !SETUP.remote,
EVENT = kiri.broker = gapp.broker, EVENT = kiri.broker = gapp.broker,
SDB = data.Local, SDB = data.local,
ODB = kiri.odb = new data.Index(SETUP.d ? SETUP.d[0] : 'kiri'), ODB = kiri.odb = new data.Index(SETUP.d ? SETUP.d[0] : 'kiri'),
// K3DB = kiri.wdb = new data.Index('kiri3', { stores:["file","work"] }).init(), // K3DB = kiri.wdb = new data.Index('kiri3', { stores:["file","work"] }).init(),
SPACE = kiri.space = moto.Space, SPACE = kiri.space = moto.Space,
@ -90,30 +90,10 @@
} }
}; };
const tweak = {
line_precision: (v) => { api.work.config({base:{clipperClean: v}}) },
gcode_decimals: (v) => { api.work.config({base:{gcode_decimals: v}}) }
};
const devel = {
xray: (layers, raw) => {
let proc = api.conf.get().process;
let size = proc.sliceHeight || proc.slaSlice || 1;
layers = Array.isArray(layers) ? layers : [ layers ];
proc.xray = layers.map(l => raw ? l : l * size + size / 2);
api.function.slice();
}
};
// augment api // augment api
Object.assign(api, { Object.assign(api, {
ui: UI, ui: UI,
uc: UC, uc: UC,
sdb: SDB,
o2js: o2js,
js2o: js2o,
ajax: ajax,
clone: clone,
focus: noop, focus: noop,
stats: STATS, stats: STATS,
catalog: CATALOG, catalog: CATALOG,
@ -141,16 +121,6 @@
SECURE, SECURE,
STACKS, STACKS,
}, },
devel,
doit: {
undo: noop, // set in do.js
redo: noop // set in do.js
},
var: {
layer_lo: 0,
layer_hi: 0,
layer_max: 0
},
device: { device: {
code: currentDeviceCode, code: currentDeviceCode,
get: currentDeviceName, get: currentDeviceName,
@ -173,7 +143,6 @@
alerts: updateAlerts, alerts: updateAlerts,
settings: triggerSettingsEvent settings: triggerSettingsEvent
}, },
feature,
function: { function: {
slice: prepareSlices, slice: prepareSlices,
print: preparePreview, print: preparePreview,
@ -198,7 +167,6 @@
convert: loadImageConvert convert: loadImageConvert
}, },
language: kiri.lang, language: kiri.lang,
lists: LISTS,
modal: { modal: {
show: showModal, show: showModal,
hide: hideModal, hide: hideModal,
@ -249,7 +217,6 @@
update: SPACE.update, update: SPACE.update,
is_dark() { return settings.controller.dark } is_dark() { return settings.controller.dark }
}, },
tweak,
util: { util: {
isSecure, isSecure,
ui2rec: updateSettingsFromFields, ui2rec: updateSettingsFromFields,
@ -673,7 +640,7 @@
let slice = slices[i]; let slice = slices[i];
if (slice && !range) { if (slice && !range) {
range = {lo: i, hi: i, fields: slice}; range = {lo: i, hi: i, fields: slice};
} else if (slice && range && isEquals(range.fields, slice)) { } else if (slice && range && areEqual(range.fields, slice)) {
range.hi = i; range.hi = i;
} else if (range) { } else if (range) {
ranges.push(range); ranges.push(range);
@ -1691,29 +1658,6 @@
} }
} }
function isEquals(o1, o2) {
if (o1 == o2) return true;
if (Array.isArray(o1) && Array.isArray(o2)) {
if (o1.length === o2.length) {
for (let i=0; i<o1.length; i++) {
if (o1[i] !== o2[i]) {
return false;
}
}
return true;
}
} else if (typeof(o1) === 'object' && typeof(o2) === 'object') {
let keys = Object.keys(Object.assign({}, o1, o2));
for (let key of keys) {
if (o1[key] !== o2[key]) {
return false;
}
}
return true;
}
return false;
}
/** /**
* @returns {Object} * @returns {Object}
*/ */
@ -1762,7 +1706,7 @@
if (lastChange === uie) { if (lastChange === uie) {
if (changes) changes[key] = nval; if (changes) changes[key] = nval;
} }
if (!isEquals(setrec[key], nval)) { if (!areEqual(setrec[key], nval)) {
setrec[key] = nval; setrec[key] = nval;
if (changes) { if (changes) {
changes[key] = nval; changes[key] = nval;

View file

@ -46,7 +46,31 @@ function js2o(s,def) {
} }
} }
function areEqual(o1, o2) {
if (o1 == o2) return true;
if (Array.isArray(o1) && Array.isArray(o2)) {
if (o1.length === o2.length) {
for (let i=0; i<o1.length; i++) {
if (o1[i] !== o2[i]) {
return false;
}
}
return true;
}
} else if (typeof(o1) === 'object' && typeof(o2) === 'object') {
let keys = Object.keys(Object.assign({}, o1, o2));
for (let key of keys) {
if (o1[key] !== o2[key]) {
return false;
}
}
return true;
}
return false;
}
kiri.utils = { kiri.utils = {
areEqual,
parseOpt, parseOpt,
encodeOpt, encodeOpt,
ajax, ajax,

View file

@ -16,7 +16,7 @@
}; };
const AP = Ajax.prototype, const AP = Ajax.prototype,
KV = data.Local, KV = data.local,
KEY = "moto-ajax", KEY = "moto-ajax",
TIME = function() { return new Date().getTime() }, TIME = function() { return new Date().getTime() },
MOKEY = moto.id = KV.getItem(KEY) || (TIME().toString(36)+rnd()+rnd()); MOKEY = moto.id = KV.getItem(KEY) || (TIME().toString(36)+rnd()+rnd());