Created
June 18, 2013 11:28
-
-
Save vladimir-ivanov/5804634 to your computer and use it in GitHub Desktop.
AngularJs Sortable directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint bitwise:true, camelcase:true, curly:true, eqeqeq:true, forin:true, latedef:true, newcap:true, noarg:true, | |
noempty:true, nonew:true, undef:true, unused:true, strict:true, browser:true, camelcase:false */ | |
/*globals | |
$: false | |
*/ | |
/*exported | |
SortableDirective | |
*/ | |
//Assumed table with table rows and sortable attached to the tbody | |
var SortableDirective = function () { | |
"use strict"; | |
return { | |
link: function (scope, elm) { | |
var changedEl = {}; | |
$(elm).sortable({ | |
activate: function (ev, elm) { | |
changedEl.startRow = elm.item[0].rowIndex; | |
}, | |
stop: function (ev, elm) { | |
changedEl.endRow = elm.item[0].rowIndex; | |
if (changedEl.startRow !== changedEl.endRow) { // if rowIndex is undefined nothing will be triggered | |
scope.$emit('changedPosition', changedEl); | |
} | |
} | |
}); | |
// a hack to overcome jquery sortable disabling of on doulbe click event | |
$(elm).dblclick(function (ev) { | |
var el = $(ev.target); | |
if (el.is('span')) { | |
var value = $(ev.target).text() || $(ev.target).val(), | |
input = $('<input value="' + value + '" readonly="true" />'); | |
$(ev.target).replaceWith(input); | |
input.select(); | |
} | |
}); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment