imgcropper.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: imgcropper.js 30998 2012-07-06 07:22:08Z zhangguosheng $
  5. */
  6. (function(){
  7. ImgCropper = function() {
  8. this.options = {
  9. opacity: 50,
  10. color: "",
  11. width: 0,
  12. height: 0,
  13. resize: false,
  14. right: "",
  15. left: "",
  16. up: "",
  17. down: "",
  18. rightDown: "",
  19. leftDown: "",
  20. rightUp: "",
  21. leftUp: "",
  22. min: false,
  23. minWidth: 50,
  24. minHeight: 50,
  25. scale: false,
  26. ratio: 0,
  27. Preview: "",
  28. viewWidth: 0,
  29. viewHeight: 0
  30. };
  31. this.setParameter.apply(this, arguments);
  32. };
  33. ImgCropper.prototype = {
  34. setParameter: function(container, handle, url, options) {
  35. this._container = $(container);
  36. this._layHandle = $(handle);
  37. this.url = url;
  38. this._layBase = this._container.appendChild(document.createElement("img"));
  39. this._layCropper = this._container.appendChild(document.createElement("img"));
  40. this._layCropper.onload = Util.bindApply(this, this.setPos);
  41. this._tempImg = document.createElement("img");
  42. this._tempImg.onload = Util.bindApply(this, this.setSize);
  43. this.options = Util.setOptions(this.options, options || {});
  44. this.opacity = Math.round(this.options.opacity);
  45. this.color = this.options.color;
  46. this.scale = !!this.options.scale;
  47. this.ratio = Math.max(this.options.ratio, 0);
  48. this.width = Math.round(this.options.width);
  49. this.height = Math.round(this.options.height);
  50. this.setLayHandle = true;
  51. var oPreview = $(this.options.Preview);
  52. if(oPreview){
  53. oPreview.style.position = "relative";
  54. oPreview.style.overflow = "hidden";
  55. this.viewWidth = Math.round(this.options.viewWidth);
  56. this.viewHeight = Math.round(this.options.viewHeight);
  57. this._view = oPreview.appendChild(document.createElement("img"));
  58. this._view.style.position = "absolute";
  59. this._view.onload = Util.bindApply(this, this.SetPreview);
  60. }
  61. this._drag = new dzDrag(handle, {limit:true, container:container, onDragMove: Util.bindApply(this, this.setPos)});
  62. this.resize = !!this.options.resize;
  63. if(this.resize){
  64. var op = this.options;
  65. var _resize = new ImgCropperResize(container, {max: false, scale:true, min:true, minWidth:options.minWidth, minHeight:options.minHeight, onResize: Util.bindApply(this, this.scaleImg)});
  66. op.rightDown && (_resize.set(op.rightDown, "right-down"));
  67. op.leftDown && (_resize.set(op.leftDown, "left-down"));
  68. op.rightUp && (_resize.set(op.rightUp, "right-up"));
  69. op.leftUp && (_resize.set(op.leftUp, "left-up"));
  70. op.right && (_resize.set(op.right, "right"));
  71. op.left && (_resize.set(op.left, "left"));
  72. op.down && (_resize.set(op.down, "down"));
  73. op.up && (_resize.set(op.up, "up"));
  74. this.min = !!this.options.min;
  75. this.minWidth = Math.round(this.options.minWidth);
  76. this.minHeight = Math.round(this.options.minHeight);
  77. this._resize = _resize;
  78. }
  79. this._container.style.position = "relative";
  80. this._container.style.overflow = "hidden";
  81. this._layHandle.style.zIndex = 200;
  82. this._layCropper.style.zIndex = 100;
  83. this._layBase.style.position = this._layCropper.style.position = "absolute";
  84. this._layBase.style.top = this._layBase.style.left = this._layCropper.style.top = this._layCropper.style.left = 0;
  85. this.initialize();
  86. },
  87. initialize: function() {
  88. this.color && (this._container.style.backgroundColor = this.color);
  89. this._tempImg.src = this._layBase.src = this._layCropper.src = this.url;
  90. if(BROWSER.ie){
  91. this._layBase.style.filter = "alpha(opacity:" + this.opacity + ")";
  92. this._layHandle.style.filter = "alpha(opacity:0)";
  93. this._layHandle.style.backgroundColor = "#FFF";
  94. } else {
  95. this._layBase.style.opacity = this.opacity / 100;
  96. }
  97. this._view && (this._view.src = this.url);
  98. if(this.resize){
  99. with(this._resize){
  100. Scale = this.scale; Ratio = this.ratio; Min = this.min; minWidth = this.minWidth; minHeight = this.minHeight;
  101. }
  102. }
  103. },
  104. setPos: function() {
  105. if(BROWSER.ie == 6.0){ with(this._layHandle.style){ zoom = .9; zoom = 1; }; };
  106. var p = this.getPos();
  107. this._layCropper.style.clip = "rect(" + p.Top + "px " + (p.Left + p.Width) + "px " + (p.Top + p.Height) + "px " + p.Left + "px)";
  108. this.SetPreview();
  109. parent.resetHeight(this._container, this.getPos(), this._layBase);
  110. },
  111. scaleImg:function() {
  112. this.height = this._resize._styleHeight;
  113. this.width = this._resize._styleWidth;
  114. this.initialize();
  115. this.setSize();
  116. this.setPos();
  117. var maxRight = (parseInt(this._layHandle.style.left) || 0) + (parseInt(this._layHandle.offsetWidth) || 0);
  118. var maxBottom = (parseInt(this._layHandle.style.top) || 0) + (parseInt(this._layHandle.offsetHeight) || 0);
  119. if(this._container != null) {
  120. if(maxRight > this._container.clientWidth) {
  121. var nowLeft = this._container.clientWidth - this._layHandle.offsetWidth;
  122. this._layHandle.style.left = (nowLeft < 0 ? 0 : nowLeft) + "px";
  123. }
  124. if(maxBottom > this._container.clientHeight) {
  125. var nowTop = this._container.clientHeight - this._layHandle.offsetHeight;
  126. this._layHandle.style.top = (nowTop < 0 ? 0 : nowTop) + "px";
  127. }
  128. }
  129. parent.resetHeight(this._container, this.getPos(), this._layBase);
  130. },
  131. SetPreview: function() {
  132. if(this._view){
  133. var p = this.getPos(), s = this.getSize(p.Width, p.Height, this.viewWidth, this.viewHeight), scale = s.Height / p.Height;
  134. var pHeight = this._layBase.height * scale, pWidth = this._layBase.width * scale, pTop = p.Top * scale, pLeft = p.Left * scale;
  135. with(this._view.style){
  136. width = pWidth + "px"; height = pHeight + "px"; top = - pTop + "px "; left = - pLeft + "px";
  137. clip = "rect(" + pTop + "px " + (pLeft + s.Width) + "px " + (pTop + s.Height) + "px " + pLeft + "px)";
  138. }
  139. }
  140. },
  141. setSize: function() {
  142. if(this.width > this._tempImg.width) {
  143. this.width = this._tempImg.width;
  144. }
  145. if(this.height > this._tempImg.height) {
  146. this.height = this._tempImg.height;
  147. }
  148. var s = this.getSize(this._tempImg.width, this._tempImg.height, this.width, this.height);
  149. if(this.options.min && (s.Width <= this.options.minWidth || s.Height <= this.options.minHeight)) {
  150. return false;
  151. }
  152. this._layBase.style.width = this._layCropper.style.width = s.Width + "px";
  153. this._layBase.style.height = this._layCropper.style.height = s.Height + "px";
  154. this._drag.maxRight = s.Width; this._drag.maxBottom = s.Height;
  155. if(this.resize) {
  156. this._container.style.width = this._layBase.style.width; this._container.style.height = this._layBase.style.height;
  157. if(this.setLayHandle) {
  158. this._layHandle.style.left = ((s.Width - this._layHandle.offsetWidth)/2)+"px";
  159. this._layHandle.style.top = ((s.Height - this._layHandle.offsetHeight)/2)+"px";
  160. this.setPos();
  161. this.setLayHandle = false;
  162. }
  163. }
  164. },
  165. getPos: function() {
  166. with(this._layHandle){
  167. return { Top: offsetTop, Left: offsetLeft, Width: offsetWidth, Height: offsetHeight };
  168. }
  169. },
  170. getSize: function(nowWidth, nowHeight, fixWidth, fixHeight) {
  171. var iWidth = nowWidth, iHeight = nowHeight, scale = iWidth / iHeight;
  172. if(fixHeight){ iWidth = (iHeight = fixHeight) * scale; }
  173. if(fixWidth && (!fixHeight || iWidth > fixWidth)){ iHeight = (iWidth = fixWidth) / scale; }
  174. return { Width: iWidth, Height: iHeight }
  175. }
  176. };
  177. ImgCropperResize = function() {
  178. this.options = {
  179. max: false,
  180. container: "",
  181. maxLeft: 0,
  182. maxRight: 9999,
  183. maxTop: 0,
  184. maxBottom: 9999,
  185. min: false,
  186. minWidth: 50,
  187. minHeight: 50,
  188. scale: false,
  189. ratio: 0,
  190. onResize: function(){}
  191. };
  192. this.initialize.apply(this, arguments);
  193. };
  194. ImgCropperResize.prototype = {
  195. initialize: function(resizeObjId, options) {
  196. this.options = Util.setOptions(this.options, options || {});
  197. this._resizeObj = $(resizeObjId);
  198. this._styleWidth = this._styleHeight = this._styleLeft = this._styleTop = 0;
  199. this._sideRight = this._sideDown = this._sideLeft = this._sideUp = 0;
  200. this._fixLeft = this._fixTop = 0;
  201. this._scaleLeft = this._scaleTop = 0;
  202. this._maxSet = function(){};
  203. this._maxRightWidth = this._maxDownHeight = this._maxUpHeight = this._maxLeftWidth = 0;
  204. this._maxScaleWidth = this._maxScaleHeight = 0;
  205. this._fun = function(){};
  206. var _style = Util.currentStyle(this._resizeObj);
  207. this._borderX = (parseInt(_style.borderLeftWidth) || 0) + (parseInt(_style.borderRightWidth) || 0);
  208. this._borderY = (parseInt(_style.borderTopWidth) || 0) + (parseInt(_style.borderBottomWidth) || 0);
  209. this._resizeTranscript = Util.bindApply(this, this.resize);
  210. this._stopTranscript = Util.bindApply(this, this.stop);
  211. this.max = !!this.options.max;
  212. this._container = $(this.options.container) || null;
  213. this.maxLeft = Math.round(this.options.maxLeft);
  214. this.maxRight = Math.round(this.options.maxRight);
  215. this.maxTop = Math.round(this.options.maxTop);
  216. this.maxBottom = Math.round(this.options.maxBottom);
  217. this.min = !!this.options.min;
  218. this.minWidth = Math.round(this.options.minWidth);
  219. this.minHeight = Math.round(this.options.minHeight);
  220. this.scale = !!this.options.scale;
  221. this.ratio = Math.max(this.options.ratio, 0);
  222. this.onResize = this.options.onResize;
  223. this._resizeObj.style.position = "absolute";
  224. !this._container || Util.currentStyle(this._container).position == "relative" || (this._container.style.position = "relative");
  225. },
  226. set: function(resize, side) {
  227. var resize = $(resize), fun;
  228. if(!resize) return;
  229. switch(side.toLowerCase()) {
  230. case "up":
  231. fun = this.up;
  232. break;
  233. case "down":
  234. fun = this.down;
  235. break;
  236. case "left":
  237. fun = this.left;
  238. break;
  239. case "right":
  240. fun = this.right;
  241. break;
  242. case "left-up":
  243. fun = this.leftUp;
  244. break;
  245. case "right-up":
  246. fun = this.rightUp;
  247. break;
  248. case "left-down":
  249. fun = this.leftDown;
  250. break;
  251. case "right-down" :
  252. default:
  253. fun = this.rightDown;
  254. break;
  255. };
  256. Util.addEventHandler(resize, "mousedown", Util.bindApply(this, this.start, fun));
  257. },
  258. start: function(oEvent, fun, touch) {
  259. oEvent.stopPropagation ? oEvent.stopPropagation() : (oEvent.cancelBubble = true);
  260. this._fun = fun;
  261. this._styleWidth = this._resizeObj.clientWidth;
  262. this._styleHeight = this._resizeObj.clientHeight;
  263. this._styleLeft = this._resizeObj.offsetLeft;
  264. this._styleTop = this._resizeObj.offsetTop;
  265. this._sideLeft = oEvent.clientX - this._styleWidth;
  266. this._sideRight = oEvent.clientX + this._styleWidth;
  267. this._sideUp = oEvent.clientY - this._styleHeight;
  268. this._sideDown = oEvent.clientY + this._styleHeight;
  269. this._fixLeft = this._styleLeft + this._styleWidth;
  270. this._fixTop = this._styleTop + this._styleHeight;
  271. if(this.scale) {
  272. this.ratio = Math.max(this.ratio, 0) || this._styleWidth / this._styleHeight;
  273. this._scaleLeft = this._styleLeft + this._styleWidth / 2;
  274. this._scaleTop = this._styleTop + this._styleHeight / 2;
  275. };
  276. if(this.max) {
  277. var maxLeft = this.maxLeft, maxRight = this.maxRight, maxTop = this.maxTop, maxBottom = this.maxBottom;
  278. if(!!this._container){
  279. maxLeft = Math.max(maxLeft, 0);
  280. maxTop = Math.max(maxTop, 0);
  281. maxRight = Math.min(maxRight, this._container.clientWidth);
  282. maxBottom = Math.min(maxBottom, this._container.clientHeight);
  283. };
  284. maxRight = Math.max(maxRight, maxLeft + (this.min ? this.minWidth : 0) + this._borderX);
  285. maxBottom = Math.max(maxBottom, maxTop + (this.min ? this.minHeight : 0) + this._borderY);
  286. this._mxSet = function() {
  287. this._maxRightWidth = maxRight - this._styleLeft - this._borderX;
  288. this._maxDownHeight = maxBottom - this._styleTop - this._borderY;
  289. this._maxUpHeight = Math.max(this._fixTop - maxTop, this.min ? this.minHeight : 0);
  290. this._maxLeftWidth = Math.max(this._fixLeft - maxLeft, this.min ? this.minWidth : 0);
  291. };
  292. this._mxSet();
  293. if(this.scale) {
  294. this._maxScaleWidth = Math.min(this._scaleLeft - maxLeft, maxRight - this._scaleLeft - this._borderX) * 2;
  295. this._maxScaleHeight = Math.min(this._scaleTop - maxTop, maxBottom - this._scaleTop - this._borderY) * 2;
  296. }
  297. }
  298. Util.addEventHandler(document, "mousemove", this._resizeTranscript);
  299. Util.addEventHandler(document, "mouseup", this._stopTranscript);
  300. if(BROWSER.ie){
  301. Util.addEventHandler(this._resizeObj, "losecapture", this._stopTranscript);
  302. this._resizeObj.setCapture();
  303. }else{
  304. Util.addEventHandler(window, "blur", this._stopTranscript);
  305. oEvent.preventDefault();
  306. };
  307. },
  308. resize: function(e) {
  309. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  310. this._fun(e);
  311. if(this.options.min && (this._styleWidth <= this.options.minWidth || this._styleHeight <= this.options.minHeight)) {
  312. return false;
  313. }
  314. with(this._resizeObj.style) {
  315. width = this._styleWidth + "px"; height = this._styleHeight + "px";
  316. top = this._styleTop + "px"; left = this._styleLeft + "px";
  317. }
  318. this.onResize();
  319. },
  320. up: function(e) {
  321. this.repairY(this._sideDown - e.clientY, this._maxUpHeight);
  322. this.repairTop();
  323. this.turnDown(this.down);
  324. },
  325. down: function(e) {
  326. this.repairY(e.clientY - this._sideUp, this._maxDownHeight);
  327. this.turnUp(this.up);
  328. },
  329. right: function(e) {
  330. this.repairX(e.clientX - this._sideLeft, this._maxRightWidth);
  331. this.turnLeft(this.left);
  332. },
  333. left: function(e) {
  334. this.repairX(this._sideRight - e.clientX, this._maxLeftWidth);
  335. this.repairLeft();
  336. this.turnRight(this.right);
  337. },
  338. rightDown: function(e) {
  339. this.repairAngle(
  340. e.clientX - this._sideLeft, this._maxRightWidth,
  341. e.clientY - this._sideUp, this._maxDownHeight
  342. );
  343. this.turnLeft(this.leftDown) || this.scale || this.turnUp(this.rightUp);
  344. },
  345. rightUp: function(e) {
  346. this.repairAngle(
  347. e.clientX - this._sideLeft, this._maxRightWidth,
  348. this._sideDown - e.clientY, this._maxUpHeight
  349. );
  350. this.repairTop();
  351. this.turnLeft(this.leftUp) || this.scale || this.turnDown(this.rightDown);
  352. },
  353. leftDown: function(e) {
  354. this.repairAngle(
  355. this._sideRight - e.clientX, this._maxLeftWidth,
  356. e.clientY - this._sideUp, this._maxDownHeight
  357. );
  358. this.repairLeft();
  359. this.turnRight(this.rightDown) || this.scale || this.turnUp(this.leftUp);
  360. },
  361. leftUp: function(e) {
  362. this.repairAngle(
  363. this._sideRight - e.clientX, this._maxLeftWidth,
  364. this._sideDown - e.clientY, this._maxUpHeight
  365. );
  366. this.repairTop();
  367. this.repairLeft();
  368. this.turnRight(this.rightUp) || this.scale || this.turnDown(this.leftDown);
  369. },
  370. repairX: function(iWidth, maxWidth) {
  371. iWidth = this.repairWidth(iWidth, maxWidth);
  372. if(this.scale){
  373. var iHeight = this.repairScaleHeight(iWidth);
  374. if(this.max && iHeight > this._maxScaleHeight){
  375. iHeight = this._maxScaleHeight;
  376. iWidth = this.repairScaleWidth(iHeight);
  377. }else if(this.min && iHeight < this.minHeight){
  378. var tWidth = this.repairScaleWidth(this.minHeight);
  379. if(tWidth < maxWidth){ iHeight = this.minHeight; iWidth = tWidth; }
  380. }
  381. this._styleHeight = iHeight;
  382. this._styleTop = this._scaleTop - iHeight / 2;
  383. }
  384. this._styleWidth = iWidth;
  385. },
  386. repairY: function(iHeight, maxHeight) {
  387. iHeight = this.repairHeight(iHeight, maxHeight);
  388. if(this.scale){
  389. var iWidth = this.repairScaleWidth(iHeight);
  390. if(this.max && iWidth > this._maxScaleWidth){
  391. iWidth = this._maxScaleWidth;
  392. iHeight = this.repairScaleHeight(iWidth);
  393. }else if(this.min && iWidth < this.minWidth){
  394. var tHeight = this.repairScaleHeight(this.minWidth);
  395. if(tHeight < maxHeight){ iWidth = this.minWidth; iHeight = tHeight; }
  396. }
  397. this._styleWidth = iWidth;
  398. this._styleLeft = this._scaleLeft - iWidth / 2;
  399. }
  400. this._styleHeight = iHeight;
  401. },
  402. repairAngle: function(iWidth, maxWidth, iHeight, maxHeight) {
  403. iWidth = this.repairWidth(iWidth, maxWidth);
  404. if(this.scale) {
  405. iHeight = this.repairScaleHeight(iWidth);
  406. if(this.max && iHeight > maxHeight){
  407. iHeight = maxHeight;
  408. iWidth = this.repairScaleWidth(iHeight);
  409. }else if(this.min && iHeight < this.minHeight){
  410. var tWidth = this.repairScaleWidth(this.minHeight);
  411. if(tWidth < maxWidth){ iHeight = this.minHeight; iWidth = tWidth; }
  412. }
  413. } else {
  414. iHeight = this.repairHeight(iHeight, maxHeight);
  415. }
  416. this._styleWidth = iWidth;
  417. this._styleHeight = iHeight;
  418. },
  419. repairTop: function() {
  420. this._styleTop = this._fixTop - this._styleHeight;
  421. },
  422. repairLeft: function() {
  423. this._styleLeft = this._fixLeft - this._styleWidth;
  424. },
  425. repairHeight: function(iHeight, maxHeight) {
  426. iHeight = Math.min(this.max ? maxHeight : iHeight, iHeight);
  427. iHeight = Math.max(this.min ? this.minHeight : iHeight, iHeight, 0);
  428. return iHeight;
  429. },
  430. repairWidth: function(iWidth, maxWidth) {
  431. iWidth = Math.min(this.max ? maxWidth : iWidth, iWidth);
  432. iWidth = Math.max(this.min ? this.minWidth : iWidth, iWidth, 0);
  433. return iWidth;
  434. },
  435. repairScaleHeight: function(iWidth) {
  436. return Math.max(Math.round((iWidth + this._borderX) / this.ratio - this._borderY), 0);
  437. },
  438. repairScaleWidth: function(iHeight) {
  439. return Math.max(Math.round((iHeight + this._borderY) * this.ratio - this._borderX), 0);
  440. },
  441. turnRight: function(fun) {
  442. if(!(this.min || this._styleWidth)){
  443. this._fun = fun;
  444. this._sideLeft = this._sideRight;
  445. this.max && this._mxSet();
  446. return true;
  447. }
  448. },
  449. turnLeft: function(fun) {
  450. if(!(this.min || this._styleWidth)){
  451. this._fun = fun;
  452. this._sideRight = this._sideLeft;
  453. this._fixLeft = this._styleLeft;
  454. this.max && this._mxSet();
  455. return true;
  456. }
  457. },
  458. turnUp: function(fun) {
  459. if(!(this.min || this._styleHeight)){
  460. this._fun = fun;
  461. this._sideDown = this._sideUp;
  462. this._fixTop = this._styleTop;
  463. this.max && this._mxSet();
  464. return true;
  465. }
  466. },
  467. turnDown: function(fun) {
  468. if(!(this.min || this._styleHeight)){
  469. this._fun = fun;
  470. this._sideUp = this._sideDown;
  471. this.max && this._mxSet();
  472. return true;
  473. }
  474. },
  475. stop: function() {
  476. Util.removeEventHandler(document, "mousemove", this._resizeTranscript);
  477. Util.removeEventHandler(document, "mouseup", this._stopTranscript);
  478. if(BROWSER.ie){
  479. Util.removeEventHandler(this._resizeObj, "losecapture", this._stopTranscript);
  480. this._resizeObj.releaseCapture();
  481. }else{
  482. Util.removeEventHandler(window, "blur", this._stopTranscript);
  483. }
  484. }
  485. };
  486. dzDrag = function() {
  487. this.options = {
  488. handle: '',
  489. limit: false,
  490. maxLeft: 0,
  491. maxRight: 9999,
  492. maxTop: 0,
  493. maxBottom: 9999,
  494. container: '',
  495. lockX: false,
  496. lockY: false,
  497. onDragStart: function(){},
  498. onDragMove: function(){},
  499. onDragEnd: function(){}
  500. };
  501. this.initialize.apply(this, arguments);
  502. };
  503. dzDrag.prototype = {
  504. initialize: function(dragId, options) {
  505. this.options = Util.setOptions(this.options, options || {});
  506. this._dragObj = $(dragId);
  507. this._x = this._y = 0;
  508. this._marginLeft = this._marginTop = 0;
  509. this._handle = $(this.options.handle) || this._dragObj;
  510. this._container = $(this.options.container) || null;
  511. this._dragObj.style.position = "absolute";
  512. this._dragEndTranscript = Util.bindApply(this, this.dragEnd);
  513. this._dragMoveTranscript = Util.bindApply(this, this.dragMove);
  514. Util.addEventHandler(this._handle, "mousedown", Util.bindApply(this, this.dragStart));
  515. },
  516. dragStart: function(event) {
  517. this.setLimit();
  518. this._x = event.clientX - this._dragObj.offsetLeft;
  519. this._y = event.clientY - this._dragObj.offsetTop;
  520. var curStyle = Util.currentStyle(this._dragObj);
  521. this._marginLeft = parseInt(curStyle.marginLeft) || 0;
  522. this._marginTop = parseInt(curStyle.marginTop) || 0;
  523. Util.addEventHandler(document, "mousemove", this._dragMoveTranscript);
  524. Util.addEventHandler(document, "mouseup", this._dragEndTranscript);
  525. if(BROWSER.ie){
  526. Util.addEventHandler(this._handle, "losecapture", this._dragEndTranscript);
  527. this._handle.setCapture();
  528. }else{
  529. Util.addEventHandler(window, "blur", this._dragEndTranscript);
  530. event.preventDefault();
  531. };
  532. this.options.onDragStart();
  533. },
  534. dragMove: function(event) {
  535. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  536. var iLeft = event.clientX - this._x;
  537. var iTop = event.clientY - this._y;
  538. if(this.options.limit) {
  539. var maxLeft = this.options.maxLeft, maxRight = this.options.maxRight, maxTop = this.options.maxTop, maxBottom = this.options.maxBottom;
  540. if(this._container != null){
  541. maxLeft = Math.max(maxLeft, 0);
  542. maxTop = Math.max(maxTop, 0);
  543. maxRight = Math.min(maxRight, this._container.clientWidth);
  544. maxBottom = Math.min(maxBottom, this._container.clientHeight);
  545. };
  546. iLeft = Math.max(Math.min(iLeft, maxRight - this._dragObj.offsetWidth), maxLeft);
  547. iTop = Math.max(Math.min(iTop, maxBottom - this._dragObj.offsetHeight), maxTop);
  548. }
  549. if(!this.options.lockX) { this._dragObj.style.left = iLeft - this._marginLeft + "px"; }
  550. if(!this.options.lockY) { this._dragObj.style.top = iTop - this._marginTop + "px"; }
  551. this.options.onDragMove();
  552. },
  553. dragEnd: function(event) {
  554. Util.removeEventHandler(document, "mousemove", this._dragMoveTranscript);
  555. Util.removeEventHandler(document, "mouseup", this._dragEndTranscript);
  556. if(BROWSER.ie) {
  557. Util.removeEventHandler(this._handle, "losecapture", this._dragEndTranscript);
  558. this._handle.releaseCapture();
  559. } else {
  560. Util.removeEventHandler(window, "blur", this._dragEndTranscript);
  561. }
  562. this.options.onDragEnd();
  563. },
  564. setLimit: function() {
  565. if(this.options.limit) {
  566. this.options.maxRight = Math.max(this.options.maxRight, this.options.maxLeft + this._dragObj.offsetWidth);
  567. this.options.maxBottom = Math.max(this.options.maxBottom, this.options.maxTop + this._dragObj.offsetHeight);
  568. !this._container || Util.currentStyle(this._container).position == "relative" || Util.currentStyle(this._container).position == "absolute" || (this._container.style.position = "relative");
  569. }
  570. }
  571. };
  572. var Util = {
  573. setOptions: function(object, source) {
  574. for(var property in source) {
  575. object[property] = source[property];
  576. }
  577. return object;
  578. },
  579. addEventHandler: function(targetObj, eventType, funHandler) {
  580. if(targetObj.addEventListener) {
  581. targetObj.addEventListener(eventType, funHandler, false);
  582. } else if (targetObj.attachEvent) {
  583. targetObj.attachEvent("on" + eventType, funHandler);
  584. } else {
  585. targetObj["on" + eventType] = funHandler;
  586. }
  587. },
  588. removeEventHandler: function(targetObj, eventType, funHandler) {
  589. if(targetObj.removeEventListener) {
  590. targetObj.removeEventListener(eventType, funHandler, false);
  591. } else if (targetObj.detachEvent) {
  592. targetObj.detachEvent("on" + eventType, funHandler);
  593. } else {
  594. targetObj["on" + eventType] = null;
  595. }
  596. },
  597. bindApply: function(object, fun) {
  598. var args = Array.prototype.slice.call(arguments).slice(2);
  599. return function(event) {
  600. return fun.apply(object, [event || window.event].concat(args));
  601. };
  602. },
  603. currentStyle: function(element){
  604. return element.currentStyle || document.defaultView.getComputedStyle(element, null);
  605. }
  606. };
  607. })();