forked from seagullua/JS-BuyList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (96 loc) · 3.6 KB
/
main.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
$(function (){
var $list = $(".bl-list");
var ONE_ROW_HTML = $(".row-template").html();
var $left = $(".left-products");
var ONE_PRODUCT_HTML = $(".left-product-template").html();
var $bought = $(".bought-products");
var ONE_BOUGHT_HTML = $(".bought-product-template").html();
function addItem(title){
var $node = $(ONE_ROW_HTML);
var $node_left = $(ONE_PRODUCT_HTML);
var $node_bought = $(ONE_BOUGHT_HTML);
var canChange = true;
var amount = 1;
var $amount_label = $node.find(".bl-label");
$amount_label.text(amount);
var $amount_labelleft = $node_left.find(".bl-amount");
$amount_labelleft.text(amount);
var $amount_labelbought = $node_bought.find(".bl-bought-amount");
$amount_labelbought.text(amount);
$node.find(".bl-product").text(title);
$node_left.find(".bl-name").text(title);
$node_bought.find(".bl-bought-name").text(title);
$node.find(".bl-plus").click(function () {
amount += 1;
$amount_label.text(amount);
$amount_labelleft.text(amount);
$amount_labelbought.text(amount);
$node.find(".bl-minus").prop("disabled", false).css('opacity',1);
});
if (amount === 1){
$node.find(".bl-minus").prop("disabled", true).css('opacity',0.5);
}
$node.find(".bl-minus").click(function() {
if(amount > 1){
amount -= 1;
$amount_label.text(amount);
$amount_labelleft.text(amount);
$amount_labelbought.text(amount);
}
if (amount === 1){
$node.find(".bl-minus").prop("disabled", true).css('opacity',0.5);
}
});
$node.find(".bl-product").click(function(){
if(canChange){
$node.find(".bl-product").hide();
$node.find(".edit-product").show().focus();
$node.find(".edit-product").val(title);
$node_left.find(".bl-name").val(title);
$node_bought.find(".bl-bought-name").val(title);
}
});
$node.find(".edit-product").focusout(function(){
if(canChange){
$node.find(".bl-product").show();
$node.find(".edit-product").hide();
title = $node.find(".edit-product").val();
$node.find(".bl-product").text(title);
$node_left.find(".bl-name").text(title);
$node_bought.find(".bl-bought-name").text(title);
}
});
$node.find(".bl-deletebutton").click(function() {
$node.hide();
$node_left.hide();
$node_bought.hide();
});
$node.find(".bl-boughtbutton").click(function() {
$node.addClass("is-bought");
$node_left.hide();
$bought.append($node_bought);
$node_bought.show();
canChange = false;
});
$node.find(".bl-notboughtbutton").click(function() {
$node.removeClass("is-bought");
$node_left.show();
$node_bought.hide();
canChange = true;
});
$list.append($node);
$left.append($node_left);
}
addItem("Джекфрут");
addItem("Апельсин");
addItem("Карамбола");
var $input = $(".bl-user");
$(".bl-add").click(function(){
var new_product = $input.val();
if(new_product.trim()){
addItem(new_product);
$input.val("");
$input.focus();
}
});
});