var React = require('react'); var Mui = require('material-ui'); var Paper = Mui.Paper; var NutellaMixin = require('./NutellaMixin'); var AnimationMixin = require('./AnimationMixin'); var ColorPicker = require('./ColorPicker'); var TextField = Mui.TextField; var RadioButtonGroup = Mui.RadioButtonGroup; var RadioButton = Mui.RadioButton; var FlatButton = Mui.FlatButton; //var queue = require("queue-async"); /** * @prop channelId * @prop channel * @prop selected * @prop onselectedChannel */ var Channel = React.createClass({ mixins: [NutellaMixin, AnimationMixin], componentDidMount: function() { this.imageFile = null; this.imageData_ = null; }, /** * State * @returns selected: boolean * side: 'front' or 'back' */ getInitialState: function () { return { selected: false, flipped: false, modifyField: null } }, setModifyField: function(field) { this.setState({ modifyField: field }); }, componentWillReceiveProps: function(nextProps) { if(nextProps.selected) { this.setState({ selected: true }); } else { this.setState({ selected: false }); this.setModifyField(null); } }, handleSelectChannel: function() { this.props.onSelectChannel(this.props.channelId); }, handleModifyIcon: function() { this.setModifyField('icon'); }, flipCard: function() { var self = this; this.setState({ flipped: true }); function callbackFlip() { self.addCSSClass(self.refs.cardFront.getDOMNode(), 'hidden-card'); self.removeCSSClass(self.refs.cardBack.getDOMNode(), 'hidden-card'); self.flipY(self.refs.card.getDOMNode(), 90, 180, false); } this.flipY(this.refs.card.getDOMNode(), 0, 90, false, callbackFlip); }, flipCardBack: function() { var self = this; this.setState({ flipped: false }); function callbackFlip() { self.addCSSClass(self.refs.cardBack.getDOMNode(), 'hidden-card'); self.removeCSSClass(self.refs.cardFront.getDOMNode(), 'hidden-card'); self.flipY(self.refs.card.getDOMNode(), 90, 0, true); } this.flipY(this.refs.card.getDOMNode(), 180, 90, true, callbackFlip); }, handleDeleteCard: function() { this.props.onDeleteCard(this.props.channelId); }, addCSSClass: function(node, class_) { node.className += ' ' + class_; }, removeCSSClass: function(node, class_) { var regex = new RegExp("(?:^|\\s)" + class_ + "(?!\\S)", "g"); node.className = node.className.replace(regex, ''); }, handleSetName: function() { var value = this.refs['textFieldName' + this.props.channelId].getValue(); this.props.onSetName(value); }, handleSetDescription: function() { var value = this.refs['textFieldDescription' + this.props.channelId].getValue(); this.props.onSetDescription(value); }, handleSetType: function(e, value) { this.props.onSetType(value); }, handleSetUrl: function() { var value = this.refs['textFieldUrl' + this.props.channelId].getValue(); switch(this.props.channel.type) { case 'web': var prefix = value.slice(0, 7); if(prefix === 'http://') { // do nothing } else { value = 'http://' + value; } break; case 'iOS': value = value + '://'; break; default: } this.props.onSetUrl(value); }, validateInputName: function() { var value = this.refs['textFieldName' + this.props.channelId].getValue(); if(value.length > 30) { event.returnValue = false; } }, validateInputDescription: function() { var value = this.refs['textFieldDescription' + this.props.channelId].getValue(); if(value.length > 50) { event.returnValue = false; } }, handleUploadImage: function(e) { var node = e.target; if (node.files && node.files[0]) { var reader = new FileReader(); reader.onload = this.handleLoadedImage; reader.readAsDataURL(node.files[0]); // Store file for later upload this.imageFile_ = node.files[0]; } }, handleLoadedImage: function(e) { // Set as temporary screenshot var imageData = e.target.result; this.imageData_ = imageData; this.refs['channel' + this.props.channelId].getDOMNode().style.backgroundImage = 'url(' + imageData + ')'; }, /** * Called on every Channel every time Save Changes is pressed */ handleStoreImageOnServer: function() { var self = this; if(this.imageFile_) { ReactMain.imagesQueue++; nutella.net.bin.uploadFile(this.imageFile_, function(url) { self.props.onSetScreenshot(self.props.channelId, url); ReactMain.imagesQueue--; if(ReactMain.imagesQueue === 0) { self.props.onSaveCallback(); } }); } }, render: function() { var colorPicker = null; var onTouchTapIcon = this.handleModifyIcon; var backgroundImage; if(this.imageData_) { backgroundImage = 'url(' + this.imageData_ + ')'; } else { backgroundImage = 'url(' + this.props.channel.screenshot + ')'; } var style = { backgroundImage: backgroundImage, backgroundSize: '100% 100%' }; var iconStyle = { backgroundColor: this.props.channel.icon }; if(this.state.selected) { if (this.state.modifyField === 'icon') { colorPicker = ; onTouchTapIcon = null; } } var urlPlaceholder; var urlPrefix = null; var urlSuffix = null; var urlCleaned = this.props.channel.url; switch(this.props.channel.type) { case 'web': urlPlaceholder = 'URL:'; urlPrefix = 'http://'; urlCleaned = urlCleaned.slice(7, urlCleaned.length); break; case 'iOS': urlPlaceholder = 'Custom URL:'; urlSuffix = '://'; urlCleaned = urlCleaned.slice(0, urlCleaned.length - 3); break; default: urlPlaceholder = 'URL:'; } var cardBack =
{urlPlaceholder} {urlPrefix} {urlSuffix}
; var catalogueCard =

{this.props.channel.name}

{this.props.channel.description}

{cardBack}
; var detailCard =
{colorPicker}
Upload screenshot
{cardBack}
; return this.state.selected? detailCard : catalogueCard; } }); module.exports = Channel;