App = Ember.Application.create() App.Router.map -> @route 'standards-list', path: '/' ###### Routes ######### App.StandardsListRoute = Ember.Route.extend model: -> window.ENV.map (item) -> standard = item.grading_standard App.Standard.create title: standard.title grades: standard.data.map (data) -> App.Grade.create label: data[0] value: data[1] ####### Models ###### App.Standard = Ember.Object.extend title: null ranges: null selected: false #TODO: this seems like it belongs in the controller, but the ArrayController doesn't seem to be able to set controller properties. App.Grade = Ember.Object.extend label: null value: 0 #TODO: these seem like they belong in the controller, but the ArrayController doesn't seem to be able to set controller properties. min: -1 max: 1 color: 'transparent' editable: false ####### Controllers ####### App.StandardsListController = Ember.ArrayController.extend actions: remove: (standard) -> # TODO: ic-ajax @removeObject(standard) edit: (standard) -> editable = @findBy 'editable', true if editable? then editable.set 'editable', false standard.set 'editable', true add: -> newStandard = App.Standard.create title: 'New Standard' grades: Ember.A [ App.Grade.create selected: true editable: true value: .5 label: 'Pass' App.Grade.create editable: true value: 0 label: 'Fail' ] @addObject newStandard @send 'edit', newStandard App.StandardController = Ember.ObjectController.extend setEditable: (-> @get('grades').setEach 'editable', @get('editable') ).observes('editable') actions: save: -> # TODO: ic-ajax then: @set('editable', false) App.GradesListController = Ember.ArrayController.extend updateGrades: ( -> grades = @get('content') @forEach (grade, index) -> grade.setProperties max: if grades[index-1]? then grades[index-1].get('value') else 1 min: if grades[index+1]? then grades[index+1].get('value') else -1 ).on('init').observes('content', '@each.value') updateColors: ( -> length = @get('length') @forEach (grade, i) -> grade.set 'color', $.Color( hue: 120 - (i/(length-1) * 120) saturation: .7 lightness: .8 ).toRgbaString() ).on('init').observes('length') actions: remove: (grade) -> @removeObject(grade) add: -> newGrade = App.Grade.create label: 'New' value: .01 editable: true @replaceContent @get('length')-1, 0, [newGrade] @send 'select', newGrade select: (grade) -> selected = @findBy 'selected', true if selected? then selected.set 'selected', false grade.set 'selected', true App.GradeController = Ember.ObjectController.extend setSelected: (-> if not @get('editable') then @set 'selected', false ).observes('editable') colorStyle: (-> "background-color: #{@get('color')}" ).property('color').readOnly() readOnlyValue: (-> @get('min') == -1 or !@get('selected') ).property('min', 'selected').readOnly() deleteable: (-> @get('min') > -1 ).property('min').readOnly() isValid: (-> # TODO: validate label value = @get('value') min = @get('min') max = @get('max') (min < value < max) or @get('min') == -1 ).property('value', 'min', 'max').readOnly() maxPercentage: (-> @get('max') * 100 ).property('max').readOnly() minPercentage: (-> @get('min') * 100 ).property('min').readOnly() percentage: ((key, value) -> if arguments.length > 1 @set 'value', value/100 @get('value') * 100 ).property('value') ######## Views ####### App.GradeView = Ember.View.extend classNames: ['gs-grade'] tagName: 'tr' classNameBindings: ['controller.isValid::error'] attributeBindings: ['tabindex'] tabindex: (-> -1 if @get('controller').get('editable') ).property('controller.editable') focusIn: (evt) -> controller = @get('controller') controller.send 'select', controller ######## Components ####### App.FocusedInputComponent = Ember.TextField.extend initFocus: (-> Ember.run.scheduleOnce 'afterRender', => @$()?.focus() ).on('didInsertElement') Ember.Handlebars.helper 'focused-input', App.FocusedInputComponent App.SpinnerInputComponent = Ember.TextField.extend classNames: ['spinner-input'] initSpinner: (-> Ember.run.scheduleOnce 'afterRender', => @$()?.spinner @get('spinnerOptions') ).on('didInsertElement').observes('spinnerOptions') spinnerOptions: (-> max: @get('max') min: @get('min') mouseWheel: false ).property('max', 'min') Ember.Handlebars.helper 'spinner-input', App.SpinnerInputComponent