Built motion from commit 943eae279.|1.0.24
[motion.git] / public / bower_components / Sortable / knockout-sortable.js
1 \feff(function (factory) {
2     "use strict";
3     if (typeof define === "function" && define.amd) {
4         // AMD anonymous module
5         define(["knockout"], factory);
6     } else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
7         // CommonJS module
8         var ko = require("knockout");
9         factory(ko);
10     } else {
11         // No module loader (plain <script> tag) - put directly in global namespace
12         factory(window.ko);
13     }
14 })(function (ko) {
15     "use strict";
16
17     var init = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) {
18
19         var options = buildOptions(valueAccessor, sortableOptions);
20
21         //It's seems that we cannot update the eventhandlers after we've created the sortable, so define them in init instead of update
22         ['onStart', 'onEnd', 'onRemove', 'onAdd', 'onUpdate', 'onSort', 'onFilter'].forEach(function (e) {
23             if (options[e] || eventHandlers[e])
24                 options[e] = function (eventType, parentVM, parentBindings, handler, e) {
25                     var itemVM = ko.dataFor(e.item),
26                         //All of the bindings on the parent element
27                         bindings = ko.utils.peekObservable(parentBindings()),
28                         //The binding options for the draggable/sortable binding of the parent element
29                         bindingHandlerBinding = bindings.sortable || bindings.draggable,
30                         //The collection that we should modify
31                         collection = bindingHandlerBinding.collection || bindingHandlerBinding.foreach;
32                     if (handler)
33                         handler(e, itemVM, parentVM, collection, bindings);
34                     if (eventHandlers[eventType])
35                         eventHandlers[eventType](e, itemVM, parentVM, collection, bindings);
36                 }.bind(undefined, e, viewModel, allBindings, options[e]);
37         });
38
39         var sortableElement = Sortable.create(element, options);
40
41         //Destroy the sortable if knockout disposes the element it's connected to
42         ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
43             sortableElement.destroy();
44         });
45         return ko.bindingHandlers.template.init(element, valueAccessor);
46     },
47     update = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) {
48
49         //There seems to be some problems with updating the options of a sortable
50         //Tested to change eventhandlers and the group options without any luck
51
52         return ko.bindingHandlers.template.update(element, valueAccessor, allBindings, viewModel, bindingContext);
53     },
54     eventHandlers = (function (handlers) {
55
56         var moveOperations = [],
57             tryMoveOperation = function (e, itemVM, parentVM, collection, parentBindings) {
58                 //A move operation is the combination of a add and remove event, this is to make sure that we have both the target and origin collections
59                 var currentOperation = { event: e, itemVM: itemVM, parentVM: parentVM, collection: collection, parentBindings: parentBindings },
60                     existingOperation = moveOperations.filter(function (op) {
61                         return op.itemVM === currentOperation.itemVM;
62                     })[0];
63
64                 if (!existingOperation) {
65                     moveOperations.push(currentOperation);
66                 }
67                 else {
68                     //We're finishing the operation and already have a handle on the operation item meaning that it's safe to remove it
69                     moveOperations.splice(moveOperations.indexOf(existingOperation), 1);
70
71                     var removeOperation = currentOperation.event.type === 'remove' ? currentOperation : existingOperation,
72                         addOperation = currentOperation.event.type === 'add' ? currentOperation : existingOperation;
73
74                     moveItem(itemVM, removeOperation.collection, addOperation.collection, addOperation.event.clone, addOperation.event);
75                 }
76             },
77             //Moves an item from the to (collection to from (collection), these can be references to the same collection which means it's a sort,
78             //clone indicates if we should move or copy the item into the new collection
79             moveItem = function (itemVM, from, to, clone, e) {
80                 //Unwrapping this allows us to manipulate the actual array
81                 var fromArray = from(),
82                     //It's not certain that the items actual index is the same as the index reported by sortable due to filtering etc.
83                     originalIndex = fromArray.indexOf(itemVM),
84                     newIndex = e.newIndex;
85
86                 if (e.item.previousElementSibling)
87                 {
88                     newIndex = fromArray.indexOf(ko.dataFor(e.item.previousElementSibling));
89                     if (originalIndex > newIndex)
90                         newIndex = newIndex + 1;
91                 }                
92
93                 //Remove sortables "unbound" element
94                 e.item.parentNode.removeChild(e.item);
95
96                 //This splice is necessary for both clone and move/sort
97                 //In sort/move since it shouldn't be at this index/in this array anymore
98                 //In clone since we have to work around knockouts valuHasMutated when manipulating arrays and avoid a "unbound" item added by sortable
99                 fromArray.splice(originalIndex, 1);
100                 //Update the array, this will also remove sortables "unbound" clone
101                 from.valueHasMutated();
102                 if (clone && from !== to) {
103                     //Readd the item
104                     fromArray.splice(originalIndex, 0, itemVM);
105                     //Force knockout to update
106                     from.valueHasMutated();
107                 }
108                 //Insert the item on its new position
109                 to().splice(newIndex, 0, itemVM);
110                 //Make sure to tell knockout that we've modified the actual array.
111                 to.valueHasMutated();
112             };
113
114         handlers.onRemove = tryMoveOperation;
115         handlers.onAdd = tryMoveOperation;
116         handlers.onUpdate = function (e, itemVM, parentVM, collection, parentBindings) {
117             //This will be performed as a sort since the to/from collections reference the same collection and clone is set to false
118             moveItem(itemVM, collection, collection, false, e);
119         };
120
121         return handlers;
122     })({}),
123     //bindingOptions are the options set in the "data-bind" attribute in the ui.
124     //options are custom options, for instance draggable/sortable specific options
125     buildOptions = function (bindingOptions, options) {
126         //deep clone/copy of properties from the "from" argument onto the "into" argument and returns the modified "into"
127         var merge = function (into, from) {
128             for (var prop in from) {
129                 if (Object.prototype.toString.call(from[prop]) === '[object Object]') {
130                     if (Object.prototype.toString.call(into[prop]) !== '[object Object]') {
131                         into[prop] = {};
132                     }
133                     into[prop] = merge(into[prop], from[prop]);
134                 }
135                 else
136                     into[prop] = from[prop];
137             }
138
139             return into;
140         },
141         //unwrap the supplied options
142         unwrappedOptions = ko.utils.peekObservable(bindingOptions()).options || {};
143
144         //Make sure that we don't modify the provided settings object
145         options = merge({}, options);
146
147         //group is handled differently since we should both allow to change a draggable to a sortable (and vice versa),
148         //but still be able to set a name on a draggable without it becoming a drop target.
149         if (unwrappedOptions.group && Object.prototype.toString.call(unwrappedOptions.group) !== '[object Object]') {
150             //group property is a name string declaration, convert to object.
151             unwrappedOptions.group = { name: unwrappedOptions.group };
152         }
153
154         return merge(options, unwrappedOptions);
155     };
156
157     ko.bindingHandlers.draggable = {
158         sortableOptions: {
159             group: { pull: 'clone', put: false },
160             sort: false
161         },
162         init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
163             return init(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.draggable.sortableOptions);
164         },
165         update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
166             return update(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.draggable.sortableOptions);
167         }
168     };
169
170     ko.bindingHandlers.sortable = {
171         sortableOptions: {
172             group: { pull: true, put: true }
173         },
174         init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
175             return init(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.sortable.sortableOptions);
176         },
177         update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
178             return update(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.sortable.sortableOptions);
179         }
180     };
181
182 });