Permalink
Browse files

Focal areas

  • Loading branch information...
1 parent 2c39710 commit 659827745f9c21a8b48b050bd2db37ec1f5ed88d @decioferreira decioferreira committed Apr 11, 2012
Showing with 351 additions and 48 deletions.
  1. +26 −0 app/assets/javascripts/backbone/models/focal_area.js.coffee
  2. +9 −0 app/assets/javascripts/backbone/models/indicator.js.coffee
  3. +31 −0 app/assets/javascripts/backbone/routers/focal_areas_router.js.coffee
  4. +23 −6 app/assets/javascripts/backbone/routers/indicators_router.js.coffee
  5. +15 −0 app/assets/javascripts/backbone/templates/focal_areas/edit.jst.ejs
  6. +5 −0 app/assets/javascripts/backbone/templates/focal_areas/focal_area.jst.ejs
  7. +14 −0 app/assets/javascripts/backbone/templates/focal_areas/index.jst.ejs
  8. +15 −0 app/assets/javascripts/backbone/templates/focal_areas/new.jst.ejs
  9. +7 −0 app/assets/javascripts/backbone/templates/focal_areas/show.jst.ejs
  10. +24 −0 app/assets/javascripts/backbone/views/focal_areas/edit_view.js.coffee
  11. +19 −0 app/assets/javascripts/backbone/views/focal_areas/focal_area_view.js.coffee
  12. +20 −0 app/assets/javascripts/backbone/views/focal_areas/index_view.js.coffee
  13. +37 −0 app/assets/javascripts/backbone/views/focal_areas/new_view.js.coffee
  14. +8 −0 app/assets/javascripts/backbone/views/focal_areas/show_view.js.coffee
  15. +1 −0 app/controllers/indicators_controller.rb
  16. +3 −0 app/models/focal_area.rb
  17. +1 −0 app/models/indicator.rb
  18. +9 −10 app/views/indicators/index.html.erb
  19. +1 −1 app/views/layouts/application.html.erb
  20. +9 −0 db/migrate/20120411134121_create_focal_areas.rb
  21. +8 −0 db/migrate/20120411135810_focal_area_indicator_join.rb
  22. +12 −1 db/schema.rb
  23. +49 −30 db/seeds.rb
  24. +5 −0 spec/models/focal_area_spec.rb
@@ -0,0 +1,26 @@
+class BIPIndicatorsPage.Models.FocalArea extends Backbone.Model
+ paramRoot: 'focal_area'
+
+ defaults:
+ name: null
+ # extra fields for interface
+ selected: false
+
+ select: ->
+ @collection.deselectAll()
+ @set({'selected': true})
+
+ deselect: ->
+ @set({'selected': false})
+
+class BIPIndicatorsPage.Collections.FocalAreasCollection extends Backbone.Collection
+ model: BIPIndicatorsPage.Models.FocalArea
+ url: '/focal_areas'
+
+ deselectAll: ->
+ _.each @models, (target) ->
+ target.deselect()
+
+ selected: ->
+ @filter (headline) ->
+ headline.get('selected')
@@ -7,6 +7,7 @@ class BIPIndicatorsPage.Models.Indicator extends Backbone.Model
position: null
link: null
targets: []
+ focal_areas: []
# extra fields for interface
show: false
@@ -22,6 +23,14 @@ class BIPIndicatorsPage.Collections.IndicatorsCollection extends Backbone.Collec
_.each @models, (indicator) ->
indicator.set({'show': false })
+ filterByFocalArea: (focalArea) ->
+ if focalArea?
+ _.each @models, (indicator) ->
+ indicator.set({'show': (_.pluck(indicator.get('focal_areas'), 'id').indexOf(focalArea.get('id')) != -1 ) })
+ else
+ _.each @models, (indicator) ->
+ indicator.set({'show': false })
+
filterByGoal: (goal) ->
_.each @models, (indicator) ->
indicator.set({'show': (_.intersection(_.pluck(indicator.get('targets'), 'id'), _.pluck(goal.get('targets'), 'id')).length > 0 ) })
@@ -0,0 +1,31 @@
+class BIPIndicatorsPage.Routers.FocalAreasRouter extends Backbone.Router
+ initialize: (options) ->
+ @focalAreas = new BIPIndicatorsPage.Collections.FocalAreasCollection()
+ @focalAreas.reset options.focalAreas
+
+ routes:
+ "/new" : "newFocalArea"
+ "/index" : "index"
+ "/:id/edit" : "edit"
+ "/:id" : "show"
+ ".*" : "index"
+
+ newFocalArea: ->
+ @view = new BIPIndicatorsPage.Views.FocalAreas.NewView(collection: @focal_areas)
+ $("#focal_areas").html(@view.render().el)
+
+ index: ->
+ @view = new BIPIndicatorsPage.Views.FocalAreas.IndexView(focal_areas: @focal_areas)
+ $("#focal_areas").html(@view.render().el)
+
+ show: (id) ->
+ focal_area = @focal_areas.get(id)
+
+ @view = new BIPIndicatorsPage.Views.FocalAreas.ShowView(model: focal_area)
+ $("#focal_areas").html(@view.render().el)
+
+ edit: (id) ->
+ focal_area = @focal_areas.get(id)
+
+ @view = new BIPIndicatorsPage.Views.FocalAreas.EditView(model: focal_area)
+ $("#focal_areas").html(@view.render().el)
@@ -11,6 +11,10 @@ class BIPIndicatorsPage.Routers.IndicatorsRouter extends Backbone.Router
@headlines = new BIPIndicatorsPage.Collections.HeadlinesCollection()
@headlines.reset options.headlines
+ # Focal areas
+ @focal_areas = new BIPIndicatorsPage.Collections.FocalAreasCollection()
+ @focal_areas.reset options.focal_areas
+
routes:
".*" : "index"
@@ -30,21 +34,37 @@ class BIPIndicatorsPage.Routers.IndicatorsRouter extends Backbone.Router
$('a[data-toggle="tab"]').on('shown', @switchContext)
switchContext: (e) =>
- if($(e.target).attr('href') == '#matrix' || $(e.target).attr('href') == '#graphic')
+ if($(e.target).attr('href') == '#matrix')
# by default if deselect all the indicators
@indicators.filterByTarget()
_.each @goals.models, (goal) =>
if goal.targets.selected().length == 1
@indicators.filterByTarget(goal.targets.selected()[0])
- else if goal.targets.selected().length > 1
- @indicators.filterByGoal(goal)
+ else if($(e.target).attr('href') == '#graphic')
+ @filterByHeadline(@focal_areas.selected()[0])
else
@filterByHeadline(@headlines.selected()[0])
filterByHeadline: (headline) ->
@indicators.filterByHeadline(headline)
+ filterByFocalArea: (focalArea) ->
+ if typeof(focalArea) == 'string'
+ found_focal_areas = _.filter(@focal_areas.models, (iter_focal_area) ->
+ iter_focal_area.get('name') == focalArea
+ )
+ if found_focal_areas.length == 1
+ focalArea = found_focal_areas[0]
+ else
+ console.log("Error: #{found_focal_areas.length} focal areas found. Should have found an unique focal area for the given name.")
+ return false
+
+ # Force selection on model, as there is no view for the focal area
+ focalArea.select()
+
+ @indicators.filterByFocalArea(focalArea)
+
filterByGoal: (goal) ->
if typeof(goal) == 'string'
found_goals = _.filter(@goals.models, (iter_goal) ->
@@ -62,8 +82,5 @@ class BIPIndicatorsPage.Routers.IndicatorsRouter extends Backbone.Router
@indicators.filterByGoal(goal)
- # Switch to first tab
- $("ul.nav a:first").click()
-
filterByTarget: (target) ->
@indicators.filterByTarget(target)
@@ -0,0 +1,15 @@
+<h1>Edit focal_area</h1>
+
+<form id="edit-focal_area" name="focal_area">
+ <div class="field">
+ <label for="name"> name:</label>
+ <input type="text" name="name" id="name" value="<%= name %>" >
+ </div>
+
+ <div class="actions">
+ <input type="submit" value="Update Focal area" />
+ </div>
+
+</form>
+
+<a href="#/index">Back</a>
@@ -0,0 +1,5 @@
+<td><%= name %></td>
+
+<td><a href="#/<%= id %>">Show</td>
+<td><a href="#/<%= id %>/edit">Edit</td>
+<td><a href="#/<%= id %>/destroy" class="destroy">Destroy</a></td>
@@ -0,0 +1,14 @@
+<h1>Listing focal_areas</h1>
+
+<table id="focal_areas-table">
+ <tr>
+ <th>Name</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+</table>
+
+<br/>
+
+<a href="#/new">New Focal area</a>
@@ -0,0 +1,15 @@
+<h1>New focal_area</h1>
+
+<form id="new-focal_area" name="focal_area">
+ <div class="field">
+ <label for="name"> name:</label>
+ <input type="text" name="name" id="name" value="<%= name %>" >
+ </div>
+
+ <div class="actions">
+ <input type="submit" value="Create Focal area" />
+ </div>
+
+</form>
+
+<a href="#/index">Back</a>
@@ -0,0 +1,7 @@
+<p>
+ <b>Name:</b>
+ <%= name %>
+</p>
+
+
+<a href="#/index">Back</a>
@@ -0,0 +1,24 @@
+BIPIndicatorsPage.Views.FocalAreas ||= {}
+
+class BIPIndicatorsPage.Views.FocalAreas.EditView extends Backbone.View
+ template : JST["backbone/templates/focal_areas/edit"]
+
+ events :
+ "submit #edit-focal_area" : "update"
+
+ update : (e) ->
+ e.preventDefault()
+ e.stopPropagation()
+
+ @model.save(null,
+ success : (focal_area) =>
+ @model = focal_area
+ window.location.hash = "/#{@model.id}"
+ )
+
+ render : ->
+ $(@el).html(@template(@model.toJSON() ))
+
+ this.$("form").backboneLink(@model)
+
+ return this
@@ -0,0 +1,19 @@
+BIPIndicatorsPage.Views.FocalAreas ||= {}
+
+class BIPIndicatorsPage.Views.FocalAreas.FocalAreaView extends Backbone.View
+ template: JST["backbone/templates/focal_areas/focal_area"]
+
+ events:
+ "click .destroy" : "destroy"
+
+ tagName: "tr"
+
+ destroy: () ->
+ @model.destroy()
+ this.remove()
+
+ return false
+
+ render: ->
+ $(@el).html(@template(@model.toJSON() ))
+ return this
@@ -0,0 +1,20 @@
+BIPIndicatorsPage.Views.FocalAreas ||= {}
+
+class BIPIndicatorsPage.Views.FocalAreas.IndexView extends Backbone.View
+ template: JST["backbone/templates/focal_areas/index"]
+
+ initialize: () ->
+ @options.focalAreas.bind('reset', @addAll)
+
+ addAll: () =>
+ @options.focalAreas.each(@addOne)
+
+ addOne: (focalArea) =>
+ view = new BIPIndicatorsPage.Views.FocalAreas.FocalAreaView({model : focalArea})
+ @$("tbody").append(view.render().el)
+
+ render: =>
+ $(@el).html(@template(focalAreas: @options.focalAreas.toJSON() ))
+ @addAll()
+
+ return this
@@ -0,0 +1,37 @@
+BIPIndicatorsPage.Views.FocalAreas ||= {}
+
+class BIPIndicatorsPage.Views.FocalAreas.NewView extends Backbone.View
+ template: JST["backbone/templates/focal_areas/new"]
+
+ events:
+ "submit #new-focal_area": "save"
+
+ constructor: (options) ->
+ super(options)
+ @model = new @collection.model()
+
+ @model.bind("change:errors", () =>
+ this.render()
+ )
+
+ save: (e) ->
+ e.preventDefault()
+ e.stopPropagation()
+
+ @model.unset("errors")
+
+ @collection.create(@model.toJSON(),
+ success: (focal_area) =>
+ @model = focal_area
+ window.location.hash = "/#{@model.id}"
+
+ error: (focal_area, jqXHR) =>
+ @model.set({errors: $.parseJSON(jqXHR.responseText)})
+ )
+
+ render: ->
+ $(@el).html(@template(@model.toJSON() ))
+
+ this.$("form").backboneLink(@model)
+
+ return this
@@ -0,0 +1,8 @@
+BIPIndicatorsPage.Views.FocalAreas ||= {}
+
+class BIPIndicatorsPage.Views.FocalAreas.ShowView extends Backbone.View
+ template: JST["backbone/templates/focal_areas/show"]
+
+ render: ->
+ $(@el).html(@template(@model.toJSON() ))
+ return this
@@ -6,6 +6,7 @@ def index
@goals = Goal.all
@targets = Target.all
@headlines = Headline.all
+ @focal_areas = FocalArea.all
@partners = Partner.all
end
end
View
@@ -0,0 +1,3 @@
+class FocalArea < ActiveRecord::Base
+ has_and_belongs_to_many :indicators
+end
View
@@ -1,6 +1,7 @@
class Indicator < ActiveRecord::Base
has_and_belongs_to_many :targets
belongs_to :headline
+ has_and_belongs_to_many :focal_areas
has_and_belongs_to_many :partners
def link
@@ -10,18 +10,17 @@
</div>
<div class="tab-pane" id="graphic">
<map id="graph-map" name="graph-map">
- <area shape="rect" coords="118,161,377,283" href="javascript:window.router.filterByGoal('A')" alt="" title="" />
- <area shape="rect" coords="596,161,832,285" href="javascript:window.router.filterByGoal('B')" alt="" title="" />
- <area shape="rect" coords="620,452,833,576" href="javascript:window.router.filterByGoal('C')" alt="" title="" />
- <area shape="rect" coords="118,453,357,577" href="javascript:window.router.filterByGoal('D')" alt="" title="" />
+ <area shape="rect" coords="118,161,377,283" href="javascript:window.router.filterByFocalArea('A')" alt="" title="" />
+ <area shape="rect" coords="596,161,832,285" href="javascript:window.router.filterByFocalArea('B')" alt="" title="" />
+ <area shape="rect" coords="620,452,833,576" href="javascript:window.router.filterByFocalArea('C')" alt="" title="" />
+ <area shape="rect" coords="118,453,357,577" href="javascript:window.router.filterByFocalArea('D')" alt="" title="" />
</map>
<%= image_tag 'graph.png', :usemap => "graph-map" %>
- <button class="btn first" onclick="window.router.filterByGoal('A');return false">A</button>
- <button class="btn" onclick="window.router.filterByGoal('B');return false">B</button>
- <button class="btn" onclick="window.router.filterByGoal('C');return false">C</button>
- <button class="btn" onclick="window.router.filterByGoal('D');return false">D</button>
- <button class="btn" onclick="window.router.filterByGoal('E');return false">E</button>
+ <button class="btn first" onclick="window.router.filterByFocalArea('A');return false">A</button>
+ <button class="btn" onclick="window.router.filterByFocalArea('B');return false">B</button>
+ <button class="btn" onclick="window.router.filterByFocalArea('C');return false">C</button>
+ <button class="btn" onclick="window.router.filterByFocalArea('D');return false">D</button>
</div>
<div class="tab-pane" id="headlines">
<div id="headlines"></div>
@@ -34,7 +33,7 @@
<script type="text/javascript">
$(function() {
- window.router = new BIPIndicatorsPage.Routers.IndicatorsRouter({indicators: <%= @indicators.to_json(:include => :targets, :methods => :link).html_safe -%>, goals: <%= @goals.to_json(:include => :targets).html_safe -%>, headlines: <%= @headlines.to_json.html_safe -%>, partners: <%= @partners.to_json.html_safe -%>});
+ window.router = new BIPIndicatorsPage.Routers.IndicatorsRouter({indicators: <%= @indicators.to_json(:include => [:targets, :focal_areas], :methods => :link).html_safe -%>, goals: <%= @goals.to_json(:include => :targets).html_safe -%>, headlines: <%= @headlines.to_json.html_safe -%>, focal_areas: <%= @focal_areas.to_json.html_safe -%>, partners: <%= @partners.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
@@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
- <title>Bootstrap, from Twitter</title>
+ <title>BIP</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
@@ -0,0 +1,9 @@
+class CreateFocalAreas < ActiveRecord::Migration
+ def change
+ create_table :focal_areas do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 6598277

Please sign in to comment.