Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Чтение данных из файла #7

Open
wants to merge 5 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Collection.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
38 changes: 38 additions & 0 deletions Event.js
Original file line number Diff line number Diff line change
@@ -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;
};
72 changes: 72 additions & 0 deletions Events.js
Original file line number Diff line number Diff line change
@@ -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;
};
38 changes: 38 additions & 0 deletions Model.js
Original file line number Diff line number Diff line change
@@ -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');
};
34 changes: 34 additions & 0 deletions async.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading