<div>
<p><input class="check" id="check-option-1" name="name" type="checkbox">
<label for="check-option-1"></label>
</p>
<p class="copy">1</p>
<p class="copy-2">1.1</p>
</div>
<div>
<p><input class="check" id="check-option-2" name="name" type="checkbox">
<label for="check-option-2"></label>
</p>
<p class="copy">2</p>
<p class="copy-2">2.1</p>
</div>
<div class="preview"><i class="fa fa-share" aria-hidden="true"></i></div>
<div class="modal">Здесь ничего нет</div>
* {
box-sizing: border-box;
}
div p {
display: inline-block;
vertical-align: middle;
}
input[type="checkbox"] {
display: none;
margin: 0;
}
label {
position: relative;
display: inline-block;
}
label, .preview {
user-select: none;
}
input[type="checkbox"] + label:before {
content: "";
width: 20px;
height: 20px;
display: inline-block;
margin: 5px 5px 0 0;
border: 1px solid #808080;
}
input[type="checkbox"] + label:after {
content: "";
position: absolute;
top: 11px;
left: 6px;
width: 10px;
height: 10px;
display: inline-block;
border-radius: 50%;
background-color: #808080;
opacity: 0;
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
}
.modal {
position: absolute;
left: -410px;
width: 400px;
height: 400px;
border: 1px solid #000;
background-color: #fff;
transition: 0.6s ease;
}
.modal-show {
left: 75px;
}
.preview {
display: inline-block;
}
.preview i {
position: relative;
width: 50px;
height: 50px;
border-radius: 2px;
background-color: gray;
text-align: center;
line-height: 50px;
color: #fff;
font-size: 30px;
cursor: pointer;
}
class Stogage {
constructor(name){
this.name = name;
this.hash = {};
let text = localStorage.getItem(this.name);
if(text)
this.hash = JSON.parse(text);
this.save();
}
get(id){
return this.item.find(item=>item.id===id)
}
set(id, data){
if(!data)
return this.del(id);
this.hash[id] = data;
this.save();
}
del(id){
delete this.hash[id];
this.save();
}
save(){
this.list = Object.values(this.hash);
const text = JSON.stringify(this.hash);
localStorage.setItem(this.name, text);
}
}
const cart = new Stogage('local');
const modal = $('.modal');
function reDrawModal(list){
let html = "";
list.forEach(item=>{
html += '<div class="item">';
html += item.list.map(element=>{
return '<'+element.tag+' class="'+element.class+'">'
+element.text
+'</'+element.tag+'>';
}).join(' ');
html += '</div>';
});
if(html){
modal.addClass('modal-show').html(html);
}else{
modal.removeClass('modal-show').text('Здесь ничего нет');;
}
}
reDrawModal(cart.list);
cart.list.forEach(item=>{
$('#' + item.id).prop('checked', true);
});
$('body').on('change', '.check', function(){
let id = $(this).attr('id');
if ($(this).prop('checked')) {
const data = {
id: id,
list: []
};
const elements = $(this)
.parent()
.siblings('.copy, .copy-2');
for(var i=0; i<elements.length; i++ ){
const item = elements[i];
console.log("item:", item);
data.list.push({
tag: item.tagName,
class: item.className,
text: item.innerText
});
}
console.log("data:",data);
cart.set(id, data);
} else {
cart.del(id);
}
reDrawModal(cart.list);
});
$('.preview').click(function(){
$(".modal").toggleClass("modal-show");
});