Skip to content

Commit

Permalink
Refactored editor viewmodel; Fixes #27.
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay committed Jun 6, 2013
1 parent e6881e7 commit e5d2d4d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 70 deletions.
14 changes: 3 additions & 11 deletions assets/css/editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,9 @@ ul.nav-list {
margin-top: 3px;
}
}
}

ul.level1 {
a {
margin-left: -30px;
margin-right: -30px;
}
ul.level2 {
a {
margin-left: -45px;
margin-right: -45px;
&.objects {
li a {
margin-left: -1000px;
}
}
}
Expand Down
77 changes: 25 additions & 52 deletions assets/js/view_models/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -206,56 +206,27 @@ class EditorView
found

removeFromTree: (id) ->
removeChild = (id, p) =>
[match] = (child for child in p.children() when child.id is id)
if match?
p.children.remove match
match
else
removed = null
for child in p.children()
removed = removeChild(id, child)
break if removed?
removed

removed = null
for o in @objects()
if o.id is id
@objects.remove o
removed = true
break
removed = removeChild id, o
break if removed?

removed

insertIntoTree: (obj) ->
insertChild = (o, p) =>
if p.id == o.parent_id
if o instanceof TreeNode
p.children.push o
else
p.children.push new TreeNode o, @, p.level+1
p.children.sort (l, r) -> l.id - r.id
true
else
added = false
for child in p.children()
added = insertChild(o, child)
break if added
added

added = false
if obj.parent_id is null
@objects.push new TreeNode obj, @
@objects.sort (l, r) -> l.id - r.id
return true
node = @findInTree id
if not node? then return null
if node.parent?
node.parent.children.remove node
node.parent = null
else
for o in @objects()
added = insertChild obj, o
break if added

added
@objects.remove node
node

insertIntoTree: (node, parent_id) ->
if parent_id?
parentNode = @findInTree parent_id
if not parentNode?
#toastr.error "couldn't find parent node"
return null
node.parent = parentNode
parentNode.children.push node
parentNode.children.sort (l, r) -> l.id - r.id
else
@objects.push node
@objects.sort (l, r) -> l.id - r.id

findInTabs: (id) ->
os = @tabs().map (tab) ->
Expand Down Expand Up @@ -353,7 +324,10 @@ class EditorView

object_created: (obj) =>
obj.children = []
@insertIntoTree obj
parent_id = obj.parent_id
delete obj.parent_id
obj = new TreeNode obj, @
@insertIntoTree obj, parent_id

object_deleted: (id) =>
@removeFromTree id
Expand All @@ -363,8 +337,7 @@ class EditorView

object_parent_changed: (spec) =>
o = @removeFromTree spec.id
o.parent_id = spec.parent_id
@insertIntoTree o
@insertIntoTree o, spec.parent_id

object_name_changed: (spec) =>
o = @findInTree spec.id
Expand Down
6 changes: 2 additions & 4 deletions assets/js/view_models/editor/tree_node.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# View model for object browser tree node
class TreeNode

constructor: (o, @view, @level = 1) ->
constructor: (o, @view, @parent) ->

# attributes
@id = o.id
@name = ko.observable o.name
@player = ko.observable o.player
@alias = ko.observable o.alias
@children = ko.observableArray o.children.map (p) => new TreeNode p, @view, @level+1
@children = ko.observableArray o.children.map (p) => new TreeNode p, @view, @

# presenters
@idPresenter = ko.computed =>
Expand Down Expand Up @@ -49,8 +49,6 @@ class TreeNode
else
if @player() then 'icon-user' else 'icon-file'

@levelClass = ko.computed => "level#{@level}"

# subscriptions
@view.filter.subscribe (filter) =>
if filter isnt '' and @visible()
Expand Down
6 changes: 3 additions & 3 deletions views/editor.jade
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ html
span.label.label-inverse(data-bind='html: idPresenter')
span.label(data-bind='html: aliasPresenter')
span(data-bind='html: namePresenter')
ul.nav.nav-list(data-bind='visible: expanded, template: {name: "TreeNodeTemplate", foreach: children}, css: levelClass')
ul.nav.nav-list(data-bind='visible: expanded, template: {name: "TreeNodeTemplate", foreach: children}')

.noconnection(data-bind='visible: !connected()')
.alert.alert-error.alert-error
Expand All @@ -29,7 +29,7 @@ html
.control-group
label.control-label(for='username') Username
.controls
input(type='text', id='username', placeholder='Username', data-bind='value: username')
input(type='text', autofocus=true, id='username', placeholder='Username', data-bind='value: username')
.control-group
label.control-label(for='password') Password
.controls
Expand Down Expand Up @@ -102,7 +102,7 @@ html
hr
.row-fluid
.overflow-container
ul.nav.nav-list(data-bind='template: {name: "TreeNodeTemplate", foreach: objects}')
ul.nav.nav-list.objects(data-bind='template: {name: "TreeNodeTemplate", foreach: objects}')

.ui-layout-west-south(data-bind='context: attributeMenu')
.overflow-container
Expand Down

0 comments on commit e5d2d4d

Please sign in to comment.