Rudimentary sequence builder without step updates

pull/130/head
Rick Carlino 2015-01-27 11:42:43 -06:00
parent 7b59e64f36
commit 1e5dfa814f
9 changed files with 40 additions and 13 deletions

View File

@ -8,12 +8,22 @@ controller = ($scope, Data) ->
.findAll('sequence', {})
.catch(nope)
# TODO figure out why ng-sortables break if passed a null value.
# TODO figure out why ng-sortables breaks if passed a null value.
$scope.sequenceSteps ?= []
$scope.dragControlListeners = orderChanged: (event) ->
rank = event.dest.index
position = event.dest.index
step = event.source.itemScope.modelValue
debugger
# TODO I want to do $scope.reload(step.sequence) but cant until I resolve
# the stack overflow issue.
yep = (step) -> $scope.reload($scope.sequence)
# Failure to delete step.sequence results in a stack overflow :(
# TODO Figure out why angular-data isn't doing this by default.
# https://github.com/jmdobry/angular-data/issues/299
delete step.sequence
Data
.update('step', step._id, {position: position})
.then(yep)
.catch(nope)
Data.bindAll($scope, 'storedSequences', 'sequence', {})
hasSequence = ->
if $scope.sequence
@ -29,12 +39,19 @@ controller = ($scope, Data) ->
).then((step) -> $scope.sequence.steps.push(step))
.catch(nope)
$scope.load = (seq) ->
# Invalidate the cache to prevent the `position` field from going stale.
Data.ejectAll('step', {sequence_id: seq._id})
Data
.loadRelations('sequence', seq._id, ['step'])
.catch(nope)
.then ->
$scope.sequence = seq
$scope.sequenceSteps = $scope.sequence.steps
$scope.reload = (seq) ->
Data
.refresh('sequence', seq._id)
.then($scope.load)
.catch(nope)
$scope.addSequence = (params = {}, makeItDefaultNow = yes) ->
params.name ?= 'Untitled Sequence'
Data
@ -45,7 +62,9 @@ controller = ($scope, Data) ->
return unless hasSequence()
Data
.destroy('sequence', seq._id)
.then(() -> $scope.sequence = null)
.then(() ->
$scope.sequence = null
$scope.sequenceSteps = [])
.catch(nope)
$scope.saveSequence = (seq) ->
Data
@ -64,6 +83,7 @@ controller = ($scope, Data) ->
).then(yep)
.catch(nope)
$scope.remove = (index) ->
# TODO Rename to deleteStep
step = $scope.sequence.steps[index]
Data.destroy('step', step._id).catch((e) -> console.error e)

View File

@ -6,6 +6,7 @@ data = (DS) ->
endpoint: 'steps',
baseUrl: '/api',
idAttribute: "_id"
serialize: (resourceName, data) -> _.omit(data, 'sequence')
relations:
belongsTo:
sequence:
@ -18,10 +19,7 @@ data = (DS) ->
endpoint: 'sequences',
baseUrl: '/api',
idAttribute: "_id"
serialize: (resourceName, data) ->
pojo = _.clone(data)
delete pojo.steps
pojo
serialize: (resourceName, data) -> _.omit(data, 'steps')
relations:
hasMany:
step:

View File

@ -4,6 +4,10 @@ module Api
render json: Sequence.where(user: current_user)
end
def show
render json: sequence
end
def create
mutate Sequences::Create.run(params, user: current_user)
end

View File

@ -17,7 +17,7 @@ module Api
end
def update
mutate Steps::Update.run(params: params, step: step)
mutate Steps::Update.run(step_params: params[:step], step: step)
end
private

View File

@ -5,7 +5,7 @@ class Step
MESSAGE_TYPES = %w(single_command read_status pin_write move_abs move_rel)
embedded_in :sequence
orderable
orderable base: 0
field :message_type
validates :message_type, presence: true

View File

@ -4,7 +4,7 @@ module Steps
required do
model :step, class: Step
hash :params do
hash :step_params do
optional do
integer :position
string :message_type, in: Step::MESSAGE_TYPES
@ -22,7 +22,10 @@ module Steps
# 1. Create a StepValidatorFactory
# 2. Create a SingleCommandValidator, ReadStatusValidator, etc.
# Or: Use inheritance and embed different classes of Command
update_attributes(step, params)
# TODO move_to is possibly broke. Going to reorder on every request to stay in sync?
step.move_to step_params[:position] || step.position
update_attributes(step, step_params.except(:position))
end
end
end

View File

@ -147,6 +147,7 @@
<div class="row" as-sortable='dragControlListeners' ng-model="sequenceSteps">
<div ng-repeat='step in sequenceSteps | orderBy: "position" ' >
<div class="row" as-sortable-item ng-model="step">
{{ step.position }}
<ng-include src="'sequence-builder/' + step.message_type">
</ng-include>
</div>

View File

@ -3,7 +3,7 @@ Dss::Application.routes.draw do
mount JasmineRails::Engine => '/specs' if defined?(JasmineRails)
namespace :api, defaults: {format: :json} do
resources :devices, only: [:index, :destroy, :create, :update]
resources :sequences, only: [:create, :update, :destroy, :index] do
resources :sequences, only: [:create, :update, :destroy, :index, :show] do
resources :steps, only: [:create, :index, :update, :destroy]
end
end

View File

@ -30,6 +30,7 @@ module MongoidRefinements
else
# TODO test this
add_error klass.to_s.downcase.to_sym, :invalid, model.errors.messages
false
end
end
end