Created
April 3, 2014 09:17
-
-
Save olkeene/9951178 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
(function() { | |
var GuideUnit, GuidesList, OfferedGuides, | |
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
GuideUnit = React.createClass({ | |
render: function() { | |
return React.DOM.div( {className:"row"}, | |
React.DOM.div( {className:"col-xs-12 guide-unit"}, | |
React.DOM.a( {href:this.props.data.path, className:"guide-title"}, | |
React.DOM.img( {src:this.props.data.cover, width:"700", height:"260"} ) | |
), | |
React.DOM.div( {className:"guide-info-container"}, | |
React.DOM.div( {className:"guide-info"}, | |
React.DOM.div( {className:"make-offer"}, | |
React.DOM.a( {href:this.props.data.offer_path, className:"btn btn-primary"}, "Make the Offer") | |
), | |
React.DOM.a( {href:this.props.data.path, className:"guide-title"}, | |
this.props.data.title | |
), | |
this.props.data.active ? '' : React.DOM.div( {className:"guide-status not-published"}, "Not published"), | |
React.DOM.div( {className:"message-from-curator"}, React.DOM.i(null),this.props.data.places_msg) | |
) | |
) | |
) | |
); | |
} | |
}); | |
GuidesList = React.createClass({ | |
render: function() { | |
var items; | |
if (this.props.guides.length === 0) { | |
return this.transferPropsTo(React.DOM.div(null, React.DOM.div( {className:"empty-unit"}))); | |
} else { | |
items = this.props.guides.map(function(guide) { | |
return GuideUnit( {data:guide} ); | |
}); | |
return React.DOM.div(null, items); | |
} | |
} | |
}); | |
OfferedGuides = React.createClass({ | |
getInitialState: function() { | |
return { | |
place: this.props.places[0], | |
bpv: null | |
}; | |
}, | |
componentDidMount: function() { | |
var adapter, engine, options, places; | |
options = { | |
minLength: 0, | |
highlight: true, | |
hint: false | |
}; | |
engine = new Bloodhound({ | |
local: this.props.places, | |
datumTokenizer: function(d) { | |
return Bloodhound.tokenizers.whitespace(d.name); | |
}, | |
queryTokenizer: Bloodhound.tokenizers.whitespace, | |
ttl: 1 | |
}); | |
engine.initialize(); | |
adapter = engine.ttAdapter(); | |
places = { | |
name: 'Places', | |
source: (function(_this) { | |
return function(q, cb) { | |
if (q === "") { | |
return cb(_this.props.places); | |
} else { | |
return adapter(q, cb); | |
} | |
}; | |
})(this), | |
displayKey: function(obj) { | |
return obj.name; | |
}, | |
templates: { | |
empty: "", | |
footer: "", | |
header: "", | |
suggestion: HandlebarsTemplates['guides/suggester/item'] | |
} | |
}; | |
return $(this.refs.placeInput.getDOMNode()).resizableInput().typeahead(options, places).on('typeahead:selected', this.updateSelected).on('focus click', (function(_this) { | |
return function(e) { | |
var input, typeahead; | |
input = $(_this.refs.placeInput.getDOMNode()); | |
input.select(); | |
typeahead = input.data('ttTypeahead'); | |
return typeahead.input.trigger('queryChanged', ""); | |
}; | |
})(this)); | |
}, | |
updateSelected: function(e, suggestion, datasetName) { | |
$(this.refs.placeInput.getDOMNode()).trigger('change'); | |
return this.setState({ | |
place: suggestion | |
}); | |
}, | |
handleBpvInput: function(e) { | |
var bpv; | |
bpv = e.target.value !== "" ? parseInt(e.target.value) : null; | |
return this.setState({ | |
bpv: bpv | |
}); | |
}, | |
checkBpv: function(guide) { | |
if (this.state.bpv) { | |
return guide.bpv_from <= this.state.bpv; | |
} else { | |
return true; | |
} | |
}, | |
checkPlace: function(guide) { | |
var _ref, _ref1; | |
if (this.state.place.name === "All Places") { | |
return true; | |
} else { | |
return (_ref = this.state.place.city_id, __indexOf.call(guide.city_ids, _ref) >= 0) && (_ref1 = this.state.place.category_id, __indexOf.call(guide.category_ids, _ref1) >= 0); | |
} | |
}, | |
formatBudget: function() { | |
if (this.state.place.value) { | |
return '$' + this.state.place.value; | |
} else { | |
return ''; | |
} | |
}, | |
formatGuides: function() { | |
if (this.state.place.matching_guides_count === 1) { | |
return this.state.place.matching_guides_count + ' guide'; | |
} else { | |
return this.state.place.matching_guides_count + ' guides'; | |
} | |
}, | |
render: function() { | |
var guides; | |
guides = _.select(this.props.initialGuides, (function(_this) { | |
return function(guide) { | |
return _this.checkBpv(guide) && _this.checkPlace(guide); | |
}; | |
})(this)); | |
return React.DOM.div(null, | |
React.DOM.div( {id:"offered-guides-filter"}, | |
React.DOM.div( {id:"places-filter", className:"pull-left"}, | |
React.DOM.div( {className:"selected-place"}, | |
React.DOM.input( {ref:"placeInput", type:"text", className:"place-title", defaultValue:this.state.place.name} ), React.DOM.span( {className:"place-budget"}, this.formatBudget()),React.DOM.br(null ), | |
React.DOM.span( {className:"place-guides-number"}, this.formatGuides()) | |
) | |
), | |
React.DOM.div( {className:"pull-right"}, | |
" BPV $ ", React.DOM.input( {ref:"bpvField", className:"bpv-field", type:"text", name:"bpv", onInput:this.handleBpvInput} ), | |
Hint( {title:this.props.hint.title, content:this.props.hint.content, placement:"left"} ) | |
) | |
), | |
GuidesList( {className:"guides-list", guides:guides} ) | |
); | |
} | |
}); | |
window.OfferedGuides = OfferedGuides; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment