Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Migration to Coffee

  • Loading branch information...
commit 59216df724943b3cd1b48a83a3f997aefde15ab8 1 parent 16bfa05
@mmavko authored
Showing with 224 additions and 99 deletions.
  1. +2 −2 index.html
  2. +0 −97 sort.js
  3. +93 −0 sortable.coffee
  4. +129 −0 sortable.js
View
4 index.html
@@ -1,9 +1,9 @@
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
-<script src="sort.js"></script>
+<script src="sortable.js"></script>
<script>
$(function () {
- new Sort($('ul#list'));
+ new Sortable($('ul#list'));
});
</script>
View
97 sort.js
@@ -1,97 +0,0 @@
-
-Sort = (function () {
- function _bind(f, scope) {
- return function () {
- f.apply(scope, arguments);
- }
- }
- function Sort (el, options) {
- $.extend(this, Sort.defaultOptions, options);
- this.$list = $(el).first();
- this.refreshItems();
- if (this.$items.length > 1) this.height = this.$items.get(1).offsetTop - this.$items.get(0).offsetTop;
- else this.height = 0;
- $(document).on({
- mousemove: _bind(this.onMouseMove, this),
- mouseup: _bind(this.onMouseUp, this)
- });
- this.$items.on({
- mousedown: _bind(this.onMouseDown, this)
- });
- };
- Sort.defaultOptions = {
- itemsSelector: 'li',
- browserPrefix: '-webkit-',
- transitionDuration: '0.1'
- };
- Sort.prototype = {
- refreshItems: function () {
- this.$items = this.$list.children(this.itemsSelector);
- },
- onMouseDown: function (e) {
- this.$target = $(e.target);
- this.x1 = e.pageX;
- this.y1 = e.pageY;
- this.start();
- e.preventDefault();
- },
- onMouseMove: function (e) {
- if (!this.$target) return;
- this.move(e.pageX - this.x1, e.pageY - this.y1);
- },
- onMouseUp: function (e) {
- if (!this.$target) return;
- this.end();
- delete this.$target;
- delete this.x1;
- delete this.y1;
- },
- start: function () {
- this.$items.css('position', 'relative');
- this.$items.not(this.$target).css(this.browserPrefix+'transition', 'top '+this.transitionDuration+'s');
- this.$target.css('z-index', 1);
- // store target index
- this.ti = this.$items.index(this.$target);
- },
- move: function (dx, dy) {
- this.$target.css({
- top: dy,
- left: dx
- });
- // index delta
- var di = Math.round(dy / this.height);
- if (di === this.di) return;
- this.di = di;
- // current index
- var ci = this.ti + this.di;
- // adjust positions
- this.$items.each(_bind(function (i, item) {
- if (i == this.ti) return true;
- var top = '';
- if (i >= ci && i < this.ti) top = this.height;
- if (i <= ci && i > this.ti) top = -this.height;
- $(item).css('top', top);
- }, this));
- },
- end: function () {
- this.$items.css({
- position: '',
- top: '',
- left: '',
- 'z-index': ''
- }).css(this.browserPrefix+'transition', '');
- var
- ci = this.ti + this.di,
- maxi = this.$items.length-1,
- func = 'after';
- if (ci < 0) ci = 0;
- if (ci > maxi) ci = maxi;
- if (this.di < 0) func = 'before';
- if (ci !== this.ti) $(this.$items.get(ci))[func](this.$target);
- delete this.di;
- delete this.ti;
- this.refreshItems();
- }
- };
- return Sort;
-})();
View
93 sortable.coffee
@@ -0,0 +1,93 @@
+
+class Sortable
+
+ constructor: (el, options) ->
+ $.extend @, Sortable.defaultOptions, options
+ @$list = $(el).first()
+ $(document).bind
+ mousemove: @onMouseMove
+ mouseup: @onMouseUp
+ @$list.delegate "#{@itemSelector} #{@triggerSelector}", 'mousedown', @onMouseDown
+
+ @defaultOptions:
+ itemSelector: 'li'
+ triggerSelector: ''
+ browserPrefix: '-webkit-'
+ transitionDuration: '0.1'
+
+ refreshItems: ->
+ @$items = @$list.find @itemSelector
+ if @$items.length > 1
+ @height = @$items.get(1).offsetTop - @$items.get(0).offsetTop
+ else
+ @height = 0
+
+ onMouseDown: (e) =>
+ if @triggerSelector
+ @$target = $(e.target).parent @itemSelector
+ else
+ @$target = $(e.target)
+ @x1 = e.pageX
+ @y1 = e.pageY
+ @start()
+ e.preventDefault()
+ e.stopPropagation()
+
+ onMouseMove: (e) =>
+ return unless @$target
+ @move e.pageX - @x1, e.pageY - @y1
+
+ onMouseUp: (e) =>
+ return unless @$target
+ @end()
+ delete @$target
+ delete @x1
+ delete @y1
+
+ start: ->
+ @refreshItems()
+ # set items styles
+ @$items.css 'position', 'relative'
+ @$items.not(@$target).css "#{@browserPrefix}transition", "top #{@transitionDuration}s"
+ @$target.css 'z-index', 1
+ # store target index
+ @ti = @$items.index @$target
+
+ move: (dx, dy) ->
+ @$target.css
+ top: dy
+ left: dx
+ # index delta
+ di = Math.round dy / @height
+ return if di is @di
+ @di = di
+ # current index
+ ci = @ti + @di
+ # adjust positions
+ @$items.each (i, item) =>
+ return if i is @ti
+ switch yes
+ when i >= ci and i < @ti then top = @height
+ when i <= ci and i > @ti then top = -@height
+ else top = ''
+ $(item).css 'top', top
+
+ end: ->
+ # reset items styles
+ @$items.css("#{@browserPrefix}transition", '').css
+ position: ''
+ top: ''
+ left: ''
+ 'z-index': ''
+ # current index
+ ci = @ti + @di
+ maxi = @$items.length - 1
+ ci = 0 if ci < 0
+ ci = maxi if ci > maxi
+ if @di < 0 then func = 'before' else func = 'after'
+ $(@$items.get ci)[func] @$target unless ci is @ti
+ delete @di
+ delete @ti
+
+
+if module? then module.exports = Sortable else @Sortable = Sortable
View
129 sortable.js
@@ -0,0 +1,129 @@
+(function() {
+ var Sortable;
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+
+ Sortable = (function() {
+
+ function Sortable(el, options) {
+ this.onMouseUp = __bind(this.onMouseUp, this);
+ this.onMouseMove = __bind(this.onMouseMove, this);
+ this.onMouseDown = __bind(this.onMouseDown, this); $.extend(this, Sortable.defaultOptions, options);
+ this.$list = $(el).first();
+ $(document).bind({
+ mousemove: this.onMouseMove,
+ mouseup: this.onMouseUp
+ });
+ this.$list.delegate("" + this.itemSelector + " " + this.triggerSelector, 'mousedown', this.onMouseDown);
+ }
+
+ Sortable.defaultOptions = {
+ itemSelector: 'li',
+ triggerSelector: '',
+ browserPrefix: '-webkit-',
+ transitionDuration: '0.1'
+ };
+
+ Sortable.prototype.refreshItems = function() {
+ this.$items = this.$list.find(this.itemSelector);
+ if (this.$items.length > 1) {
+ return this.height = this.$items.get(1).offsetTop - this.$items.get(0).offsetTop;
+ } else {
+ return this.height = 0;
+ }
+ };
+
+ Sortable.prototype.onMouseDown = function(e) {
+ if (this.triggerSelector) {
+ this.$target = $(e.target).parent(this.itemSelector);
+ } else {
+ this.$target = $(e.target);
+ }
+ this.x1 = e.pageX;
+ this.y1 = e.pageY;
+ this.start();
+ e.preventDefault();
+ return e.stopPropagation();
+ };
+
+ Sortable.prototype.onMouseMove = function(e) {
+ if (!this.$target) return;
+ return this.move(e.pageX - this.x1, e.pageY - this.y1);
+ };
+
+ Sortable.prototype.onMouseUp = function(e) {
+ if (!this.$target) return;
+ this.end();
+ delete this.$target;
+ delete this.x1;
+ return delete this.y1;
+ };
+
+ Sortable.prototype.start = function() {
+ this.refreshItems();
+ this.$items.css('position', 'relative');
+ this.$items.not(this.$target).css("" + this.browserPrefix + "transition", "top " + this.transitionDuration + "s");
+ this.$target.css('z-index', 1);
+ return this.ti = this.$items.index(this.$target);
+ };
+
+ Sortable.prototype.move = function(dx, dy) {
+ var ci, di;
+ var _this = this;
+ this.$target.css({
+ top: dy,
+ left: dx
+ });
+ di = Math.round(dy / this.height);
+ if (di === this.di) return;
+ this.di = di;
+ ci = this.ti + this.di;
+ return this.$items.each(function(i, item) {
+ var top;
+ if (i === _this.ti) return;
+ switch (true) {
+ case i >= ci && i < _this.ti:
+ top = _this.height;
+ break;
+ case i <= ci && i > _this.ti:
+ top = -_this.height;
+ break;
+ default:
+ top = '';
+ }
+ return $(item).css('top', top);
+ });
+ };
+
+ Sortable.prototype.end = function() {
+ var ci, func, maxi;
+ this.$items.css("" + this.browserPrefix + "transition", '').css({
+ position: '',
+ top: '',
+ left: '',
+ 'z-index': ''
+ });
+ ci = this.ti + this.di;
+ maxi = this.$items.length - 1;
+ if (ci < 0) ci = 0;
+ if (ci > maxi) ci = maxi;
+ if (this.di < 0) {
+ func = 'before';
+ } else {
+ func = 'after';
+ }
+ if (ci !== this.ti) $(this.$items.get(ci))[func](this.$target);
+ delete this.di;
+ return delete this.ti;
+ };
+
+ return Sortable;
+
+ })();
+
+ if (typeof module !== "undefined" && module !== null) {
+ module.exports = Sortable;
+ } else {
+ this.Sortable = Sortable;
+ }
+
+}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.