app/assets/javascripts/medivo/models.coffee in medivo-0.0.4 vs app/assets/javascripts/medivo/models.coffee in medivo-0.0.7

- old
+ new

@@ -1,6 +1,9 @@ ########## handlebars helpers ############## +isNumber = (n)-> + !isNaN(parseFloat(n)) && isFinite(n) + String.prototype.capitalize = -> this.charAt(0).toUpperCase() + this.substring(1).toLowerCase() String.prototype.titleize = -> res = [] @@ -9,69 +12,106 @@ res.push(part.capitalize()) ) res.join(" ") Handlebars.registerHelper("rounded", (number)-> - if (number != undefined) + if isNumber(number) parseFloat(number).toFixed(2) ) Handlebars.registerHelper("titleize", (string)-> if (string != undefined) string.titleize() ) Handlebars.registerHelper("address_without_comma", (lab)-> - if (this.address != undefined) - this.address.replace(/(\s*,\s*$)/g, "").titleize() + return unless this.data and this.data.address + this.data.address.replace(/(\s*,\s*$)/g, "").titleize() ) + ########## Lab model ############## class window.Lab - marker: null map_tooltip_template: Handlebars.compile("{{titleize name}}\n{{address_without_comma address}}\n{{titleize city}},{{state}} ") - constructor: (@data)-> + constructor: (@data) -> + @index = null + @marker = null + @point = null clearMarker: -> - if this.marker - this.marker.setMap(null) - this.marker = null + if @marker + @marker.setMap(null) + @marker = null - setMarker: (map, markerBounds)-> - this.point = new google.maps.LatLng(@data.lat, @data.lng) - this.marker = this.makeMarker(map) + setMarker: (map, markerBounds, index) -> + @point = new google.maps.LatLng(@data.lat, @data.lng) + @marker = this.makeMarker(map, index) if markerBounds - markerBounds.extend(this.point) + markerBounds.extend(@point) - makeMarker: (map)-> + makeMarker: (map, index) -> new google.maps.Marker({ - position: this.point + position: @point map: map title: this.map_tooltip_template(@data) - icon: this.makeIcon() + icon: this.makeIcon(index) }) makeIcon: -> - new google.maps.MarkerImage("/assets/medivo/lab.png") + "/assets/medivo/lab.png" + icon: -> + return @marker.icon if @marker + this.makeIcon() + + +########## OrderedLab model ############## +class window.OrderedLab extends Lab + constructor: (@index, @data) -> + + makeIcon: -> + super() unless isNumber(@index) + "/assets/medivo/marker" + this.makeLetter(@index) + ".png" + + makeLetter: (number) -> + if (number >= 0 && number <= 26) + return String.fromCharCode(number + 65) + return '' + + icon: -> + return @marker.icon if @marker + this.makeIcon(@index) + + ########## LabList model ############## class window.LabList show_number: 3 constructor: (lab_info, show_number)-> @labs = [] @show_number = show_number if show_number - console.log(@show_number) this.setLabs(lab_info) getLabs: -> @labs.slice(0, @show_number) setLabs: (lab_info)-> this.clearMarkers() labs = @labs = [] - $.each( lab_info, (index, lab_data)-> - labs.push(new Lab(lab_data)) - ) + if lab_info != undefined + $.each( lab_info, (index, lab_data)-> + labs.push(new Lab(lab_data)) + ) clearMarkers: -> $.each( @labs, (index, lab)-> lab.clearMarker() ) + + +########## OrderedLabList model ############## +class window.OrderedLabList extends LabList + setLabs: (lab_info)-> + this.clearMarkers() + labs = @labs = [] + if lab_info != undefined + $.each( lab_info, (index, lab_data)-> + labs.push(new OrderedLab(index, lab_data)) + )