Improve optional metadata handling

This commit is contained in:
lukasmartinelli 2016-09-20 16:49:02 +02:00
parent adc00138a8
commit a7b32cffe3
2 changed files with 24 additions and 14 deletions

View file

@ -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,
}

View file

@ -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'))