########## handlebars helpers ############## isNumber = (n)-> !isNaN(parseFloat(n)) && isFinite(n) String.prototype.capitalize = -> this.charAt(0).toUpperCase() + this.substring(1).toLowerCase() String.prototype.titleize = -> res = [] parts = this.split(" ") $.each(parts, (index, part)-> res.push(part.capitalize()) ) res.join(" ") Handlebars.registerHelper("rounded", (number)-> if isNumber(number) parseFloat(number).toFixed(2) ) Handlebars.registerHelper("titleize", (string)-> if (string != undefined) string.titleize() ) Handlebars.registerHelper("address_without_comma", (lab)-> return unless this.data and this.data.address this.data.address.replace(/(\s*,\s*$)/g, "").titleize() ) ########## Lab model ############## class window.Lab map_tooltip_template: Handlebars.compile("{{titleize name}}\n{{address_without_comma address}}\n{{titleize city}},{{state}} ") index = null marker = null point = null constructor: (@data) -> clearMarker: -> if @marker @marker.setMap(null) @marker = null setMarker: (map, markerBounds, index) -> @point = new google.maps.LatLng(@data.lat, @data.lng) @marker = this.makeMarker(map, index) if markerBounds markerBounds.extend(@point) makeMarker: (map, index) -> new google.maps.Marker({ position: @point map: map title: this.map_tooltip_template(@data) icon: this.makeIcon(index) }) makeIcon: -> "/assets/medivo/lab.png" icon: -> return @marker.icon if @marker this.makeIcon() ########## OrderedLab model ############## class window.OrderedLab extends Lab constructor: (@data, index) -> @index = index 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 @tracker_elem: $("") constructor: (lab_info, show_number)-> @labs = [] @show_number = show_number if show_number this.setLabs(lab_info) getLabs: -> @labs.slice(0, @show_number) # use this method to set new lab info setLabs: (lab_info)-> this.clearMarkers() @labs = [] this.loadLabs(lab_info) LabList.tracker_elem.change() clearMarkers: -> $.each( @labs, (index, lab)-> lab.clearMarker() ) loadLabs: (lab_info)-> return unless lab_info labs = @labs $.each( lab_info, (index, lab_data)-> labs.push(new Lab(lab_data)) ) ########## OrderedLabList model ############## class window.OrderedLabList extends LabList loadLabs: (lab_info)-> return unless lab_info labs = @labs $.each( lab_info, (index, lab_data)-> labs.push(new OrderedLab(lab_data, index)) )