-
Notifications
You must be signed in to change notification settings - Fork 9
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
Дз5 #9
base: master
Are you sure you want to change the base?
Дз5 #9
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Календарь</title> | ||
<link rel="stylesheet" type="text/css" href="choose.css"> | ||
<script type="text/javascript" src="Model.js"></script> | ||
<script type="text/javascript" src="Collection.js"></script> | ||
<script type="text/javascript" src="Event.js"></script> | ||
<script type="text/javascript" src="Events.js"></script> | ||
<script type="text/javascript" src="CreateCalendar.js"></script> | ||
</head> | ||
|
||
<body> | ||
<div> | ||
<h3>Фильтрация текущей коллекции </h3> | ||
<div> | ||
<label><input type="checkbox" class="filter1"/>Выбрать последущие события</label> | ||
<label><input type="checkbox" class="filter2"/>Выбрать предыдущие события</label> | ||
<div>Сортировать по</div> | ||
<label><input type="radio" name = "sort" value = "start"/>Началу</label> | ||
<label><input type="radio" name = "sort" value = "length"/>Длительности</label> | ||
<label><input type="radio" name = "sort" value = "rating"/>Рейтингу</label> | ||
</div> | ||
</div> | ||
<hr/> | ||
<form name = "Создание события"> | ||
|
||
<label>Название события | ||
<input type="text" value="Новое событие" name = "Новое событие" autofocus/> | ||
</label> | ||
<br> | ||
<label>Дата начала: | ||
<input type="date" name = "Дата начала"/> | ||
</label> | ||
<br> | ||
<label>Время начала: | ||
<input type="time" name = "Время начала" /> | ||
</label> | ||
<br> | ||
<label>Дата окончания: | ||
<input type="date" name = "Дата окончания" /> | ||
</label> | ||
<br> | ||
<label>Время окончания: | ||
<input type="time" name = "Время окончания" /> | ||
</label> | ||
<br> | ||
<label>Рейтинг | ||
<select name = "Рейтинг"> | ||
<option>1 *</option> | ||
<option>2 *</option> | ||
<option>3 *</option> | ||
<option>4 *</option> | ||
<option>5 *</option> | ||
</select> | ||
</label> | ||
<br> | ||
<label>Место | ||
<input type="text" name = "Место"/> | ||
</label> | ||
<br> | ||
<label>Комментарий | ||
<textarea name = "Комментарий"></textarea> | ||
</label> | ||
<br> | ||
<label>Ссылка | ||
<input type="url" name = "Ссылка"/> | ||
</label> | ||
<br> | ||
<input type="button" value="Добавить событие" onclick="CreateCalendar()"/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Крайне не желательно объявлять обработчики в HTML |
||
</form> | ||
<hr/> | ||
<h3> Текущее состояние коллекции </h3> | ||
|
||
</body> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*jslint plusplus: true, browser: true, devel: true */ | ||
// абстрактная коллекция Collection, представляющая из себя набор объектов Model | ||
/** | ||
* @constructor | ||
* @param {Object} items | ||
**/ | ||
var Collection = function (items) { | ||
"use strict"; | ||
this.items = []; | ||
var keyName; | ||
for (keyName in items) { | ||
if (items.hasOwnProperty(keyName)) { | ||
this.items.push(items[keyName]); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* @param {Model} model | ||
* | ||
* @return {Collection} | ||
*/ | ||
Collection.prototype.add = function (model) { | ||
"use strict"; | ||
this.items.push(model); | ||
}; | ||
|
||
/** | ||
* @param {Function} selector | ||
* | ||
* @example | ||
* new Collection().filter(function (item) { | ||
* return item.get('attendee').indexOf("me") !== -1; | ||
* }); | ||
* @return {Collection} | ||
*/ | ||
Collection.prototype.filter = function (selector) { | ||
"use strict"; | ||
return new Collection(this.items.filter(selector)); | ||
}; | ||
/** | ||
* @param {String} fieldName параметр, по которому происходит сотрировка | ||
* @return {Collection} | ||
*/ | ||
Collection.prototype.sortBy = function (fieldName) { | ||
"use strict"; | ||
if (fieldName === "start") { // сортировка по началу события | ||
return new Collection(this.items.sort(function (Event1, Event2) {return Event1.start - Event2.start; })); | ||
} | ||
if (fieldName === "length") {//сортировка по длине события | ||
return new Collection(this.items.sort(function (Event1, Event2) {return (Event1.end - Event1.start) - (Event2.end - Event2.start); })); | ||
} | ||
if (fieldName === "rating") {//сортировка по рейтингу события | ||
return new Collection((this.items.sort(function (Event1, Event2) {return Event1.rating - Event2.rating; })).reverse()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это не читается :) var items = this.items.sort(function (Event1, Event2) {
return Event1.rating - Event2.rating;
}).reverse();
return new Collection(items); |
||
} | ||
if (fieldName === "") { | ||
return new Collection(this.items); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Табуляция поехала |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*jslint plusplus: true, browser: true, devel: true */ | ||
var newEvents = new Events(); | ||
function CreateCalendar() { | ||
"use strict"; | ||
var s = (document.getElementsByName("Дата начала")[0]).value,// строка даты | ||
st = (document.getElementsByName("Время начала")[0]).value, // строка времени | ||
startEv = s + " " + st + ":00", | ||
endEv = s + " " + st + ":00", | ||
element, | ||
filterEvents = newEvents, | ||
filter1, | ||
filter2, | ||
bool, | ||
res; | ||
|
||
s = (document.getElementsByName("Дата окончания")[0]).value; | ||
st = (document.getElementsByName("Время окончания")[0]).value; | ||
|
||
element = new Event({ | ||
start: new Date(startEv), | ||
end: new Date(endEv), | ||
name: (document.getElementsByName("Новое событие")[0]).value, | ||
place: (document.getElementsByName("Место")[0]).value, | ||
rating: parseFloat((document.getElementsByName("Рейтинг")[0]).value[0]), | ||
comment: (document.getElementsByName("Комментарий")[0]).value, | ||
link: (document.getElementsByName("Ссылка")[0]).value | ||
}); | ||
element.validate(); | ||
newEvents.add(element); | ||
|
||
filter1 = document.querySelector('input[class="filter1"]:checked'); | ||
filter2 = document.querySelector('input[class="filter2"]:checked'); | ||
bool = false; // флажок. если =true, выдаем отфильтрованную коллекцию. иначе - всю. | ||
if (filter1 !== null && filter1.value === "on") { | ||
filterEvents = newEvents.findFutureEvents(); | ||
bool = !bool; | ||
} | ||
if (filter2 !== null && filter2.value === "on") { | ||
filterEvents = newEvents.findPastEvents(); | ||
bool = !bool; | ||
} | ||
|
||
s = ""; | ||
if (document.querySelector('input[name="sort"]:checked') !== null) { | ||
s = document.querySelector('input[name="sort"]:checked').value; | ||
} | ||
|
||
if (bool) { | ||
res = new Events(filterEvents.sortBy(s).items); | ||
} else { | ||
res = new Events(newEvents.sortBy(s).items); | ||
} | ||
res.write(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/*jslint plusplus: true, browser: true, devel: true */ | ||
|
||
function datatype(data) {// возвращает true, если data имеет тип дата и она корректна | ||
"use strict"; | ||
if (typeof data === 'undefined') { | ||
return false; | ||
} | ||
if (!data.getTime) { | ||
return false; | ||
} | ||
if ('Invalid Date' === data) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function ratingtype(rating) {// возвращает true, если rating - число от 0 до 5 | ||
"use strict"; | ||
if (typeof rating !== 'number') { | ||
return false; | ||
} | ||
if (rating > 5 || rating < 0) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function inherits(Constructor, SuperConstructor) { | ||
"use strict"; | ||
var F = function () {}; | ||
|
||
F.prototype = SuperConstructor.prototype; | ||
|
||
Constructor.prototype = new F(); | ||
} | ||
|
||
|
||
// наследуем от Абстракнтого конструктора Model объект Event | ||
var Event = function (data) { | ||
"use strict"; | ||
Model.apply(this, arguments); | ||
}; | ||
inherits(Event, Model); | ||
|
||
Event.prototype.validate = function () {//проверяет корректность переданных данных. | ||
"use strict"; | ||
if (!datatype(this.start)) { | ||
throw new Error(this.start + " не является датой!"); | ||
} | ||
if (!datatype(this.end)) { | ||
throw new Error(this.end + " не является датой!"); | ||
} | ||
if (this.start.getTime() - this.end.getTime() > 0) { | ||
throw new Error("некорректное событие: не может закончиться раньше, чем начаться!!!"); | ||
} | ||
if (!ratingtype(this.rating)) { | ||
throw new Error("введите рейтинг от 0 до 5"); | ||
} | ||
}; | ||
|
||
Event.prototype.createSection = function () { | ||
"use strict"; | ||
var el, line, clone1, clone2, clone3, clone4, clone5, clone6, clone7; | ||
el = document.createElement('section'); | ||
line = document.createElement('p'); | ||
line.textContent = "Событие: " + this.name; | ||
el.appendChild(line); | ||
|
||
clone1 = line.cloneNode(true); | ||
clone1.textContent = "Начало: " + this.start; | ||
el.appendChild(clone1); | ||
|
||
clone2 = line.cloneNode(true); | ||
clone2.textContent = "Конец: " + this.end; | ||
el.appendChild(clone2); | ||
|
||
clone3 = line.cloneNode(true); | ||
clone3.textContent = "Продолжительность: " + hours(this.end - this.start); | ||
el.appendChild(clone3); | ||
|
||
clone4 = line.cloneNode(true); | ||
clone4.textContent = "Рейтинг: " + this.rating; | ||
el.appendChild(clone4); | ||
|
||
if (this.place !== '') { | ||
clone5 = line.cloneNode(true); | ||
clone5.textContent = "Место: " + this.place; | ||
el.appendChild(clone5); | ||
} | ||
|
||
if (this.comment !== '') { | ||
clone6 = line.cloneNode(true); | ||
clone6.textContent = "Комментарий: " + this.comment; | ||
el.appendChild(clone6); | ||
} | ||
|
||
if (this.link !== '') { | ||
clone7 = line.cloneNode(true); | ||
clone7.textContent = "Ссылка: " + this.link; | ||
el.appendChild(clone7); | ||
} | ||
el.appendChild(document.createElement('br')); | ||
return el; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/*jslint plusplus: true, browser: true, devel: true */ | ||
|
||
|
||
function getRandomInt(min, max) {//Случайное целое число между min и max | ||
"use strict"; | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
|
||
var today = new Date(); | ||
|
||
function addDay(n) {// прибавляет к текущей дате n дней | ||
"use strict"; | ||
return new Date(today.getTime() + (86400000 * n)); | ||
} | ||
function nDay(n) {// возвращает n-ый день от текущей даты в 00часов 00минут | ||
"use strict"; | ||
var Day, a, b; | ||
Day = addDay(n); | ||
a = Day.getTime(); | ||
b = Day.getMilliseconds() + (Day.getHours() * 3600000) + (Day.getMinutes() * 60000) + (Day.getSeconds() * 1000); | ||
return new Date(a - b); | ||
} | ||
function hours(milis) {// переводит миллисекунда в часы. Возвращает строку | ||
"use strict"; | ||
var hour, minute, s; | ||
hour = Math.floor(milis / 3600000); | ||
minute = Math.floor((milis - (hour * 3600000)) / 60000); | ||
s = hour + "ч " + minute + "мин"; | ||
return s; | ||
} | ||
var week = addDay(7); | ||
var maxdate = addDay(31); | ||
var mindate = addDay(-31); | ||
|
||
|
||
// наследуем коллекцию событий Events от абстрактной коллекции Collection | ||
var Events = function (items) { | ||
"use strict"; | ||
Collection.apply(this, arguments); | ||
}; | ||
inherits(Events, Collection); | ||
|
||
Events.prototype.write = function () {//выводит на экран все элементы коллекции | ||
"use strict"; | ||
/** | ||
var el = document.getElementsByTagName('section'); | ||
if (el.length === 1) { | ||
document.body.removeChild(el); | ||
} | ||
**/ | ||
var fragment = document.createDocumentFragment('section'); // фрагмент новых элементов | ||
|
||
this.items.forEach(function (NewEvent) { | ||
fragment.appendChild(NewEvent.createSection()); | ||
}); | ||
document.body.appendChild(fragment); | ||
}; | ||
|
||
/** | ||
* показывает все будующие события | ||
* @return {Events} | ||
*/ | ||
Events.prototype.findFutureEvents = function () { | ||
"use strict"; | ||
return new Events((this.filter(function (NewEvent) {return (NewEvent.start > today); })).items); | ||
}; | ||
/** | ||
* показывает все прошедшие события | ||
* @return {Events} | ||
*/ | ||
Events.prototype.findPastEvents = function () { | ||
"use strict"; | ||
return new Events((this.filter(function (NewEvent) {return (NewEvent.start < today); })).items); | ||
}; | ||
/** | ||
* сортировка по началу события | ||
* @return {Events} | ||
*/ | ||
Events.prototype.sortByStart = function () { | ||
"use strict"; | ||
return new Events((this.sortBy("start")).items); | ||
}; | ||
/** | ||
* сортировка по продолжительности события | ||
* @return {Events} | ||
*/ | ||
Events.prototype.sortByLength = function () { | ||
"use strict"; | ||
return new Events((this.sortBy("length")).items); | ||
}; | ||
/** | ||
* сортировка по рейтингу события | ||
* @return {Events} | ||
*/ | ||
Events.prototype.sortByRating = function () { | ||
"use strict"; | ||
return new Events((this.sortBy("rating")).items); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не желательно использовать не-латинские символы. Лучше -
start_date
Аналогично со всеми остальными