-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.js
141 lines (126 loc) · 4.37 KB
/
backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
$(document).ready(function(){
for(var i = 0; i < localStorage.length; i++){
var object = JSON.parse(localStorage.getItem(localStorage.key(i)));
ideaContent(object);
}
})
// Save Disabled or Enabled
$('.title, .body').on('keyup', function(){
var title = $('.title');
var body = $('.body');
var saveBtn = $('.save-btn');
if(!title.val() || !body.val()) {
saveBtn.prop('disabled', true);
} else {
saveBtn.prop('disabled', false);
}
})
// Creates idea properties
function CreateIdea (title, body, id) {
this.title = title;
this.body = body;
this.id = Date.now();
this.quality = 'swill';
}
function ideaContent(createIdea) {
$(".stored").prepend(
`<article class="card" id="${createIdea.id}">
<p class ="title search-bar" contenteditable>${createIdea.title}</p>
<button class="delete-btn card-btn icon"></button>
<p class="body search-bar" contenteditable>${createIdea.body}</p>
<button class="up card-btn icon"></button>
<button class="down card-btn icon"></button>
<p class="quality">Quality:</p>
<p class="quality-type search-bar">${createIdea.quality}</p>
</article>`)
};
// function ideaContent(createIdea) {
// $(".stored").prepend(
// `<article class="card" id="${createIdea.id}">
// <div class="searchField">
// <h5 class ="title search-bar" contenteditable>${createIdea.title}</h5>
// <button class="delete-btn card-btn icon"></button>
// <p class="body search-bar" contenteditable>${createIdea.body}</p>
// <button class="up card-btn icon"></button>
// <button class="down card-btn icon"></button>
// <p class="quality">Quality:</p>
// <p class="quality-type search-bar">${createIdea.quality}</p>
// </div>
// </article>`)
// };
// Sets idea in motion from user input and btn click
$( ".save-btn" ).click(function() {
var title = $( ".title" ).val();
var body = $( ".body" ).val();
var idea = new CreateIdea(title, body);
ideaContent(idea);
resetInputFields();
localStorage.setItem(idea.id, JSON.stringify(idea));
});
// Stores if User Edits Cards
$('.stored').on('blur', '.title', function(){
var getNewTitle = $(this).closest('.card').find('.title');
var getNewTitleText = getNewTitle.text();
var idKey = $(this).closest('.card').attr('id');
var storeTitle = JSON.parse(localStorage.getItem(idKey));
storeTitle.title = getNewTitleText;
localStorage.setItem(idKey, JSON.stringify(storeTitle));
})
$('.stored').on('blur', '.body', function(){
var getNewBody = $(this).closest('.card').find('.body');
var getNewBodyText = getNewBody.text();
var idKey = $(this).closest('.card').attr('id');
var storeBody = JSON.parse(localStorage.getItem(idKey));
storeBody.body = getNewBodyText;
localStorage.setItem(idKey, JSON.stringify(storeBody));
})
// Delete Button
$('.stored').on('click', '.delete-btn', function(){
$(this).closest('.card').remove();
var idKey = $(this).closest('.card').attr('id');
localStorage.removeItem(idKey);
})
// Qualities
$( '.stored' ).on('click', '.up', function() {
var qualityType = $(this).siblings('.quality-type');
upVote(qualityType);
storeQuality(this, qualityType);
});
$( '.stored' ).on('click', '.down', function() {
var currentQuality = $(this).siblings('.quality-type');
downVote(currentQuality);
storeQuality(this, currentQuality);
});
function storeQuality(card, newQuality) {
var id = $(card).parent().attr('id');
var editStorage = JSON.parse(localStorage.getItem(id));
editStorage.quality = newQuality.text();
localStorage.setItem(id, JSON.stringify(editStorage));
}
function upVote(qualityType) {
if(qualityType.text() === 'swill'){
qualityType.text('plausible');
} else if(qualityType.text() === 'plausible') {
qualityType.text('genius');
}
}
function downVote(qualityType) {
if(qualityType.text() === 'genius'){
qualityType.text('plausible');
} else if(qualityType.text() === 'plausible') {
qualityType.text('swill');
}
}
//resets input fields to placeholder values
function resetInputFields() {
$( ".title" ).val('');
$( ".body" ).val('');
}
// $('.search-input').on('keyup', function(){
// var searchTerm = $(this).val().toLowerCase();
// $('.searchField').each(function (index, element) {
// var text = $(element).text().toLowerCase();
// var match = !!text.match(searchTerm);
// $(this).parent().toggle(match);
// })
// });