diff --git a/Collection.js b/Collection.js new file mode 100644 index 0000000..ab6c5ee --- /dev/null +++ b/Collection.js @@ -0,0 +1,33 @@ +/* +* + - абстрактная коллекция объектов +* Collection.prototype.add - добавление объекта в коллекцию +* +*/ +var Collection = function (elem) { + 'use strict'; + this.elem = []; + var key; + for (key in elem) { + this.elem.push(elem[key]); + } + }; + +Collection.prototype.add = function (model) { + 'use strict'; + var key; + //console.log(model); + if (model.length > 0) { + for (key in model) { + // console.log(model[key]); + model[key] = new Event(model[key]); + if (model[key].validate() === true) { + this.elem.push(model[key]); + } + } + } else { + if (model.validate() === true) { + this.elem.push(model); + } + } +}; \ No newline at end of file diff --git a/Event.js b/Event.js new file mode 100644 index 0000000..2faa84f --- /dev/null +++ b/Event.js @@ -0,0 +1,38 @@ +/* +* Event - объект события в календаре. Объект наследуется от Model +* Event.prototype.validate - проверяет корректность полей объекта +* +* function inherits(Constructor, SuperConstructor) - функция для чистого наследования +*/ +var Event = function (data) { + "use strict"; + Model.apply(this, arguments); + this.name = this.name || "Встреча"; + this.place = this.place || {}; + this.info = this.info || {}; + this.reminder = this.reminder || "За день до встречи"; + this.type = this.type || "Работа"; + this.party = this.party || "участвую"; + }; + +function inherits(Constructor, SuperConstructor) { + "use strict"; + var F = function () {}; + F.prototype = SuperConstructor.prototype; + Constructor.prototype = new F(); +} + +inherits(Event, Model); + +Event.prototype.validate = function () { + "use strict"; + if (this.start === "undefined") { + console.log("starts is can not be null"); + return "starts is can not be null"; + } + if (this.end !== "undefined" && this.end < this.start) { + console.log("can't end before it starts"); + return "can't end before it starts"; + } + return true; +}; diff --git a/Events.js b/Events.js new file mode 100644 index 0000000..cca1d6e --- /dev/null +++ b/Events.js @@ -0,0 +1,72 @@ +/* +* Events - коллекция событий в календаре. Объект наследуется от Collection +* +* Event - объект события в календаре +* Event.prototype.validate - проверяет корректность полей объекта +* Events.prototype.filterToDate - возвращает предстоящие или прощедшие события в зависимости от входящего параметра flag +* Events.prototype.FilterToParty - возвращает события, в которых я принимаю/ не принимаю участие в зависимости от входящего параметра flag +* Events.prototype.sortToDate - сортирует события по дате +* function str2date(s) - преобразует строку в дату +*/ +var Events = function (items) { + "use strict"; + Collection.apply(this, arguments); +}; +inherits(Events, Collection); + +Events.prototype.constructor = Events; + +function str2date(s) { + "use strict"; + var dateParts = s.split('.'); + if (typeof dateParts[2] === 'string') { + return new Date(dateParts[2], dateParts[1], dateParts[0]); + } + if (typeof dateParts[2] === 'undefined') { + dateParts = s.split('-'); + return new Date(dateParts[0], dateParts[1], dateParts[2]); + } +} + +Events.prototype.filterToDate = function (flag) { + "use strict"; + var result, collection; + collection = this.elem; + if (flag === -1) { + result = collection.filter(function (collection) { + var s = str2date(collection.start); + return s < new Date(); + }); + } else { + result = collection.filter(function (collection) { + var s = str2date(collection.start); + return s >= new Date(); + }); + } + return result; +}; + +Events.prototype.FilterToParty = function (flag) { + "use strict"; + var result, collection; + collection = this.elem; + if (flag === -1) { + result = collection.filter(function (collection) { + return collection.party === "не участвую"; + }); + } else { + result = collection.filter(function (collection) { + return collection.party === "участвую"; + }); + } + return result; +}; + +Events.prototype.sortToDate = function () { + "use strict"; + var collection = this.elem; + collection.sort(function (a, b) { + return str2date(a.start) > str2date(b.start) ? 1 : -1; + }); + return collection; +}; \ No newline at end of file diff --git a/Model.js b/Model.js new file mode 100644 index 0000000..c224f44 --- /dev/null +++ b/Model.js @@ -0,0 +1,38 @@ +/* +* Model - абстрактный объект +* Model.prototype.set - устанавливает аттрибуты и значения атрибутов, в соответсвии с принятым в качестве параметра объектом +* Model.prototype.get - возвращает запрашиваемое свойство у объекта +* Model.prototype.validate - проверяет корректность полей объекта +*/ +var Model = function (attributes) { + "use strict"; + var key; + for (key in attributes) { + if (attributes.hasOwnProperty(key)) { + this[key] = attributes[key]; + } + } + }; + +Model.prototype.set = function (attributes) { + "use strict"; + var key; + for (key in attributes) { + if (attributes.hasOwnProperty(key)) { + this[key] = attributes[key]; + } + } +}; + +Model.prototype.get = function (attribute) { + "use strict"; + if (this.hasOwnProperty(attribute)) { + return this[attribute]; + } + return undefined; +}; + +Model.prototype.validate = function (attributes) { + "use strict"; + throw new Error('this is Abstract method'); +}; diff --git a/async.js b/async.js new file mode 100644 index 0000000..b0774ba --- /dev/null +++ b/async.js @@ -0,0 +1,34 @@ +function asyncXHR(method, url, data, writeToFile) { + 'use strict'; + var xhr = new XMLHttpRequest(); + xhr.open(method, url, true); + xhr.addEventListener('readystatechange', function () { + // console.log(xhr.status); + if (xhr.readyState === 4) { + if (xhr.status === 200) { + console.log(xhr.responseText); + writeToFile(xhr.responseText); + } else { + console.log("error: status != 200 "); + } + } + }); + xhr.send(data); +} + +//var testEvent= new Event({"name": "Pewpe", "start": "11.12.2012", "end": "13.12.2012"}); +var coll = new Events(); + +writeToFile = function (text) { + 'use strict'; +// console.log("функция writetoFile"); + var newEvents, key; + newEvents = JSON.parse(text); +// console.log("сейчас будет вывод объекта"); +// console.log(newEvents.length); + for (key in newEvents) { + coll.add(newEvents[key]); + console.log(coll); + document.getElementById('contekst').innerHTML = text; + } +} diff --git a/createevent.js b/createevent.js new file mode 100644 index 0000000..bf88fe1 --- /dev/null +++ b/createevent.js @@ -0,0 +1,207 @@ +/* +* function Event(collection) - создает объект +* @param {collection} - массив со значениями полей +* function create() - добавляет событие в список событий при отпревке данных из формы +* function content() - выводит полную информацию событии при клике на него +* function filterAll() - функция фильтрует или сортирует события в зависимости от значений формы фильтрации +* function all() - выводит полный список событий по нажатию на кнопку "Вывести все события" +* function str2date(s) - преобразует дату начала и окончания события +* function FilterToDate(collection, flag) - возвращает предстоящие или прощедшие события в зависимости от значения flag +* function FilterToParty(collection, flag) - возвращает события, в которых я принимаю/ не принимаю участие в зависимости от значения flag +* function SortToDate(collection) сортирует встречи по дате +*/ +var hash = []; +var collection = []; +/*function Event(collection) { + "use strict"; + if (typeof collection.start.value === "undefined" || collection.start.value.length === 0) { + alert("Дата начала встречи задана не корректно"); + throw new TypeError("should be date"); + } + return { + "name": collection.name.value || "Встреча", + "start": collection.start.value, + "end": collection.end.value, +// "participants": collection.participants.value || {}, +// "organizer": collection.organizer.value || {}, + "place": collection.place.value || {}, + "info": collection.info.value || {}, + "reminder": collection.reminder.value || "За день до встречи", + "type": collection.type.value || "Работа", + "party": collection.party.value || "участвую" + }; +} */ +function create() { + "use strict"; + var newEvent, el; + newEvent = new Event(document.forms[0]); + el = document.createElement('div'); + el.className = 'oneEvent'; + el.setAttribute('id', 'oneEvent' + hash.length); + el.textContent = newEvent.start + "-" + newEvent.end + " " + newEvent.name; + spisok.appendChild(el); + document.getElementById('oneEvent' + hash.length).addEventListener('click', content, true); + hash[hash.length] = newEvent; +} +function content() { + while (contekst.childNodes.length > 0) { + contekst.removeChild(contekst.childNodes[0]); + } + var newEvent = new Event(document.forms[0]); + var el = document.getElementById('meeting').cloneNode(true); + el.className = 'elements'; + var list = el.getElementsByTagName('label'); + for (i=0; i 0) { + if (list[i].childNodes[n - 1].nodeType === 1) { + list[i].removeChild(list[i].childNodes[n - 1]); + } + n = n - 1; + } + for (k in newEvent) { + if (this.className === 'oneEventCollection') { + if (k === document.forms[0].elements[i].name && typeof newEvent[k] === 'string') { + list[i].textContent = list[i].textContent + " " + collection[this.id.charAt(this.id.length - 1)][k]; + } + if (k === document.forms[0].elements[i].name && typeof newEvent[k] !== 'string') { + list[i].textContent = list[i].textContent + " " + collection[this.id.charAt(this.id.length - 1)][k].value; + } + } else { + if (k === document.forms[0].elements[i].name && typeof newEvent[k] === 'string') { + list[i].textContent = list[i].textContent + " " + hash[this.id.charAt(this.id.length - 1)][k]; + } + if (k === document.forms[0].elements[i].name && typeof newEvent[k] !== 'string') { + list[i].textContent = list[i].textContent + " " + hash[this.id.charAt(this.id.length - 1)][k].value; + } + } + } + } + console.log(el.getElementsByTagName('input').length); + el.removeChild(el.childNodes[el.childNodes.length - 2]); + contekst.appendChild(el); +} +window.onload = function () { + "use strict"; + document.getElementById('submitEvent').addEventListener('click', create, true); + document.getElementById('sort').addEventListener('click', filterAll, true); + document.getElementById('all').addEventListener('click', all, true); + asyncXHR('get', 'test.json', null, writeToFile); +} + +writeToFile = function (data) { + //coll.add(testEvent); + //console.log(coll); + document.getElementById('contekst').innerHTML = data; +} + + +function filterAll() { + "use strict"; + var collection2 = []; + while (spisok.childNodes.length > 0) { + spisok.removeChild(spisok.childNodes[0]); + } + if (document.forms[1].elements[0].value === "предстоящие") { + collection2 = new FilterToDate(hash, 1); + } + if (document.forms[1].elements[0].value === "прошедшие") { + collection2 = new FilterToDate(hash, -1); + } + if (document.forms[1].elements[0].value === "я не участвую") { + collection2 = new FilterToParty(hash, -1); + } + if (document.forms[1].elements[0].value === "я участвую") { + collection2 = new FilterToParty(hash, 1); + } + + if (document.forms[1].elements[1].value === "предстоящие") { + collection = new FilterToDate(collection2, 1); + } + if (document.forms[1].elements[1].value === "прошедшие") { + collection = new FilterToDate(collection2, -1); + } + if (document.forms[1].elements[1].value === "я не участвую") { + collection = new FilterToParty(collection2, -1); + } + if (document.forms[1].elements[1].value === "я участвую") { + collection = new FilterToParty(collection2, 1); + } + if (document.forms[1].elements[1].value === "сортировать по дате") { + collection = [].concat(new SortToDate(collection2)); + } + for (i = 0; i < collection.length; i ++) { + var el = document.createElement('div'); + el.className = 'oneEventCollection'; + el.setAttribute('id', 'oneEvent' + i); + el.textContent = collection[i].start + "-" + collection[i].end + " " + collection[i].name; + spisok.appendChild(el); + document.getElementById('oneEvent' + i).addEventListener('click', content, true); + } +} + +function all() { + "use strict"; + while (spisok.childNodes.length > 0) { + spisok.removeChild(spisok.childNodes[0]); + } + for (i = 0; i < hash.length; i++) { + var el = document.createElement('div'); + el.className = 'oneEvent'; + el.setAttribute('id', 'oneEvent' + i); + el.textContent = hash[i].start + "-" + hash[i].end + " " + hash[i].name; + spisok.appendChild(el); + document.getElementById('oneEvent' + i).addEventListener('click', content, true); + } +} + +// функции фильтрации +function str2date(s) { + "use strict"; + var dateParts = s.split('.'); + if (typeof dateParts[2] === 'string') { + return new Date(dateParts[2], dateParts[1], dateParts[0]); + } + if (typeof dateParts[2] === 'undefined') { + dateParts = s.split('-'); + return new Date(dateParts[0], dateParts[1], dateParts[2]); + } +} +function FilterToDate(collection, flag) { + "use strict"; + var result; + if (flag === -1) { + result = collection.filter(function (collection) { + var s = str2date(collection.start); + return s < new Date(); + }); + } else { + result = collection.filter(function (collection) { + var s = str2date(collection.start); + return s >= new Date(); + }); + } + return result; +} +function FilterToParty(collection, flag) { + "use strict"; + var result; + if (flag === -1) { + result = collection.filter(function (collection) { + return collection.party === "нет"; + }); + } else { + result = collection.filter(function (collection) { + return collection.party === "да"; + }); + } + return result; +} +function SortToDate(collection) { + "use strict"; + collection.sort(function (a, b) { + return str2date(a.start) > str2date(b.start) ? 1 : -1; + }); + return collection; +} + diff --git a/current-event.json b/current-event.json deleted file mode 100644 index eecedbc..0000000 --- a/current-event.json +++ /dev/null @@ -1 +0,0 @@ -[{"name":"pewpew","start":"2012-11-07T10:18:39.207Z","end":"2012-11-07T10:18:39.207Z"}] \ No newline at end of file diff --git a/form.html b/form.html new file mode 100644 index 0000000..c51435e --- /dev/null +++ b/form.html @@ -0,0 +1,81 @@ + + + + Вывод информации о событиях + + + + + + + + + +
+
Создать новое событие
+
+ + + + + + + + + + +
+
+
+
+ + + + +
+
+
+
+ + \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index 3cd1e5c..0000000 --- a/index.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - -Loading... - - - \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..851ad59 --- /dev/null +++ b/style.css @@ -0,0 +1,36 @@ +.l-level{ + width: 500px; + // margin: 0 auto; + padding: 20px; + font: italic 14px Georgia; + float: right; + } + .t-block{ + font-size: 1.3em; + font-style: italic; + } + .meeting{ position: relative; width: 500px; padding: 10px;} + .meeting label {display: block; padding-bottom: 10px; overflow: hidden;} + .meeting .info {display: block; width: 494px; margin: 10px 0 10px 0;} + .meeting input { float: right;} + .party label, .organize label{ + padding-left: 30px; + } + .b-submit { width: 300px; margin: 10px 100px 10px 100px;} + .party {overflow: hidden;} + .meeting select {margin: 0 10px;} + .l-spisok { + padding: 20px; + // width: 200px; + float: left; + } + .l-spisok form{ + padding-bottom: 10px; + } + .oneEvent, .oneEventCollection { + margin-bottom: 10px; + background-color: blue; + padding: 5px 10px; + } + .contekst {overflow:hidden; padding-top: 20px;} + .contekst label {display: block;} \ No newline at end of file diff --git a/test.json b/test.json new file mode 100644 index 0000000..a7552b8 --- /dev/null +++ b/test.json @@ -0,0 +1 @@ +[{"name": "Pewpe", "start": "11.12.2012", "end": "13.12.2012"}] \ No newline at end of file