bjui-basedrag.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*!
  2. * B-JUI v1.2 (http://b-jui.com)
  3. * Git@OSC (http://git.oschina.net/xknaan/B-JUI)
  4. * Copyright 2014 K'naan (xknaan@163.com).
  5. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  6. */
  7. /* ========================================================================
  8. * B-JUI: bjui-basedrag.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.drag.js (author:Roger Wu)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-basedrag.js
  12. * ========================================================================
  13. * Copyright 2014 K'naan.
  14. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  15. * ======================================================================== */
  16. +function ($) {
  17. 'use strict';
  18. // BASEDRAG CLASS DEFINITION
  19. // ======================
  20. var Basedrag = function(element, options) {
  21. this.$element = $(element)
  22. this.options = options
  23. }
  24. Basedrag.prototype.init = function() {
  25. var that = this
  26. this.options.$obj = this.$element
  27. if (this.options.obj) this.options.$obj = this.options.obj
  28. if (this.options.event)
  29. this.start(this.options.event)
  30. else
  31. this.$element.find(this.options.selector).bind('mousedown', function(e) { that.start.apply(that, [e]) })
  32. }
  33. Basedrag.prototype.start = function(e) {
  34. document.onselectstart = function(e) { return false } //禁止选择
  35. var that = this
  36. this.options.oleft = parseInt(this.$element.css('left')) || 0
  37. this.options.otop = parseInt(this.$element.css('top')) || 0
  38. $(document).bind('mouseup.bjui.basedrag', function(e) { that.stop.apply(that, [e]) })
  39. .bind('mousemove.bjui.basedrag', function(e) { that.drag.apply(that, [e]) })
  40. }
  41. Basedrag.prototype.drag = function(e) {
  42. if (!e) e = window.event
  43. var options = this.options
  44. var left = (options.oleft + (e.pageX || e.clientX) - options.event.pageX)
  45. var top = (options.otop + (e.pageY || e.clientY) - options.event.pageY)
  46. if (top < 1) top = 0
  47. if (options.move == 'horizontal') {
  48. if ((options.minW && left >= parseInt(this.options.$obj.css('left')) + options.minW) && (options.maxW && left <= parseInt(this.options.$obj.css('left')) + options.maxW)) {
  49. this.$element.css('left', left)
  50. } else if (options.scop) {
  51. if (options.relObj) {
  52. if ((left - parseInt(options.relObj.css('left'))) > options.cellMinW)
  53. this.$element.css('left', left)
  54. else
  55. this.$element.css('left', left)
  56. }
  57. }
  58. } else if (options.move == 'vertical') {
  59. this.$element.css('top', top)
  60. } else {
  61. var $selector = options.selector ? this.options.$obj.find(options.selector) : this.options.$obj
  62. if (left >= -$selector.outerWidth() * 2 / 3 && top >= 0 && (left + $selector.outerWidth() / 3 < $(window).width()) && (top + $selector.outerHeight() < $(window).height())) {
  63. this.$element.css({left:left, top:top})
  64. }
  65. }
  66. if (options.drag)
  67. options.drag.apply(this.$element, [this.$element, e])
  68. return this.preventEvent(e)
  69. }
  70. Basedrag.prototype.stop = function(e) {
  71. $(document).unbind('mousemove.bjui.basedrag').unbind('mouseup.bjui.basedrag')
  72. if (this.options.stop)
  73. this.options.stop.apply(this.$element, [this.$element, e])
  74. if (this.options.event)
  75. this.destroy()
  76. document.onselectstart = function(e) { return true } //启用选择
  77. return this.preventEvent(e)
  78. }
  79. Basedrag.prototype.preventEvent = function(e) {
  80. if (e.stopPropagation) e.stopPropagation()
  81. if (e.preventDefault) e.preventDefault()
  82. return false
  83. }
  84. Basedrag.prototype.destroy = function() {
  85. this.$element.removeData('bjui.basedrag')
  86. if (!this.options.nounbind) this.$element.unbind('mousedown')
  87. }
  88. // BASEDRAG PLUGIN DEFINITION
  89. // =======================
  90. function Plugin(option) {
  91. var args = arguments
  92. var property = option
  93. return this.each(function () {
  94. var $this = $(this)
  95. var options = $.extend({}, $this.data(), typeof option == 'object' && option)
  96. var data = $this.data('bjui.basedrag')
  97. if (!data) $this.data('bjui.basedrag', (data = new Basedrag(this, options)))
  98. if (typeof property == 'string' && $.isFunction(data[property])) {
  99. [].shift.apply(args)
  100. if (!args) data[property]()
  101. else data[property].apply(data, args)
  102. } else {
  103. data.init()
  104. }
  105. })
  106. }
  107. var old = $.fn.basedrag
  108. $.fn.basedrag = Plugin
  109. $.fn.basedrag.Constructor = Basedrag
  110. // BASEDRAG NO CONFLICT
  111. // =================
  112. $.fn.basedrag.noConflict = function () {
  113. $.fn.basedrag = old
  114. return this
  115. }
  116. }(jQuery);