From 1285bac4b8afff8f06cf5f297ae621b768862c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A4rt=20Kalmo?= Date: Wed, 26 Apr 2017 09:23:53 +0300 Subject: [PATCH] done state --- .gitignore | 11 ++++++++ app/app.js | 6 +++++ app/board.ctrl.js | 33 +++++++++++++++++++++++ app/board.srv.js | 40 ++++++++++++++++++++++++++++ app/switch.cmp.html | 3 +++ app/switch.cmp.js | 30 +++++++++++++++++++++ gulpfile.js | 43 ++++++++++++++++++++++++++++++ img/switch-off.svg | 64 +++++++++++++++++++++++++++++++++++++++++++++ img/switch-on.svg | 47 +++++++++++++++++++++++++++++++++ index.html | 35 +++++++++++++++++++++++++ package.json | 17 ++++++++++++ styles.css | 17 ++++++++++++ 12 files changed, 346 insertions(+) create mode 100644 .gitignore create mode 100644 app/app.js create mode 100644 app/board.ctrl.js create mode 100644 app/board.srv.js create mode 100644 app/switch.cmp.html create mode 100644 app/switch.cmp.js create mode 100644 gulpfile.js create mode 100644 img/switch-off.svg create mode 100644 img/switch-on.svg create mode 100644 index.html create mode 100644 package.json create mode 100644 styles.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bdc27f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +node_modules + + +/dist +/.settings +/.classpath +/.project + +/.idea +*.iml +*.log diff --git a/app/app.js b/app/app.js new file mode 100644 index 0000000..14c5564 --- /dev/null +++ b/app/app.js @@ -0,0 +1,6 @@ +(function () { + 'use strict'; + + angular.module('app', []); +})(); + diff --git a/app/board.ctrl.js b/app/board.ctrl.js new file mode 100644 index 0000000..33788a7 --- /dev/null +++ b/app/board.ctrl.js @@ -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; + }); + } + + } + +})(); + + diff --git a/app/board.srv.js b/app/board.srv.js new file mode 100644 index 0000000..da0455d --- /dev/null +++ b/app/board.srv.js @@ -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(); + } + + } + + +})(); diff --git a/app/switch.cmp.html b/app/switch.cmp.html new file mode 100644 index 0000000..a5315d0 --- /dev/null +++ b/app/switch.cmp.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/app/switch.cmp.js b/app/switch.cmp.js new file mode 100644 index 0000000..5c1e5c0 --- /dev/null +++ b/app/switch.cmp.js @@ -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); + +})(); + diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..b48752c --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,43 @@ +var gulp = require('gulp'); +var inject = require('gulp-inject'); +var concat = require('gulp-concat'); +var uglify = require('gulp-uglify'); +var angularFilesort = require('gulp-angular-filesort'); +var del = require('del'); + +var paths = { + code: './app/**/*.js', + codeMin: 'app.min.js', + statics: ['app/**/*.html', 'img/**/*.*', '*.css'], + dist: './dist', + index: './index.html' +}; + +gulp.task('default', ['copy-static'], function () { + + var appScripts = gulp.src(paths.code) + .pipe(angularFilesort()) + .pipe(concat(paths.codeMin)) + .pipe(uglify()) + .pipe(gulp.dest(paths.dist)); + + return gulp + .src(paths.index) + .pipe(gulp.dest(paths.dist)) + .pipe(inject(appScripts, { relative: true })) + .pipe(gulp.dest(paths.dist)); + +}); + +gulp.task('copy-static', ['clean'], function() { + return gulp.src(paths.statics, { base: './' }) + .pipe(gulp.dest(paths.dist)); +}); + +gulp.task('clean', function() { + return new Promise(function(resolve, reject) { + del(paths.dist, function() { + resolve(); + }); + }); +}); diff --git a/img/switch-off.svg b/img/switch-off.svg new file mode 100644 index 0000000..74452c8 --- /dev/null +++ b/img/switch-off.svg @@ -0,0 +1,64 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/img/switch-on.svg b/img/switch-on.svg new file mode 100644 index 0000000..69f6c2e --- /dev/null +++ b/img/switch-on.svg @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/index.html b/index.html new file mode 100644 index 0000000..857941f --- /dev/null +++ b/index.html @@ -0,0 +1,35 @@ + + + + + Ng1ts + + + + +
+ + Light: + +
+ + Alarm: + +
+ + + + + + + + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..18a0ebb --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "ng1comp", + "version": "1.0.0", + "scripts": { + "serve": "http-server ./dist -a localhost -p 3000", + "build": "gulp" + }, + "devDependencies": { + "del": "^1.1.1", + "gulp": "^3.9.1", + "gulp-angular-filesort": "^1.1.1", + "gulp-concat": "^2.6.1", + "gulp-inject": "^4.2.0", + "gulp-uglify": "^2.0.0", + "http-server": "^0.9.0" + } +} diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..14dea04 --- /dev/null +++ b/styles.css @@ -0,0 +1,17 @@ + +.switch { + vertical-align: middle; + text-decoration: none; + background-size: 100% 100%; + width: 1em; + height: 1em; + content: url('img/switch-off.svg'); +} + +.switch.on { + content: url('img/switch-on.svg'); +} + +body { + font-family: arial, sans-serif; +}