forked from dbushell/Nestable
-
Notifications
You must be signed in to change notification settings - Fork 21
/
jquery.nestable++.js
175 lines (126 loc) · 4.36 KB
/
jquery.nestable++.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*jslint browser: true, devel: true, white: true, eqeq: true, plusplus: true, sloppy: true, vars: true*/
/*global $ */
/*************** General ***************/
var updateOutput = function (e) {
var list = e.length ? e : $(e.target),
output = list.data('output');
if (window.JSON) {
if (output) {
output.val(window.JSON.stringify(list.nestable('serialize')));
}
} else {
alert('JSON browser support required for this page.');
}
};
var nestableList = $(".dd.nestable > .dd-list");
/***************************************/
/*************** Delete ***************/
var deleteFromMenuHelper = function (target) {
if (target.data('new') == 1) {
// if it's not yet saved in the database, just remove it from DOM
target.fadeOut(function () {
target.remove();
updateOutput($('.dd.nestable').data('output', $('#json-output')));
});
} else {
// otherwise hide and mark it for deletion
target.appendTo(nestableList); // if children, move to the top level
target.data('deleted', '1');
target.fadeOut();
}
};
var deleteFromMenu = function () {
var targetId = $(this).data('owner-id');
var target = $('[data-id="' + targetId + '"]');
var result = confirm("Delete " + target.data('name') + " and all its subitems ?");
if (!result) {
return;
}
// Remove children (if any)
target.find("li").each(function () {
deleteFromMenuHelper($(this));
});
// Remove parent
deleteFromMenuHelper(target);
// update JSON
updateOutput($('.dd.nestable').data('output', $('#json-output')));
};
/***************************************/
/*************** Edit ***************/
var menuEditor = $("#menu-editor");
var editButton = $("#editButton");
var editInputName = $("#editInputName");
var editInputSlug = $("#editInputSlug");
var currentEditName = $("#currentEditName");
// Prepares and shows the Edit Form
var prepareEdit = function () {
var targetId = $(this).data('owner-id');
var target = $('[data-id="' + targetId + '"]');
editInputName.val(target.data("name"));
editInputSlug.val(target.data("slug"));
currentEditName.html(target.data("name"));
editButton.data("owner-id", target.data("id"));
console.log("[INFO] Editing Menu Item " + editButton.data("owner-id"));
menuEditor.fadeIn();
};
// Edits the Menu item and hides the Edit Form
var editMenuItem = function () {
var targetId = $(this).data('owner-id');
var target = $('[data-id="' + targetId + '"]');
var newName = editInputName.val();
var newSlug = editInputSlug.val();
target.data("name", newName);
target.data("slug", newSlug);
target.find("> .dd-handle").html(newName);
menuEditor.fadeOut();
// update JSON
updateOutput($('.dd.nestable').data('output', $('#json-output')));
};
/***************************************/
/*************** Add ***************/
var newIdCount = 1;
var addToMenu = function () {
var newName = $("#addInputName").val();
var newSlug = $("#addInputSlug").val();
var newId = 'new-' + newIdCount;
nestableList.append(
'<li class="dd-item" ' +
'data-id="' + newId + '" ' +
'data-name="' + newName + '" ' +
'data-slug="' + newSlug + '" ' +
'data-new="1" ' +
'data-deleted="0">' +
'<div class="dd-handle">' + newName + '</div> ' +
'<span class="button-delete btn btn-default btn-xs pull-right" ' +
'data-owner-id="' + newId + '"> ' +
'<i class="fa fa-times-circle-o" aria-hidden="true"></i> ' +
'</span>' +
'<span class="button-edit btn btn-default btn-xs pull-right" ' +
'data-owner-id="' + newId + '">' +
'<i class="fa fa-pencil" aria-hidden="true"></i>' +
'</span>' +
'</li>'
);
newIdCount++;
// update JSON
updateOutput($('.dd.nestable').data('output', $('#json-output')));
// set events
$(".dd.nestable .button-delete").on("click", deleteFromMenu);
$(".dd.nestable .button-edit").on("click", prepareEdit);
};
/***************************************/
$(function () {
// output initial serialised data
updateOutput($('.dd.nestable').data('output', $('#json-output')));
// set onclick events
editButton.on("click", editMenuItem);
$(".dd.nestable .button-delete").on("click", deleteFromMenu);
$(".dd.nestable .button-edit").on("click", prepareEdit);
$("#menu-editor").submit(function (e) {
e.preventDefault();
});
$("#menu-add").submit(function (e) {
e.preventDefault();
addToMenu();
});
});