1
|
$(function() {
|
2
|
// serialize nestable list
|
3
|
altair_nestable.init();
|
4
|
});
|
5
|
|
6
|
altair_nestable = {
|
7
|
init: function() {
|
8
|
|
9
|
var $nestable = $('#nestable');
|
10
|
if($nestable.length) {
|
11
|
|
12
|
// loop through nestable objects
|
13
|
function loop_nestable(obj, ul) {
|
14
|
$.each(obj, function(key, val) {
|
15
|
|
16
|
var content = $nestable.find('[data-id='+ val.id +']').html();
|
17
|
|
18
|
if(val && typeof val === "object") {
|
19
|
var li = $("<li class='uk-nestable-item' data-id='"+ val.id +"'>" + content + "</li>").appendTo(ul);
|
20
|
|
21
|
if(val.children) {
|
22
|
var ul2 = $("<ul>").appendTo(li);
|
23
|
loop_nestable(val.children, ul2);
|
24
|
}
|
25
|
} else {
|
26
|
$("<li class='uk-nestable-item' data-id='"+ val.id +"'>" + content + "</li>").appendTo(ul);
|
27
|
}
|
28
|
|
29
|
});
|
30
|
}
|
31
|
|
32
|
// check if localStorage is supported
|
33
|
if (lsTest()) {
|
34
|
if (localStorage.getItem("nestable_items") !== null) {
|
35
|
// get values from localStorage
|
36
|
var nestable_array = localStorage.getItem("nestable_items");
|
37
|
// clone main nestable list
|
38
|
var $nestable_cloned = $nestable.clone().empty();
|
39
|
// loop through localStorage objects to sort list
|
40
|
loop_nestable(JSON.parse(nestable_array),$nestable_cloned);
|
41
|
|
42
|
// replace original nestable list items with sorted list items
|
43
|
$nestable
|
44
|
.html($nestable_cloned.html())
|
45
|
.after('<a class="md-btn" href="#" id="restore_nestable">Restore</a>');
|
46
|
|
47
|
// restore original order on button click
|
48
|
$('#restore_nestable').click(function() {
|
49
|
localStorage.removeItem('nestable_items');
|
50
|
location.reload(true);
|
51
|
})
|
52
|
|
53
|
}
|
54
|
}
|
55
|
|
56
|
// manually init nestable
|
57
|
var nestable = UIkit.nestable($nestable, {});
|
58
|
|
59
|
// serialize nestable on change
|
60
|
$nestable.on('change.uk.nestable',function() {
|
61
|
var serialized_data = $nestable.data("nestable").serialize();
|
62
|
// check if localStorage is supported
|
63
|
if (lsTest()) {
|
64
|
if( (localStorage.length === 0) || (localStorage.getItem("nestable_items") != JSON.stringify(serialized_data)) ) {
|
65
|
localStorage.setItem("nestable_items", JSON.stringify(serialized_data));
|
66
|
}
|
67
|
}
|
68
|
});
|
69
|
|
70
|
}
|
71
|
}
|
72
|
};
|