PushRules settings: coding: separate UI and data management

This commit is contained in:
manuroe 2016-01-15 11:29:03 +01:00
parent 7412fc7f97
commit c3469b5b51
1 changed files with 166 additions and 147 deletions

View File

@ -79,14 +79,138 @@ module.exports = React.createClass({
}, },
onNotifStateButtonClicked: function(event) { onNotifStateButtonClicked: function(event) {
var self = this;
var cli = MatrixClientPeg.get();
var vectorRuleId = event.target.className.split("-")[0]; var vectorRuleId = event.target.className.split("-")[0];
var newPushRuleVectorState = event.target.className.split("-")[1]; var newPushRuleVectorState = event.target.className.split("-")[1];
if ("keywords" === vectorRuleId if ("keywords" === vectorRuleId) {
&& this.state.vectorContentRules.vectorState !== newPushRuleVectorState this._changeKeywordsPushRuleVectorState(newPushRuleVectorState)
&& this.state.vectorContentRules.rules.length) { }
else {
var rule = this.getRule(vectorRuleId);
if (rule) {
this._changePushRuleVectorState(rule, newPushRuleVectorState);
}
}
},
onKeywordsClicked: function(event) {
var self = this;
// Compute the keywords list to display
var keywords = [];
for (var i in this.state.vectorContentRules.rules) {
var rule = this.state.vectorContentRules.rules[i];
keywords.push(rule.pattern);
}
if (keywords.length) {
// As keeping the order of per-word push rules hs side is a bit tricky to code,
// display the keywords in alphabetical order to the user
keywords.sort();
keywords = keywords.join(", ");
}
else {
keywords = "";
}
var TextInputDialog = sdk.getComponent("dialogs.TextInputDialog");
Modal.createDialog(TextInputDialog, {
title: "Keywords",
description: "Enter keywords separated by a comma:",
value: keywords,
onFinished: function onFinished(should_leave, newValue) {
if (should_leave && newValue !== keywords) {
var newKeywords = newValue.split(',');
for (var i in newKeywords) {
newKeywords[i] = newKeywords[i].trim();
}
// Remove duplicates and empty
newKeywords = newKeywords.reduce(function(array, keyword){
if (keyword !== "" && array.indexOf(keyword) < 0) {
array.push(keyword);
}
return array;
},[]);
self._updateKeywords(newKeywords);
}
}
});
},
getRule: function(vectorRuleId) {
for (var i in this.state.vectorPushRules) {
var rule = this.state.vectorPushRules[i];
if (rule.vectorRuleId === vectorRuleId) {
return rule;
}
}
},
_actionsFor: function(pushRuleVectorState) {
if (pushRuleVectorState === PushRuleVectorState.ON) {
return ['notify'];
}
else if (pushRuleVectorState === PushRuleVectorState.LOUD) {
return ['notify',
{'set_tweak': 'sound', 'value': 'default'},
{'set_tweak': 'highlight', 'value': 'true'}
];;
}
},
// Determine whether a rule is in the PushRuleVectorState.ON category or in PushRuleVectorState.LOUD
// regardless of its enabled state.
_pushRuleVectorStateKind: function(rule) {
var stateKind;
// Count tweaks to determine if it is a ON or LOUD rule
var tweaks = 0;
for (var j in rule.actions) {
var action = rule.actions[j];
if (action.set_tweak === 'sound' ||
(action.set_tweak === 'highlight' && action.value)) {
tweaks++;
}
}
switch (tweaks) {
case 0:
stateKind = PushRuleVectorState.ON;
break;
case 2:
stateKind = PushRuleVectorState.LOUD;
break;
}
return stateKind;
},
_changePushRuleVectorState: function(rule, newPushRuleVectorState) {
// For now, we support only enabled/disabled for hs default rules
// Translate ON, LOUD, OFF to one of the 2.
if (rule && rule.vectorState !== newPushRuleVectorState) {
this.setState({
phase: this.phases.LOADING
});
var self = this;
MatrixClientPeg.get().setPushRuleEnabled('global', rule.rule.kind, rule.rule.rule_id, (newPushRuleVectorState !== PushRuleVectorState.OFF)).done(function() {
self._refreshFromServer();
});
}
},
_changeKeywordsPushRuleVectorState: function(newPushRuleVectorState) {
// Is there really a change?
if (this.state.vectorContentRules.vectorState === newPushRuleVectorState
|| this.state.vectorContentRules.rules.length === 0) {
return;
}
var self = this;
var cli = MatrixClientPeg.get();
this.setState({ this.setState({
phase: this.phases.LOADING phase: this.phases.LOADING
@ -145,73 +269,17 @@ module.exports = React.createClass({
onFinished: self._refreshFromServer onFinished: self._refreshFromServer
}); });
}); });
} },
else {
var rule = this.getRule(vectorRuleId);
// For now, we support only enabled/disabled for hs default rules
// Translate ON, LOUD, OFF to one of the 2.
if (rule && rule.vectorState !== newPushRuleVectorState) {
_updateKeywords: function(newKeywords) {
this.setState({ this.setState({
phase: this.phases.LOADING phase: this.phases.LOADING
}); });
cli.setPushRuleEnabled('global', rule.rule.kind, rule.rule.rule_id, (newPushRuleVectorState !== PushRuleVectorState.OFF)).done(function() {
self._refreshFromServer();
});
}
}
},
onKeywordsClicked: function(event) {
var self = this; var self = this;
// Compute the keywords list to display
var keywords = [];
for (var i in this.state.vectorContentRules.rules) {
var rule = this.state.vectorContentRules.rules[i];
keywords.push(rule.pattern);
}
if (keywords.length) {
// As keeping the order of per-word push rules hs side is a bit tricky to code,
// display the keywords in alphabetical order to the user
keywords.sort();
keywords = keywords.join(", ");
}
else {
keywords = "";
}
var TextInputDialog = sdk.getComponent("dialogs.TextInputDialog");
Modal.createDialog(TextInputDialog, {
title: "Keywords",
description: "Enter keywords separated by a comma:",
value: keywords,
onFinished: function onFinished(should_leave, newValue) {
if (should_leave && newValue !== keywords) {
var cli = MatrixClientPeg.get(); var cli = MatrixClientPeg.get();
var removeDeferreds = []; var removeDeferreds = [];
var newKeywords = newValue.split(',');
for (var i in newKeywords) {
newKeywords[i] = newKeywords[i].trim();
}
// Remove duplicates and empty
newKeywords = newKeywords.reduce(function(array, keyword){
if (keyword !== "" && array.indexOf(keyword) < 0) {
array.push(keyword);
}
return array;
},[]);
self.setState({
phase: self.phases.LOADING
});
// Remove per-word push rules of keywords that are no more in the list // Remove per-word push rules of keywords that are no more in the list
var vectorContentRulesPatterns = []; var vectorContentRulesPatterns = [];
for (var i in self.state.vectorContentRules.rules) { for (var i in self.state.vectorContentRules.rules) {
@ -276,55 +344,6 @@ module.exports = React.createClass({
self._refreshFromServer(); self._refreshFromServer();
}, onError); }, onError);
}, onError); }, onError);
}
}
});
},
getRule: function(vectorRuleId) {
for (var i in this.state.vectorPushRules) {
var rule = this.state.vectorPushRules[i];
if (rule.vectorRuleId === vectorRuleId) {
return rule;
}
}
},
_actionsFor: function(pushRuleVectorState) {
if (pushRuleVectorState === PushRuleVectorState.ON) {
return ['notify'];
}
else if (pushRuleVectorState === PushRuleVectorState.LOUD) {
return ['notify',
{'set_tweak': 'sound', 'value': 'default'},
{'set_tweak': 'highlight', 'value': 'true'}
];;
}
},
// Determine whether a rule is in the PushRuleVectorState.ON category or in PushRuleVectorState.LOUD
// regardless of its enabled state.
_pushRuleVectorStateKind: function(rule) {
var stateKind;
// Count tweaks to determine if it is a ON or LOUD rule
var tweaks = 0;
for (var j in rule.actions) {
var action = rule.actions[j];
if (action.set_tweak === 'sound' ||
(action.set_tweak === 'highlight' && action.value)) {
tweaks++;
}
}
switch (tweaks) {
case 0:
stateKind = PushRuleVectorState.ON;
break;
case 2:
stateKind = PushRuleVectorState.LOUD;
break;
}
return stateKind;
}, },
_refreshFromServer: function() { _refreshFromServer: function() {