WIP : Sequence builder step creation / deletion with API

pull/124/head
Rick Carlino 2015-01-18 08:57:04 -06:00
parent 0ff2448a1d
commit 993e163a78
8 changed files with 49 additions and 68 deletions

View File

@ -1,57 +1,35 @@
controller = ($scope, Data) ->
#TODO: We really really need an error handler / reporter at this point.
#TODO: We need a way of creating a "new" sequence. If you load a sequence,
# there is no way to clear out of it.
Data
.findAll('sequence', {})
.catch (error) -> console.error error
Data.bindAll($scope, 'storedSequences', 'sequence', {})
$scope.add = (message_type) ->
Data.create('step',
message_type: message_type
sequence_id: $scope.sequence._id
).then((step) -> $scope.sequence.steps.push(step))
.catch((e) -> console.error e)
$scope.load = (seq) ->
Data.loadRelations('sequence', seq._id, ['step'])
$scope.sequence = seq
$scope.save = ->
oldSeq = _.find($scope.storedSequences, {name: $scope.sequence.name})
if oldSeq
oldSeq = $scope.sequence
else
$scope.storedSequences.push($scope.sequence)
$scope.copy = (obj, index) -> $scope.sequence.steps.splice((index + 1), 0, angular.copy(obj))
$scope.remove = (index) ->
step = $scope.sequence.steps[index]
Data.destroy('step', step._id).catch((e) -> console.error e)
# The sequence controller supports the WYSIWYG sequence editor.
angular.module('FarmBot').controller "SequenceController", [
'$scope'
'Command'
'Sequences'
($scope, Command, Sequences) ->
# Stub for now. Maybe we can randomly set this in the
# backend on creation or something.
randomColor = ->
colors =
['blue'
'green'
'yellow'
'orange'
'purple'
'pink'
'gray'
'red']
_.sample(colors)
Sequences
.findAll({})
.then((sequences) -> console.log sequences)
.catch((error) ->
alert "There was a problem. See console for details."
console.log error)
.finally(-> null)
window.qqq = Sequences
$scope.command =
name: 'Untitled Sequence'
color: randomColor()
steps: []
$scope.dragControlListeners = {}
$scope.storedSequences = [
{name: 'Scare Away the Birds',
color: randomColor(),
steps:[Command.create("move_rel"), Command.create("move_rel"), Command.create("move_abs")]}
{name: 'Move Away For Maintenance',
color: randomColor(),
steps:[Command.create("move_abs"), Command.create("move_rel"), Command.create("move_rel")]}
]
Sequences.bindAll($scope, 'storedSequences', {})
$scope.copy = (obj, index) -> $scope.command.steps.splice((index + 1), 0, angular.copy(obj))
$scope.remove = (index) -> $scope.command.steps.splice(index, 1)
$scope.add = (name) -> $scope.command.steps.push(Command.create(name))
$scope.load = (seq) ->
Sequences.loadRelations(seq._id, ['step'])
$scope.command = seq
$scope.save = ->
oldSeq = _.find($scope.storedSequences, {name: $scope.command.name})
if oldSeq
oldSeq = $scope.command
else
$scope.storedSequences.push($scope.command)
'Data'
controller
]

View File

@ -1,6 +1,6 @@
# TODO this needs to get separated into separate factories. As you can see,
# there are two resources being defined here.
sequences = (DS) ->
# RESTful data adapter for hooking angular JS into the backend API. SEE:
# http://angular-data.pseudobry.com/
data = (DS) ->
DS.defineResource
name: "step"
endpoint: 'steps',
@ -24,7 +24,9 @@ sequences = (DS) ->
localField: "steps"
foreignKey: "sequence_id"
angular.module("FarmBot").factory 'Sequences', [
return DS
angular.module("FarmBot").service 'Data', [
'DS',
sequences
data
]

View File

@ -9,6 +9,5 @@ class Step
field :message_type
validates :message_type, presence: true
field :command, type: Hash
validates :command, presence: true
field :command, type: Hash, default: {}
end

View File

@ -5,9 +5,7 @@ module Steps
required do
string :message_type, in: Step::MESSAGE_TYPES
model :sequence, class: Sequence
hash :command do
model :*, class: Object
end
model :command, class: Hash, default: {}
end
def execute

View File

@ -95,7 +95,7 @@
<div class="content-wrapper">
<div class="row">
<div class="small-9 columns">
<input id="right-label" type="text" ng-model="command.name">
<input id="right-label" type="text" ng-model="sequence.name">
</div>
@ -138,11 +138,11 @@
</div>
<div class="row" as-sortable="dragControlListeners" ng-model="commandList">
<div ng-repeat='cmd in command.steps track by $id(cmd)' as-sortable-item>
<div class="row" as-sortable="dragControlListeners" ng-model="sequenceList">
<div ng-repeat='seq in sequence.steps track by $id(seq)' as-sortable-item>
<div class="row">
<ng-include src="'sequence-builder/' + cmd.message_type" >
<ng-include src="'sequence-builder/' + seq.message_type" >
</ng-include>
</div><div>
</div>

View File

@ -18,7 +18,7 @@ module MongoidRefinements
if model.valid? && model.save
model
else
model.errors.messages
add_error klass.to_s.downcase.to_sym, :invalid, model.errors.messages
end
end

View File

@ -20,4 +20,8 @@ describe Steps::Create do
cmd = outcome.result.command.deep_symbolize_keys
expect(cmd).to eq(valid_params[:command])
end
it 'Returns 422 for bad records' do
# curl 'http://localhost:3000/api/sequences/54ba75d8736d610b07000000/steps' -H 'Cookie: _ga=GA1.1.930394941.1409052865; _dss_session=Y0FnWFVPeXFpVVFiTG9pM1luejVvVzJnbEsrU01LOWZwNUdyWSs1SXgvRGRUbjNYc2ptNU1ZeUZMK3hiQ0dWWWRvU2VjNUtJaTNRSmpMQUNQbU9ISWlkOUlCSVZxWk1tYkxlbGdTeDJ0UVNCT3NTZ2E2bC94NlJIYnQyZjkvcThBbHpudXZ3RzZrL0NPTUN1Z0daeGVqbldvWHk4clVSNnF4WnR2N0phYS85bC95Rlc1bi9PWkVmZVQrbmtsTE5aVHR4eEMzcUd1SDNva1M3OVAzMHd5TFZFQ1AzdkM3ZGpUMVlUUTZoeDhSOERsNWcwc2JvdENJblhNTkh0VHJxNFdtQkxSdlR0WlJYL0FlUFQvY1h1ZHp0UFI4aGl6dTNxODRQUTRVU1grTzM2R010QWorQTFkb2o5R05WempuTEQtLVdSSW1mR1dGK2Z3VGxZRGZBVnI4dmc9PQ%3D%3D--5f8e7b1d49c3a21ccdf41d9e38436bdc71fd8cb9' -H 'Origin: http://localhost:3000' -H 'Accept-Encoding: gzip, deflate' -H 'X-CSRF-TOKEN: GVfkmmcVJIe8HTo5oeeRYkYoyF+WGMlMWfHSgI13+3I=' -H 'Accept-Language: en-US,en;q=0.8,ko;q=0.6' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:3000/dashboard' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data-binary '{"message_type":"move_rel","sequence_id":"54ba75d8736d610b07000000"}' --compressed
end
end