Skip to content

Commit

Permalink
add complete callback
Browse files Browse the repository at this point in the history
  • Loading branch information
springBriz committed Jan 31, 2015
1 parent c133669 commit 928b75a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.idea/
*.iml
80 changes: 50 additions & 30 deletions table_row_mover.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/**
* table row mover - jQuery Plugin
*
* @author springBriz <[email protected]>
*/
define([
"jquery"
], function ($) {
"use strict";

(function(factory) {
'use strict';

// CommonJS module
if (typeof exports === 'object') {
factory(require('jquery'));
}
// AMD module
else if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
}
// Global
else {
factory(window.jQuery);
}
}(function($) {
// direction constant variable
var DIR = {
UP: 1,
Expand All @@ -14,20 +28,21 @@ define([

/**
* @param {String} rowTagName 'tr' or 'tbody'
* @param {String} rowClassSelector default '.movable-table-row'
* @param {Object} selectors
* @param {Function} completeCallback
* @returns jQuery Object
*/
$.fn.tableRowMover = function() {
var $table = $(this),
rowTagName = (typeof arguments[0] === 'string' && arguments[0].toLowerCase() === 'tbody') ? 'tbody' : 'tr',
rowClassSelector = arguments[1] || '.movable-table-row',
selectors = $.extend({
row: '.movable-table-row',
moveTop: '.move-top',
moveUp: '.move-up',
moveDown: '.move-down',
moveBottom: '.move-bottom'
}, arguments[2] || {});
}, arguments[1] || {}),
completeCallback = arguments[2];

if ($table.css('position') === 'static') {
$table.css('position', 'relative');
Expand All @@ -38,7 +53,7 @@ define([
toEnd = e.data && e.data.toEnd || false;

var $btn = $(this),
$row = $btn.parents(rowTagName + rowClassSelector),
$row = $btn.parents(rowTagName + selectors.row),
$rowPosition = $row.position(),
$placeholder = $row.clone().removeClass();

Expand All @@ -48,7 +63,7 @@ define([
// check
if (direction === DIR.UP) {
// first or second row
var prevRowsLen = $row.prevAll(rowClassSelector).length;
var prevRowsLen = $row.prevAll(selectors.row).length;
if (prevRowsLen < 1) {
return false;
}
Expand All @@ -58,7 +73,7 @@ define([
}
else {
// last row
if ($row.nextAll(rowClassSelector).length < 1) {
if ($row.nextAll(selectors.row).length < 1) {
return false;
}
}
Expand Down Expand Up @@ -103,42 +118,42 @@ define([
// move DOM
if (direction === DIR.UP) {
if (toEnd === true) {
$row.prevAll(rowClassSelector).last().before($row);
$row.prevAll(selectors.row).last().before($row);
$theadThs.css({ borderBottomWidth: (theadThBorderBottomWidth - 1) + 'px' })
}
else {
$row.prev(rowClassSelector).before($row);
$row.prev(selectors.row).before($row);
}
}
else {
if (toEnd === true) {
$row.nextAll(rowClassSelector).last().after($row);
$row.nextAll(selectors.row).last().after($row);
}
else {
$row.next(rowClassSelector).after($row);
$row.next(selectors.row).after($row);
}
}

// animate moving
$row.animate({
top: (direction === DIR.UP ?
(
'-=' + (toEnd === true ? (function() {
var h = 0;
$placeholder.prev(rowClassSelector).prevAll(rowClassSelector).each(function() {
h += $(this).height();
});
return h;
})() : $placeholder.prev(rowClassSelector).height())
'-=' + (toEnd === true ? (function() {
var h = 0;
$placeholder.prev(selectors.row).prevAll(selectors.row).each(function() {
h += $(this).height();
});
return h;
})() : $placeholder.prev(selectors.row).height())
) : (
'+=' + (toEnd === true ? (function() {
var h = 0;
$placeholder.next(rowClassSelector).nextAll(rowClassSelector).each(function() {
h += $(this).height();
});
return h;
})() : $placeholder.next(rowClassSelector).height())
)
'+=' + (toEnd === true ? (function() {
var h = 0;
$placeholder.next(selectors.row).nextAll(selectors.row).each(function() {
h += $(this).height();
});
return h;
})() : $placeholder.next(selectors.row).height())
)
)

}, 'fast', 'swing', function() {
Expand All @@ -154,6 +169,11 @@ define([

// remove placeholder
$placeholder.remove();

// complete callback
if (typeof completeCallback === 'function') {
completeCallback();
}
});
}

Expand All @@ -166,4 +186,4 @@ define([

return $table;
};
});
}));

0 comments on commit 928b75a

Please sign in to comment.