From a7b32cffe34ec9e22b1a7bf60f4811ff017c174c Mon Sep 17 00:00:00 2001 From: lukasmartinelli Date: Tue, 20 Sep 2016 16:49:02 +0200 Subject: [PATCH] Improve optional metadata handling --- src/style.js | 26 +++++++++++++++++--------- src/stylestore.js | 12 +++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/style.js b/src/style.js index b781fdc..e755251 100644 --- a/src/style.js +++ b/src/style.js @@ -13,15 +13,7 @@ function fromJSON(jsonStyle) { jsonStyle = JSON.parse(jsonStyle) } - if(!('id' in jsonStyle)) { - jsonStyle.id = Math.random().toString(36).substr(2, 9) - } - - if(!('created' in jsonStyle)) { - jsonStyle.created = new Date().toJSON() - } - - return new Immutable.Map(Object.keys(jsonStyle).map(key => { + return Immutable.Map(Object.keys(jsonStyle).map(key => { const val = jsonStyle[key] if(key === "layers") { return [key, Immutable.OrderedMap(val.map(l => [l.id, Immutable.fromJS(l)]))] @@ -33,6 +25,21 @@ function fromJSON(jsonStyle) { })) } +function ensureHasId(style) { + if(style.has('id')) return style + console.log('has no id', style.toJS()) + return style.set('id', Math.random().toString(36).substr(2, 9)) +} + +function ensureHasTimestamp(style) { + if(style.has('id')) return style + return style.set('created', new Date().toJSON()) +} + +function ensureMetadataExists(style) { + return ensureHasId(ensureHasTimestamp(style)) +} + // Compare style with other style and return changes //TODO: Write own diff algo that operates on immutable collections // Should be able to improve performance since we can only compare @@ -74,4 +81,5 @@ export default { toJSON, fromJSON, diffStyles, + ensureMetadataExists, } diff --git a/src/stylestore.js b/src/stylestore.js index e6c1667..6c2b0b6 100644 --- a/src/stylestore.js +++ b/src/stylestore.js @@ -3,6 +3,7 @@ import Immutable from 'immutable' import style from './style.js' const storagePrefix = "maputnik" +const stylePrefix = 'style' const storageKeys = { latest: [storagePrefix, 'latest_style'].join(':'), accessToken: [storagePrefix, 'access_token'].join(':') @@ -40,8 +41,8 @@ export function loadDefaultStyle(cb) { // Return style ids and dates of all styles stored in local storage function loadStoredStyles() { const styles = [] - for (let i = 0; i < localStorage.length; i++) { - const key = localStorage.key(i) + for (let i = 0; i < window.localStorage.length; i++) { + const key = window.localStorage.key(i) if(isStyleKey(key)) { styles.push(fromKey(key)) } @@ -51,7 +52,7 @@ function loadStoredStyles() { function isStyleKey(key) { const parts = key.split(":") - return parts.length == 2 && parts[0] === storagePrefix + return parts.length == 3 && parts[0] === storagePrefix && parts[1] === stylePrefix } // Load style id from key @@ -61,13 +62,13 @@ function fromKey(key) { } const parts = key.split(":") - const styleId = parts[1] + const styleId = parts[2] return styleId } // Calculate key that identifies the style with a version function styleKey(styleId) { - return [storagePrefix, styleId].join(":") + return [storagePrefix, stylePrefix, styleId].join(":") } // Store style independent settings @@ -101,6 +102,7 @@ export class StyleStore { // Save current style replacing previous version save(mapStyle) { + mapStyle = style.ensureMetadataExists(mapStyle) const key = styleKey(mapStyle.get('id')) window.localStorage.setItem(key, JSON.stringify(style.toJSON(mapStyle))) window.localStorage.setItem(storageKeys.latest, mapStyle.get('id'))