/* global chrome */
var React = require('react');
var ReactDOM = require('react-dom');
var ReactCSSTransitionGroup = require('react-addons-css-transition-group');
var config = require('../config');
// React components
var Alert = require('./Alert.jsx');
var SitesList = require('./SitesList.jsx');
/**
* One thing to keep in mind is that you cannot use this.refs.blacklist if
* the blacklist select box is not being rendered on the form.
*
* @type {*|Function}
*/
var Options = React.createClass({
getInitialState: function () {
return {
theme: config.theme,
blacklist: '',
whitelist: '',
loggingType: config.loggingType,
loggingStyle: config.loggingStyle,
displayAlert: false,
alertType: config.alert.success.type,
alertText: config.alert.success.text
};
},
componentDidMount: function () {
this.restoreSettings();
},
restoreSettings: function () {
var that = this;
chrome.storage.sync.get({
theme: config.theme,
blacklist: '',
whitelist: '',
loggingType: config.loggingType,
loggingStyle: config.loggingStyle
}, function (items) {
that.setState({
theme: items.theme,
blacklist: items.blacklist,
whitelist: items.whitelist,
loggingType: items.loggingType,
loggingStyle: items.loggingStyle
});
ReactDOM.findDOMNode(that.refs.theme).value = items.theme;
ReactDOM.findDOMNode(that.refs.loggingType).value = items.loggingType;
ReactDOM.findDOMNode(that.refs.loggingStyle).value = items.loggingStyle;
});
},
_handleSubmit: function (e) {
e.preventDefault();
this.saveSettings();
},
saveSettings: function () {
var that = this;
var theme = ReactDOM.findDOMNode(this.refs.theme).value.trim();
var loggingType = ReactDOM.findDOMNode(this.refs.loggingType).value.trim();
var loggingStyle = ReactDOM.findDOMNode(this.refs.loggingStyle).value.trim();
// Trimming blacklist and whitelist removes blank lines and spaces.
var blacklist = that.state.blacklist.trim();
var whitelist = that.state.whitelist.trim();
// Sync options with google storage.
chrome.storage.sync.set({
theme: theme,
blacklist: blacklist,
whitelist: whitelist,
loggingType: loggingType,
loggingStyle: loggingStyle
}, function () {
// Set state to be newly entered values.
that.setState({
theme: theme,
blacklist: blacklist,
whitelist: whitelist,
loggingType: loggingType,
loggingStyle: loggingStyle,
displayAlert: true
});
});
},
_displayBlackOrWhiteList: function () {
var loggingStyle = ReactDOM.findDOMNode(this.refs.loggingStyle).value.trim();
this.setState({loggingStyle: loggingStyle});
},
_updateBlacklistState: function(sites){
this.setState({
blacklist: sites
});
},
_updateWhitelistState: function(sites){
this.setState({
whitelist: sites
});
},
render: function () {
var that = this;
var alert = function() {
if(that.state.displayAlert === true){
setTimeout(function () {
that.setState({displayAlert:false});
}, 2000);
return(