1 \feff(function (factory) {
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") {
8 var ko = require("knockout");
11 // No module loader (plain <script> tag) - put directly in global namespace
17 var init = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) {
19 var options = buildOptions(valueAccessor, sortableOptions);
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;
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]);
39 var sortableElement = Sortable.create(element, options);
41 //Destroy the sortable if knockout disposes the element it's connected to
42 ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
43 sortableElement.destroy();
45 return ko.bindingHandlers.template.init(element, valueAccessor);
47 update = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) {
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
52 return ko.bindingHandlers.template.update(element, valueAccessor, allBindings, viewModel, bindingContext);
54 eventHandlers = (function (handlers) {
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;
64 if (!existingOperation) {
65 moveOperations.push(currentOperation);
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);
71 var removeOperation = currentOperation.event.type === 'remove' ? currentOperation : existingOperation,
72 addOperation = currentOperation.event.type === 'add' ? currentOperation : existingOperation;
74 moveItem(itemVM, removeOperation.collection, addOperation.collection, addOperation.event.clone, addOperation.event);
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;
86 if (e.item.previousElementSibling)
88 newIndex = fromArray.indexOf(ko.dataFor(e.item.previousElementSibling));
89 if (originalIndex > newIndex)
90 newIndex = newIndex + 1;
93 //Remove sortables "unbound" element
94 e.item.parentNode.removeChild(e.item);
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) {
104 fromArray.splice(originalIndex, 0, itemVM);
105 //Force knockout to update
106 from.valueHasMutated();
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();
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);
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]') {
133 into[prop] = merge(into[prop], from[prop]);
136 into[prop] = from[prop];
141 //unwrap the supplied options
142 unwrappedOptions = ko.utils.peekObservable(bindingOptions()).options || {};
144 //Make sure that we don't modify the provided settings object
145 options = merge({}, options);
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 };
154 return merge(options, unwrappedOptions);
157 ko.bindingHandlers.draggable = {
159 group: { pull: 'clone', put: false },
162 init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
163 return init(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.draggable.sortableOptions);
165 update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
166 return update(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.draggable.sortableOptions);
170 ko.bindingHandlers.sortable = {
172 group: { pull: true, put: true }
174 init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
175 return init(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.sortable.sortableOptions);
177 update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
178 return update(element, valueAccessor, allBindings, viewModel, bindingContext, ko.bindingHandlers.sortable.sortableOptions);