ko-sortable - drag between lists
https://github.com/rniemeyer/knockout-sortable
by Asle Gundersby
HTML
<script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script>
<script src="https://rawgithub.com/rniemeyer/knockout-sortable/master/build/knockout-sortable.js"></script>
<div id="allocationable-texts" class="menupanel">
<h4 class="select">Text(s) to allocate:</h4>
<div class="tb-table">
<div class="tb-list" data-bind="sortable:{template:'textbatchAllocTmpl',data:$root.allocationableTextbatches,allowDrop:true,beforeMove: $root.preprocessDraggedTexts, afterMove:$root.postprocessDraggedTexts}"></div>
</div>
<span>Add text by dragging onto accordion or by pushing the button.</span>
</div>
<hr />
<div id="chosen-texts" class="menupanel">
<h4 class="select">Text(s) on scene:</h4>
<div class="tb-table">
<div class="tb-list" data-bind="sortable:{template:'textbatchSceneTmpl',data:$root.thisScenesTextbatches, allowDrop:true, beforeMove: $root.preprocessDraggedTexts, afterMove:$root.postprocessDraggedTexts, options:{update:$root.onUpdate}}"></div>
</div>
</div>
<script id="textbatchSceneTmpl" type="text/html"><div class="textbatch scene" data-bind="attr: {'title': title},click: $root.selectedText">
<span class="sequenceno" data-bind="text: $root.selectedScene().chapterNo() + '.' + sequenceNoInProject() + ' '"></span>
<span class="tb-title" data-bind="text: title"></span>
<a class="undo-textallocation" data-bind="click: function(){$root.undoTextAllocation($data)}"><span>x</span></a>
</div></script>
<script id="textbatchAllocTmpl" type="text/html"><div class="textbatch alloc" data-bind="attr: {'title': title},click: $root.selectedText">
<span class="sequenceno" data-bind="text: $root.selectedScene().chapterNo() + '.' + sequenceNoInProject() + ' '"></span>
<span class="tb-title" data-bind="text: title"></span>
<button class="btn btn-success btn-xs wysTextbatchAddButton right" title="Add text to selected scene" data-bind="click: $root.addTextbatchToScene"...
CSS
body {
font-family: arial;
background: lightgrey;
}
p, a, input {
font-size: .8em;
}
#allocationable-texts, #chosen-texts{
width: 300px;
}
span.title {
font-size: 10px;
vertical-align: text-top;
line-height: 9px;
display: block;
margin-bottom: 1px;
word-break:normal; /* when showing group description we want this - also with long email-adresses. breaks on words and hyphen*/
}
span.title {
margin-top: 1px;
margin-bottom: 0px;
}
.tb-table {
float: left;
margin-right: 5px;
width: 100%;
}
.tb-table span {
font-size: 0.75em;
padding: 2px;
}
.tb-list {
border: 3px solid #666;
height: 100px;
background-color: #777;
margin: 2px 0px 3px;
padding: 1px;
border-radius: 3px;
box-shadow: 0px 0px 0px #999;
/*overflow-y: auto;*/ /* perfectScrollbar applied */
}
.textbatch {
background-color: #333;
color: #FFF;
border-radius: 3px;
margin: 1px;
padding: 1px 0px 1px 8px;
cursor: move;
min-height: 20px;
display: inline-flex;
}
.textbatch.tb-title {
padding-left: 0px;
}
.undo-textallocation {
float: right;
width: 12px;
height: 12px;
cursor: pointer;
text-align: center;
margin: 0px 6px;
}
.undo-textallocation span {
vertical-align: text-top;
color: #F00;
font-size: 11px;
font-weight: 600;
}
JavaScript
// Wys utils
wys = {
utils : {}
};
wys.utils.getCStextbatches = function (sceneid, textbatches) {
return ko.computed(function () {
return ko.utils.arrayFilter(textbatches(), function (t) {
return ko.utils.unwrapObservable(t.sceneID) === sceneid; // sceneID is an observable for 'textbatch'!
});
});
};
Scene = function(data){
var self = this;
self.sceneID = data.sceneID;
self.title = ko.observable(data.title);
self.sequenceNo = ko.observable(data.sequenceNo);
self.chapterNo = ko.observable(data.chapterNo);
};
Textbatch = function(data){
var self = this;
self.textbatchID = data.textbatchID;
self.sceneID = ko.observable(data.sceneID);
self.title = ko.observable(data.title);
self.sequenceNo = ko.observable(data.sequenceNo);
self.sequenceNoInProject = ko.observable(data.sequenceNoInProject);
};
ViewModel = function(myscenes,mytexts){
var self = this;
// scenes
self.Scenes = ko.observableArray(
ko.utils.arrayMap(myscenes, function (item) {
return new Scene(item);
}));
self.selectedScene = ko.observable(self.Scenes()[0]);
// textbatches
self.Textbatches = ko.observableArray(
ko.utils.arrayMap(mytexts, function (item) {
return new Textbatch(item);
}));
self.selectedText = ko.observable();
// texts to allocate
self.allocationableTextbatches = ko.observableArray();
ko.computed(function () {
var notallocated = ko.utils.arrayFilter(self.Textbatches(), function (batch) {
return batch.sceneID() === null;
});
self.allocationableTextbatches(notallocated); // could we do a read-write computable for this?
// cannot use solely koFiltered as ko.sortable needs an array to write to.
});
// texts on scene
self.getCStextbatches = function (scene) { return wys.utils.getCStextbatches(scene.sceneID, self.Textbatches) };
...