Browse Source

cide review

pull/2/head
Raymond Hill 10 years ago
parent
commit
2962bd19e2
  1. 35
      src/js/matrix.js

35
src/js/matrix.js

@ -81,7 +81,7 @@ var nameToStateMap = {
'inherit': 3 'inherit': 3
}; };
var nameToSwitchMap = {
var nameToSwitchStateMap = {
'true': true, 'true': true,
'false': false, 'false': false,
'on': false, // backward compatibility 'on': false, // backward compatibility
@ -242,9 +242,7 @@ Matrix.prototype.setSwitch = function(switchName, srcHostname, newState) {
return false; return false;
} }
var bits = this.switches[srcHostname] || 0; var bits = this.switches[srcHostname] || 0;
bits &= ~(3 << bitOffset);
bits |= newState ? 1 << bitOffset : 3 << bitOffset;
this.switches[srcHostname] = bits;
this.switches[srcHostname] = (bits & ~(3 << bitOffset)) | ((newState ? 1 : 2) << bitOffset);
return true; return true;
}; };
@ -497,16 +495,15 @@ Matrix.prototype.setSwitchZ = function(switchName, srcHostname, newState) {
if ( state === newState ) { if ( state === newState ) {
return true; return true;
} }
bits |= newState ? 1 << bitOffset : 3 << bitOffset;
this.switches[srcHostname] = bits;
this.switches[srcHostname] = bits | ((newState ? 1 : 2) << bitOffset);
return true; return true;
}; };
/******************************************************************************/ /******************************************************************************/
// 0 = default state, which meaning depends on the switch
// 0 = inherit from broader scope, up to default state
// 1 = non-default state // 1 = non-default state
// 3 = forced default state (to override a broader non-default state)
// 2 = forced default state (to override a broader non-default state)
Matrix.prototype.evaluateSwitch = function(switchName, srcHostname) { Matrix.prototype.evaluateSwitch = function(switchName, srcHostname) {
var bits = this.switches[srcHostname] || 0; var bits = this.switches[srcHostname] || 0;
@ -517,16 +514,11 @@ Matrix.prototype.evaluateSwitch = function(switchName, srcHostname) {
if ( bitOffset === undefined ) { if ( bitOffset === undefined ) {
return false; return false;
} }
bits = bits >> bitOffset & 3;
return bits === 1;
return ((bits >> bitOffset) & 3) === 1;
}; };
/******************************************************************************/ /******************************************************************************/
// 0 = default state, which meaning depends on the switch
// 1 = non-default state
// 3 = forced default state (to override a broader non-default state)
Matrix.prototype.evaluateSwitchZ = function(switchName, srcHostname) { Matrix.prototype.evaluateSwitchZ = function(switchName, srcHostname) {
var bitOffset = switchBitOffsets[switchName]; var bitOffset = switchBitOffsets[switchName];
if ( bitOffset === undefined ) { if ( bitOffset === undefined ) {
@ -690,17 +682,11 @@ Matrix.prototype.fromString = function(text, append) {
// Switch on/off // Switch on/off
// `switch:` srcHostname state // `switch:` srcHostname state
// state = [`on`, `off`]
// state = [`true`, `false`]
switchName = ''; switchName = '';
pos = fieldVal.indexOf('switch:');
if ( pos !== -1 ) {
fieldVal = 'matrix-off:';
} else {
pos = fieldVal.indexOf('matrix:');
if ( pos !== -1 ) {
if ( fieldVal === 'switch:' || fieldVal === 'matrix:' ) {
fieldVal = 'matrix-off:'; fieldVal = 'matrix-off:';
} }
}
pos = fieldVal.indexOf(':'); pos = fieldVal.indexOf(':');
if ( pos !== -1 ) { if ( pos !== -1 ) {
switchName = fieldVal.slice(0, pos); switchName = fieldVal.slice(0, pos);
@ -714,16 +700,15 @@ Matrix.prototype.fromString = function(text, append) {
continue; continue;
} }
// Unknown state: reject // Unknown state: reject
if ( nameToSwitchMap.hasOwnProperty(fieldVal) === false ) {
if ( nameToSwitchStateMap.hasOwnProperty(fieldVal) === false ) {
continue; continue;
} }
matrix.setSwitch(switchName, srcHostname, nameToSwitchMap[fieldVal]);
matrix.setSwitch(switchName, srcHostname, nameToSwitchStateMap[fieldVal]);
continue; continue;
} }
// Unknown directive // Unknown directive
pos = fieldVal.indexOf(':'); pos = fieldVal.indexOf(':');
if ( pos !== -1 ) { if ( pos !== -1 ) {
continue; continue;

Loading…
Cancel
Save