js todo oop
by Alejandro M
HTML
<div class="to-do"></div>
SCSS
*, :after, :before {
box-sizing: border-box;
}
.to-do {
padding: 3.25em 0 1em;
max-width: 40em;
width: 100%;
margin: 1em auto;
overflow: hidden;
position: relative;
border: 1px solid #e6e6e6;
}
.to-do > .to-do__flash {
position: absolute;
top: 0;
left: 1em;
right: 1em;
}
.to-do__list {
padding: 0;
list-style: none;
}
.to-do__item {
border-top: 1px solid #e6e6e6;
overflow: hidden;
padding: 1em;
opacity: 0;
backface-visibility: hidden;
animation: opa .5s forwards;
}
.to-do__item .to-do__flash {
margin: 0;
}
@keyframes opa {
to {
opacity: 1;
}
}
.to-do__remove-btn {
float: right;
background: rgba(255, 0, 0, 0.25);
margin-left: .5em;
}
.to-do__edit-btn {
float: right;
}
.to-do__controls {
overflow: hidden;
padding: 1em 1em 0 1em;
border-top: 1px solid #e6e6e6;
}
.to-do__submit-btn {
width: 15%;
float: right;
height: 2.75em;
border-left: none;
border-radius: 0 .25em .25em 0 !important;
}
.to-do__input {
width: 85%;
float: left;
height: 2.85em;
padding: .25em;
border: 1px solid silver;
border-right: none;
border-radius:5px 0 0 5px;
}
.to-do__undo-btn {
float: right;
padding: .25em .5em !important;
font-size: .7em !important;
}
.to-do__flash {
overflow: hidden;
}
.alert, .to-do__flash {
padding: .5em;
margin-bottom: 1em;
border: 1px solid transparent;
border-radius: .25em;
}
.alert-success, .to-do__flash--success {
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.alert-info, .to-do__flash--info {
color: #31708f;
background-color: #d9edf7;
border-color: #bce8f1;
}
.alert-warning, .to-do__flash--warning {
color: #8a6d3b;
background-color: #fcf8e3;
border-color: #faebcc;
}
.alert-danger, .to-do__flash--danger {
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
}
.btn, .to-do__remove-btn, .to-do__edit-btn, .to-do__submit-btn, .to-do__undo-btn {
display: inline-block;
padding: 6px 12px;
...
JavaScript
(function (global, $) {
"use strict";
var ToDoList, FlashBag, Database;
// ToDoList constructor
ToDoList = function (conf) {
this.conf = $.extend({
selector: '.to-do',
baseClass: 'to-do',
noDuplicates: true,
displayFlashes: true,
allowUndo: true,
multipleFlashes: false
}, conf || {});
// Doing all the things!
this.initialize();
};
// Initializing the instance
ToDoList.prototype.initialize = function () {
this.wrapper = $(this.conf.selector);
this.classes = {
list: this.conf.baseClass + '__list',
form: this.conf.baseClass + '__controls',
listItem: this.conf.baseClass + '__item',
listItemContent: this.conf.baseClass + '__item-content',
input: this.conf.baseClass + '__input',
buttons: {
remove: this.conf.baseClass + '__remove-btn',
edit: this.conf.baseClass + '__edit-btn',
undo: this.conf.baseClass + '__undo-btn',
submit: this.conf.baseClass + '__submit-btn'
}
}
this.listItemSelector = '.' + this.classes.listItem;
this.editButtonSelector = '.' + this.classes.buttons.edit;
this.isEdit = false;
this.database = new Database({ name: this.conf.baseClass});
this.flashbag = new FlashBag({
baseClass: this.conf.baseClass,
allowUndo: this.conf.allowUndo,
multipleFlashes: this.conf.multipleFlashes,
displayFlashes: this.conf.displayFlashes,
wrapperElement: this.conf.flashWrapperElement,
flashTypes: this.conf.flashTypes
});
this.flashes = {
'entry.new' : {
'type': 'success',
'message': 'New entry added.',
'undo': false
},
'entry.edit' : {
'type': 'warning',
'message': 'Entry edited.',
'undo': true
},
'entry.remove' : {
'type': 'danger',
'message': 'Entry removed.',
'undo': true
},
'entry.exists' : {
'type': 'danger',
'message': 'There...