File: connectedLists.html

package info (click to toggle)
node-knockout-sortable 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 248 kB
  • sloc: javascript: 1,530; makefile: 5
file content (181 lines) | stat: -rw-r--r-- 5,285 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Draggable, droppable, and sortable list (2.0)</title>

    <script type='text/javascript' src='../ext/jquery-1.9.1.js'></script>
    <script type="text/javascript" src="../ext/jquery-ui.js"></script>

    <script type='text/javascript' src="../ext/knockout-3.5.1.js"></script>
    <script type='text/javascript' src="../build/knockout-sortable.min.js"></script>

    <style type='text/css'>
        body {
            font-family: arial;
        }

        h3 {
            font-weight: bold;
            text-align: center;
        }

        div {
            padding: 5px;
            margin: 5px;
            border: black 1px solid;
        }

        p, a {
            font-size: .8em;
        }

        ul {
            padding-bottom: 10px;
        }

        .ko_container {
            width: 125px;
            min-height: 50px;
            background-color: #AAA;
        }

        .high {
            background-color: red;
        }

        .trash {
            background-color: #000;
        }

        .item {
            background-color: #DDD;
            cursor: move;
            text-align: center;
        }

        .item input {
            width: 100px;
        }

        #main {
            float: left;
            width: 150px;
            margin-top: 0px;
        }

        #results {
            margin-left: 175px;
            width: 150px;
        }
    </style>
</head>
<body>
    <div id="main">
        <h3>High Priority</h3>

        <div class="high"
             data-bind="sortable: { template: 'taskTmpl', data: highPriorityTasks, afterMove: myDropCallback }"></div>

        <h3>Normal Priority</h3>

        <div data-bind="sortable: { template: 'taskTmpl', data: normalPriorityTasks, afterMove: myDropCallback }"></div>

        <a href="#" data-bind="click: addTask">Add Task</a>
        <hr/>
        <h3>Trash</h3>

        <div class="trash" data-bind="sortable: trash"><p></p></div>

        <script id="taskTmpl" type="text/html">
            <div class="item">
                 <span data-bind="visible: !$root.isTaskSelected($data)">
                    <a href="#" data-bind="text: name, click: $root.selectedTask"></a>
                </span>
                <span data-bind="visibleAndSelect: $root.isTaskSelected($data)">
                    <input data-bind="value: name, event: { blur: $root.clearTask }" />
                </span>
            </div>
        </script>
    </div>

    <div id="results">
        <h3>High Priority</h3>
        <ul data-bind="foreach: highPriorityTasks">
            <li data-bind="text: name"></li>
        </ul>
        <h3>Normal Priority</h3>
        <ul data-bind="foreach: normalPriorityTasks">
            <li data-bind="text: name"></li>
        </ul>
    </div>​

    <script type='text/javascript'>
        //control visibility, give element focus, and select the contents (in order)
        ko.bindingHandlers.visibleAndSelect = {
            update:function (element, valueAccessor) {
                ko.bindingHandlers.visible.update(element, valueAccessor);
                if (valueAccessor()) {
                    setTimeout(function () {
                        $(element).find("input").focus().select();
                    }, 0); //new tasks are not in DOM yet
                }
            }
        };

        var Task = function (name) {
            this.name = ko.observable(name);
        }

        var ViewModel = function () {
            var self = this;

            self.highPriorityTasks = ko.observableArray([
                new Task("Get dog food"),
                new Task("Mow lawn"),
                new Task("Fix car")
            ]);
            self.highPriorityTasks.id = "high";

            self.normalPriorityTasks = ko.observableArray([
                new Task("Fix fence"),
                new Task("Walk dog"),
                new Task("Read book")
            ]);
            self.normalPriorityTasks.id = "normal";

            self.selectedTask = ko.observable();
            self.clearTask = function(data, event) {
                if (data === self.selectedTask()) {
                    self.selectedTask(null);
                }

                if (data.name() === "") {
                   self.highPriorityTasks.remove(data);
                   self.normalPriorityTasks.remove(data);
                }
            };

            self.isTaskSelected = function(task) {
               return task === self.selectedTask();
            };

            self.addTask = function () {
                var task = new Task("new");
                self.selectedTask(task);
                self.normalPriorityTasks.push(task);
            };
            self.trash = ko.observableArray([]);
            self.trash.id = "trash";

            self.myDropCallback = function (arg) {
                if (console) {
                    console.log("Moved '" + arg.item.name() + "' from " + arg.sourceParent.id + " (index: " + arg.sourceIndex + ") to " + arg.targetParent.id + " (index " + arg.targetIndex + ")");
                }
            };
        };

        ko.applyBindings(new ViewModel());
    </script>
</body>
</html>