From feae1c7a93da564dd9695a6146b70c5501b10e71 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 19 Dec 2014 23:48:42 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightner.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 lightner.js diff --git a/lightner.js b/lightner.js new file mode 100644 index 0000000..f529641 --- /dev/null +++ b/lightner.js @@ -0,0 +1 @@ +coneole.log("Hello, world!") \ No newline at end of file From a5709a47a31d266eac965eceb35acbf50479a7d6 Mon Sep 17 00:00:00 2001 From: Anna Date: Sat, 20 Dec 2014 19:10:21 +0500 Subject: [PATCH 2/6] =?UTF-8?q?=D1=81=D0=B2=D0=B5=D1=82=D0=BE=D1=84=D0=BE?= =?UTF-8?q?=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightner.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/lightner.js b/lightner.js index f529641..0b53582 100644 --- a/lightner.js +++ b/lightner.js @@ -1 +1,85 @@ -coneole.log("Hello, world!") \ No newline at end of file +/** + * Светофор + * + * @param {Object} config + * @constructor + */ + +var config = Object.create(null); + +var config = { + red: 40, + yellow: 5, + green: 45, + get time() { + return this.red + this.yellow + this.green; + }, + set time(time) { + if (time < this.red + this.yellow) { + throw new Error('Bad value'); + } + + this.green = time - this.red - this.yellow; + } +}; + +function Lights(config) { + // Публичные поля + this.state = null; + + // Приватныt поля + this._timeout = null; + this._config = config; + + /** + * Переключение в зеленый + */ + Lights.prototype.toGreen = function () { + console.log("Change to green"); + if (this._timeout) { + clearTimeout(this._timeout); + } + + this.state = 'green'; + this._timeout = setTimeout(function () { + this.toYellow(); + }.bind(this), this._config.green * 1000) + }; + /** + * Переключение в желтый + */ + Lights.prototype.toYellow = function () { + console.log("Change to yellow"); + if (this._timeout) { + clearTimeout(this._timeout); + } + + this.state = 'yellow'; + this._timeout = setTimeout(function () { + this.toRed(); + }.bind(this), this._config.yellow * 1000) + }; + /** + * Переключение в красный + */ + Lights.prototype.toRed = function () { + console.log("Change to red"); + if (this._timeout) { + clearTimeout(this._timeout); + } + + this.state = 'red'; + this._timeout = setTimeout(function () { + this.toGreen(); + }.bind(this), this._config.red * 1000) + }; + +} + +var lights = new Lights(config); +lights.toYellow(); + +setInterval(function () { + console.log(lights.state); +}, 500); + From c5fd6ebd74814c8162e0804e8785c8cf5bca71f7 Mon Sep 17 00:00:00 2001 From: Anna Date: Sat, 20 Dec 2014 23:40:36 +0500 Subject: [PATCH 3/6] =?UTF-8?q?=D0=98=D0=B7=D0=BE=D0=B1=D1=80=D0=B0=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B2=D0=B5=D1=82=D0=BE=D1=84?= =?UTF-8?q?=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightner.js | 156 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 60 deletions(-) diff --git a/lightner.js b/lightner.js index 0b53582..41fa01c 100644 --- a/lightner.js +++ b/lightner.js @@ -1,85 +1,121 @@ +/** +* Отрисовка светофора +*/ +var canvas = document.getElementById("test"); +var ctx = canvas.getContext('2d'); +ctx.strokeStyle = 'red'; +ctx.strokeRect(15, 15, 100, 100); +ctx.strokeStyle = 'yellow'; +ctx.strokeRect(15, 125, 100, 100); +ctx.strokeStyle = 'green'; +ctx.strokeRect(15, 235, 100, 100); + +function colorRed(){ + ctx.fillStyle = 'red'; + ctx.fillRect(20, 20, 90, 90); + ctx.fillStyle = 'white'; + ctx.fillRect(20, 130, 90, 90); +} +function colorYellow(){ + ctx.fillStyle = 'yellow'; + ctx.fillRect(20, 130, 90, 90); + ctx.fillStyle = 'white'; + ctx.fillRect(20, 240, 90, 90); +} +function colorGreen(){ + ctx.fillStyle = 'green'; + ctx.fillRect(20, 240, 90, 90); + ctx.fillStyle = 'white'; + ctx.fillRect(20, 20, 90, 90); +} + + +/** +* Event Emitter +*/ + + + + + /** * Светофор * * @param {Object} config * @constructor */ - var config = Object.create(null); var config = { - red: 40, - yellow: 5, - green: 45, - get time() { - return this.red + this.yellow + this.green; - }, - set time(time) { - if (time < this.red + this.yellow) { - throw new Error('Bad value'); - } - - this.green = time - this.red - this.yellow; - } + red: 10, + yellow: 3, + green: 15, + tramGreen: 10, + tramWaiting: 3 }; function Lights(config) { - // Публичные поля + this.state = null; - - // Приватныt поля + this._timeout = null; this._config = config; +} - /** - * Переключение в зеленый - */ - Lights.prototype.toGreen = function () { - console.log("Change to green"); - if (this._timeout) { - clearTimeout(this._timeout); - } +/** +* Переключение в зеленый +*/ +Lights.prototype.toGreen = function () { + console.log("Change to green"); - this.state = 'green'; - this._timeout = setTimeout(function () { - this.toYellow(); - }.bind(this), this._config.green * 1000) - }; - /** - * Переключение в желтый - */ - Lights.prototype.toYellow = function () { - console.log("Change to yellow"); - if (this._timeout) { - clearTimeout(this._timeout); - } + this.state = 'green'; + colorGreen(); + + this._timeout = setTimeout(function () { + this.toYellow(); + }.bind(this), this._config.green * 1000) +}; + +/** +* Переключение в желтый +*/ +Lights.prototype.toYellow = function () { + console.log("Change to yellow"); - this.state = 'yellow'; - this._timeout = setTimeout(function () { - this.toRed(); - }.bind(this), this._config.yellow * 1000) - }; - /** - * Переключение в красный - */ - Lights.prototype.toRed = function () { - console.log("Change to red"); - if (this._timeout) { - clearTimeout(this._timeout); - } + this.state = 'yellow'; + colorYellow(); + + this._timeout = setTimeout(function () { + this.toRed(); + }.bind(this), this._config.yellow * 1000) +}; + +/** + * Переключение в красный + */ +Lights.prototype.toRed = function () { + console.log("Change to red"); - this.state = 'red'; - this._timeout = setTimeout(function () { - this.toGreen(); - }.bind(this), this._config.red * 1000) - }; - + this.state = 'red'; + colorRed(); + + this._timeout = setTimeout(function () { + this.toGreen(); + }.bind(this), this._config.red * 1000) +}; + +/** +* Состояние светофора +*/ +Lights.prototype.state = function () { + console.log(this.state); } var lights = new Lights(config); -lights.toYellow(); - +lights.toRed(); + + setInterval(function () { console.log(lights.state); }, 500); - + \ No newline at end of file From 9c9bf548e52340a2c7a59d65277c0bcae2661f51 Mon Sep 17 00:00:00 2001 From: Anna Date: Sun, 21 Dec 2014 22:40:28 +0500 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A1=D0=B2=D0=B5=D1=82=D0=BE=D1=84=D0=BE?= =?UTF-8?q?=D1=80=20=D1=81=20=D1=82=D1=80=D0=B0=D0=BC=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightner.js | 227 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 165 insertions(+), 62 deletions(-) diff --git a/lightner.js b/lightner.js index 41fa01c..6f0fd00 100644 --- a/lightner.js +++ b/lightner.js @@ -11,29 +11,70 @@ ctx.strokeStyle = 'green'; ctx.strokeRect(15, 235, 100, 100); function colorRed(){ + clearColor(); ctx.fillStyle = 'red'; ctx.fillRect(20, 20, 90, 90); - ctx.fillStyle = 'white'; - ctx.fillRect(20, 130, 90, 90); } function colorYellow(){ + clearColor(); ctx.fillStyle = 'yellow'; ctx.fillRect(20, 130, 90, 90); - ctx.fillStyle = 'white'; - ctx.fillRect(20, 240, 90, 90); + } function colorGreen(){ + clearColor(); ctx.fillStyle = 'green'; ctx.fillRect(20, 240, 90, 90); + +} +function clearColor(){ ctx.fillStyle = 'white'; ctx.fillRect(20, 20, 90, 90); + ctx.fillRect(20, 130, 90, 90); + ctx.fillRect(20, 240, 90, 90); } /** * Event Emitter */ - +function EventEmitter(){ + this._eventListener = {}; + + /** + * Добавляем событие + */ + EventEmitter.prototype.addListener = function(eventName,listener){ + if(this._eventListener[eventName] === undefined){ + this._eventListener[eventName] = []; + } + this._eventListener[eventName].push(listener); + } + /** + * Удаляем событие + */ + EventEmitter.prototype.deleteListener = function(eventName,listener){ + if(this._eventListener[eventName] !== undefined){ + for(var i = 0; i < this._eventListener[eventName].length; i++){ + if(this._eventListener[eventName][i] === listener) + this._eventListener[eventName].splice(i,1); + } + } + } + EventEmitter.prototype.returnListener = function(eventName, position, data){ + return function() { + this._eventListener[eventName][position](data); + }.bind(this); + } + + EventEmitter.prototype.emit = function (eventName,data) { + if (this._eventListener[eventName] !== undefined) { + for (var i = 0; i < this._eventListener[eventName].length; i++) { + setTimeout(this.returnListener(eventName, i, data), 0); + } + } + } +} @@ -49,73 +90,135 @@ var config = Object.create(null); var config = { red: 10, yellow: 3, - green: 15, - tramGreen: 10, - tramWaiting: 3 + green: 10, }; function Lights(config) { - this.state = null; - + this._state = null; this._timeout = null; this._config = config; -} -/** -* Переключение в зеленый -*/ -Lights.prototype.toGreen = function () { - console.log("Change to green"); - - this.state = 'green'; - colorGreen(); - - this._timeout = setTimeout(function () { - this.toYellow(); - }.bind(this), this._config.green * 1000) -}; + this._tramMode = null; + this._tramState = null; + this._tramWating = 3; + this._tramGreen = 10; + + /** + * Переключение в зеленый + */ + Lights.prototype.toGreen = function () { + console.log("Change to green"); + + this._state = 'green'; + colorGreen(); + + this._timeout = setTimeout(function () { + this.toYellow(); + }.bind(this), this._config.green * 1000) + }; + + /** + * Переключение в желтый + */ + Lights.prototype.toYellow = function () { + console.log("Change to yellow"); + + this._state = 'yellow'; + colorYellow(); + + this._timeout = setTimeout(function () { + this.toRed(); + }.bind(this), this._config.yellow * 1000) + }; + + /** + * Переключение в красный + */ + Lights.prototype.toRed = function () { + console.log("Change to red"); + + this._state = 'red'; + colorRed(); + + this._timeout = setTimeout(function () { + this.toGreen(); + }.bind(this), this._config.red * 1000) + }; + + /** + * Состояние светофора + */ + Lights.prototype.state = function () { + console.log(this._state); + return this._state; + } + + /** + * Возвращаем состояние светофора + */ + Lights.prototype.tramBuyBuy = function(){ + console.log("Tram Buy Buy!"); + document.getElementById('textTram').innerHTML = ""; + this._tramMode = null; + this._tramState = null; + switch (this._state){ + case 'red': lights.toRed(); break; + case 'yellow': lights.toYellow(); break; + case 'green': lights.toGreen(); break; + } + } + + /** + * Трамвай подъехал + */ + Lights.prototype.tramToGreen = function(){ + this._tramMode = 'Tram is there'; + console.log(this._tramMode); + document.getElementById('textTram').innerHTML = this._tramMode; + + this._tramState = 'green'; + // colorGreen(); + var tmp = this._tramGreen - this._tramWating; + this._timeout = setTimeout(function(){ + this.tramBuyBuy(); + }.bind(this), tmp * 2000) + }; + + /** + * Трамвай скоро будет + */ + Lights.prototype.drivers = function(){ + if (!this._tramMode) { + this._tramMode = 'Tram wating'; + console.log(this._tramMode); + document.getElementById('textTram').innerHTML = this._tramMode; + + clearTimeout(this._timeout); + colorGreen(); + this._timeout = setTimeout(function(){ + this.tramToGreen(); + }.bind(this),this._tramWating * 1000) + }; + }; + + /** + * Прослушка + */ + Lights.prototype.tramSubscribe = function(eventName){ + eventName.addListener('tram', function() { + this.drivers(); + }.bind(this)); + } +} -/** -* Переключение в желтый -*/ -Lights.prototype.toYellow = function () { - console.log("Change to yellow"); - - this.state = 'yellow'; - colorYellow(); - - this._timeout = setTimeout(function () { - this.toRed(); - }.bind(this), this._config.yellow * 1000) -}; -/** - * Переключение в красный - */ -Lights.prototype.toRed = function () { - console.log("Change to red"); - - this.state = 'red'; - colorRed(); - - this._timeout = setTimeout(function () { - this.toGreen(); - }.bind(this), this._config.red * 1000) -}; -/** -* Состояние светофора -*/ -Lights.prototype.state = function () { - console.log(this.state); -} var lights = new Lights(config); -lights.toRed(); - - +lights.toGreen(); +ev = new EventEmitter(); +lights.tramSubscribe(ev); setInterval(function () { - console.log(lights.state); -}, 500); - \ No newline at end of file + ev.emit('tram') +}, 11000); \ No newline at end of file From 14325bb4f3baac0b64d167072a12b401edb11240 Mon Sep 17 00:00:00 2001 From: Anna Date: Sun, 21 Dec 2014 22:46:26 +0500 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A1=D0=B2=D0=B5=D1=82=D0=BE=D1=84=D0=BE?= =?UTF-8?q?=D1=80=20=D1=81=20=D1=82=D1=80=D0=B0=D0=BC=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC,=20=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D1=82=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B0=D0=BD=D0=B3=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightner.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lightner.js b/lightner.js index 6f0fd00..4e8662a 100644 --- a/lightner.js +++ b/lightner.js @@ -1,5 +1,5 @@ /** -* Отрисовка светофора +* Draw traffic light */ var canvas = document.getElementById("test"); var ctx = canvas.getContext('2d'); @@ -42,7 +42,7 @@ function EventEmitter(){ this._eventListener = {}; /** - * Добавляем событие + * Add event */ EventEmitter.prototype.addListener = function(eventName,listener){ if(this._eventListener[eventName] === undefined){ @@ -51,7 +51,7 @@ function EventEmitter(){ this._eventListener[eventName].push(listener); } /** - * Удаляем событие + * Delete event */ EventEmitter.prototype.deleteListener = function(eventName,listener){ if(this._eventListener[eventName] !== undefined){ @@ -61,6 +61,7 @@ function EventEmitter(){ } } } + EventEmitter.prototype.returnListener = function(eventName, position, data){ return function() { this._eventListener[eventName][position](data); @@ -80,10 +81,7 @@ function EventEmitter(){ /** - * Светофор - * - * @param {Object} config - * @constructor + * Traffic light */ var config = Object.create(null); @@ -105,7 +103,7 @@ function Lights(config) { this._tramGreen = 10; /** - * Переключение в зеленый + * To Green */ Lights.prototype.toGreen = function () { console.log("Change to green"); @@ -119,7 +117,7 @@ function Lights(config) { }; /** - * Переключение в желтый + * To Yellow */ Lights.prototype.toYellow = function () { console.log("Change to yellow"); @@ -133,7 +131,7 @@ function Lights(config) { }; /** - * Переключение в красный + * To Red */ Lights.prototype.toRed = function () { console.log("Change to red"); @@ -147,7 +145,7 @@ function Lights(config) { }; /** - * Состояние светофора + * Traffic light state */ Lights.prototype.state = function () { console.log(this._state); @@ -155,7 +153,7 @@ function Lights(config) { } /** - * Возвращаем состояние светофора + * Return old state traffic light */ Lights.prototype.tramBuyBuy = function(){ console.log("Tram Buy Buy!"); @@ -170,7 +168,7 @@ function Lights(config) { } /** - * Трамвай подъехал + * Tram is there */ Lights.prototype.tramToGreen = function(){ this._tramMode = 'Tram is there'; @@ -186,7 +184,7 @@ function Lights(config) { }; /** - * Трамвай скоро будет + * Tram wating */ Lights.prototype.drivers = function(){ if (!this._tramMode) { @@ -203,7 +201,7 @@ function Lights(config) { }; /** - * Прослушка + * Subscribe */ Lights.prototype.tramSubscribe = function(eventName){ eventName.addListener('tram', function() { @@ -213,8 +211,9 @@ function Lights(config) { } - - +/** +* Start program +*/ var lights = new Lights(config); lights.toGreen(); ev = new EventEmitter(); From 228e8aea87dfa8ba997a3e7a165d2385414bebea Mon Sep 17 00:00:00 2001 From: Anna Date: Sun, 21 Dec 2014 22:49:38 +0500 Subject: [PATCH 6/6] =?UTF-8?q?=D0=A1=D0=B2=D0=B5=D1=82=D0=BE=D1=84=D0=BE?= =?UTF-8?q?=D1=80=20=D1=81=20=D1=82=D1=80=D0=B0=D0=BC=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lightning.html | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lightning.html diff --git a/lightning.html b/lightning.html new file mode 100644 index 0000000..fdb3597 --- /dev/null +++ b/lightning.html @@ -0,0 +1,10 @@ + + + + + + Элемент не поддерживается +

+ + + \ No newline at end of file