done state
This commit is contained in:
6
app/app.js
Normal file
6
app/app.js
Normal file
@@ -0,0 +1,6 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
angular.module('app', []);
|
||||
})();
|
||||
|
||||
33
app/board.ctrl.js
Normal file
33
app/board.ctrl.js
Normal file
@@ -0,0 +1,33 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
angular.module('app').controller('BoardCtrl', Ctrl);
|
||||
|
||||
Ctrl.$inject = ['board'];
|
||||
|
||||
function Ctrl(board) {
|
||||
var vm = this;
|
||||
|
||||
vm.state = {};
|
||||
|
||||
init();
|
||||
|
||||
vm.lightSwitchCallback = function (isOn) {
|
||||
board.setLight(isOn).then(init);
|
||||
};
|
||||
|
||||
vm.alarmSwitchCallback = function (isOn) {
|
||||
board.setAlarm(isOn).then(init);
|
||||
};
|
||||
|
||||
function init() {
|
||||
board.getBoardState().then(function (state) {
|
||||
vm.state = state;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
|
||||
40
app/board.srv.js
Normal file
40
app/board.srv.js
Normal file
@@ -0,0 +1,40 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
angular.module('app').service('board', Srv);
|
||||
|
||||
Srv.$inject = ['$q', '$window'];
|
||||
|
||||
function Srv($q, $window) {
|
||||
|
||||
this.getBoardState = getBoardState;
|
||||
this.setLight = setLight;
|
||||
this.setAlarm = setAlarm;
|
||||
|
||||
function getBoardState() {
|
||||
var state = JSON.parse($window.sessionStorage.getItem('BOARD-STATE'));
|
||||
return $q.when(state ? state : {});
|
||||
}
|
||||
|
||||
function setLight(isOn) {
|
||||
var state = getBoardState();
|
||||
state.light = isOn;
|
||||
saveState(state);
|
||||
return $q.when();
|
||||
}
|
||||
|
||||
function saveState(state) {
|
||||
$window.sessionStorage.setItem('BOARD-STATE', JSON.stringify(state));
|
||||
}
|
||||
|
||||
function setAlarm(isOn) {
|
||||
var state = getBoardState();
|
||||
state.alarm = isOn;
|
||||
saveState(state);
|
||||
return $q.when();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
})();
|
||||
3
app/switch.cmp.html
Normal file
3
app/switch.cmp.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<img class="switch"
|
||||
ng-click="$ctrl.toggle()"
|
||||
ng-class="{ on: $ctrl.isOn }"></img>
|
||||
30
app/switch.cmp.js
Normal file
30
app/switch.cmp.js
Normal file
@@ -0,0 +1,30 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var options = {
|
||||
templateUrl: 'app/switch.cmp.html',
|
||||
controller: Controller,
|
||||
bindings: {
|
||||
onChange: '=',
|
||||
initial: '<'
|
||||
}
|
||||
};
|
||||
|
||||
function Controller() {
|
||||
this.toggle = toggle;
|
||||
this.$onInit = $onInit;
|
||||
|
||||
function $onInit() {
|
||||
this.isOn = this.initial;
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
this.isOn = !this.isOn;
|
||||
this.onChange(this.isOn);
|
||||
}
|
||||
}
|
||||
|
||||
angular.module('app').component('switch', options);
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user