From c85971e5e6b152aa8e1ce1921e2ea9ca5395f639 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Tue, 22 Apr 2014 17:56:41 -0700 Subject: [PATCH] Added i18n population action to the level editor. --- app/lib/deltas.coffee | 2 +- app/models/CocoModel.coffee | 23 +++++++++++++++++++++++ app/templates/editor/level/edit.jade | 2 ++ app/treema-ext.coffee | 1 + app/views/editor/level/edit.coffee | 1 + 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/lib/deltas.coffee b/app/lib/deltas.coffee index de6d6c13b..e817551d1 100644 --- a/app/lib/deltas.coffee +++ b/app/lib/deltas.coffee @@ -68,7 +68,7 @@ expandFlattenedDelta = (delta, left, schema) -> parentLeft = left parentSchema = schema for key, i in delta.dataPath - # TODO: A more comprehensive way of getting child schemas + # TODO: Better schema/json walking childSchema = parentSchema?.items or parentSchema?.properties?[key] or {} childLeft = parentLeft?[key] humanKey = null diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 3ecffd12e..1c93ea125 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -133,6 +133,7 @@ class CocoModel extends Backbone.Model schema ?= @schema() models = [] + # TODO: Better schema/json walking if $.isArray(data) and schema.items? for subData, i in data models = models.concat(@getReferencedModels(subData, schema.items, path+i+'/', shouldLoadProjection)) @@ -226,5 +227,27 @@ class CocoModel extends Backbone.Model watching: -> return me.id in (@get('watchers') or []) + + populateI18N: (data, schema, path='') -> + # TODO: Better schema/json walking + sum = 0 + data ?= $.extend true, {}, @attributes + schema ?= @schema() or {} + if schema.properties?.i18n and _.isPlainObject(data) and not data.i18n? + data.i18n = {} + sum += 1 + + if _.isPlainObject data + for key, value of data + numChanged = 0 + numChanged = @populateI18N(value, childSchema, path+'/'+key) if childSchema = schema.properties?[key] + if numChanged and not path # should only do this for the root object + @set key, value + sum += numChanged + + if schema.items and _.isArray data + sum += @populateI18N(value, schema.items, path+'/'+index) for value, index in data + + sum module.exports = CocoModel diff --git a/app/templates/editor/level/edit.jade b/app/templates/editor/level/edit.jade index 6b010912d..279b5086d 100644 --- a/app/templates/editor/level/edit.jade +++ b/app/templates/editor/level/edit.jade @@ -68,6 +68,8 @@ block header a(data-i18n="common.fork")#fork-level-start-button Fork li(class=anonymous ? "disabled": "") a(data-toggle="coco-modal", data-target="modal/revert", data-i18n="editor.revert")#revert-button Revert + li(class=anonymous ? "disabled": "") + a(data-i18n="editor.pop_i18n")#pop-level-i18n-button Populate i18n li.divider li.dropdown-header Info li#level-history-button diff --git a/app/treema-ext.coffee b/app/treema-ext.coffee index 62fb12644..e377ad665 100644 --- a/app/treema-ext.coffee +++ b/app/treema-ext.coffee @@ -233,6 +233,7 @@ class InternationalizationNode extends TreemaNode.nodeMap.object type: "object" properties: {} } + return i18nChildSchema unless @parent unless @schema.props? console.warn "i18n props array is empty! Filling with all parent properties by default" @schema.props = (prop for prop,_ of @parent.schema.properties when prop isnt "i18n") diff --git a/app/views/editor/level/edit.coffee b/app/views/editor/level/edit.coffee index 7cd112566..686ebd591 100644 --- a/app/views/editor/level/edit.coffee +++ b/app/views/editor/level/edit.coffee @@ -31,6 +31,7 @@ module.exports = class EditorLevelView extends View 'click #patches-tab': -> @patchesView.load() 'click #level-patch-button': 'startPatchingLevel' 'click #level-watch-button': 'toggleWatchLevel' + 'click #pop-level-i18n-button': -> @level.populateI18N() constructor: (options, @levelID) -> super options