You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.iviewer.js 33 KiB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * iviewer Widget for jQuery UI
  3. * https://github.com/can3p/iviewer
  4. *
  5. * Copyright (c) 2009 - 2012 Dmitry Petrov
  6. * Dual licensed under the MIT and GPL licenses.
  7. * - http://www.opensource.org/licenses/mit-license.php
  8. * - http://www.gnu.org/copyleft/gpl.html
  9. *
  10. * Author: Dmitry Petrov
  11. * Version: 0.7
  12. */
  13. ( function( $, undefined ) {
  14. //this code was taken from the https://github.com/furf/jquery-ui-touch-punch
  15. var mouseEvents = {
  16. touchstart: 'mousedown',
  17. touchmove: 'mousemove',
  18. touchend: 'mouseup'
  19. };
  20. /**
  21. * Convert a touch event to a mouse-like
  22. */
  23. function makeMouseEvent (event) {
  24. var touch = event.originalEvent.changedTouches[0];
  25. return $.extend(event, {
  26. type: mouseEvents[event.type],
  27. which: 1,
  28. pageX: touch.pageX,
  29. pageY: touch.pageY,
  30. screenX: touch.screenX,
  31. screenY: touch.screenY,
  32. clientX: touch.clientX,
  33. clientY: touch.clientY,
  34. isTouchEvent: true
  35. });
  36. }
  37. var mouseProto = $.ui.mouse.prototype,
  38. _mouseInit = $.ui.mouse.prototype._mouseInit;
  39. mouseProto._mouseInit = function() {
  40. var self = this;
  41. self._touchActive = false;
  42. this.element.bind( 'touchstart.' + this.widgetName, function(event) {
  43. self._touchActive = true;
  44. return self._mouseDown(makeMouseEvent(event));
  45. })
  46. var self = this;
  47. // these delegates are required to keep context
  48. this._mouseMoveDelegate = function(event) {
  49. if (self._touchActive) {
  50. return self._mouseMove(makeMouseEvent(event));
  51. }
  52. };
  53. this._mouseUpDelegate = function(event) {
  54. if (self._touchActive) {
  55. self._touchActive = false;
  56. return self._mouseUp(makeMouseEvent(event));
  57. }
  58. };
  59. $(document)
  60. .bind('touchmove.'+ this.widgetName, this._mouseMoveDelegate)
  61. .bind('touchend.' + this.widgetName, this._mouseUpDelegate);
  62. _mouseInit.apply(this);
  63. }
  64. /**
  65. * Simple implementation of jQuery like getters/setters
  66. * var val = something();
  67. * something(val);
  68. */
  69. var setter = function(setter, getter) {
  70. return function(val) {
  71. if (arguments.length === 0) {
  72. return getter.apply(this);
  73. } else {
  74. setter.apply(this, arguments);
  75. }
  76. }
  77. };
  78. /**
  79. * Internet explorer rotates image relative left top corner, so we should
  80. * shift image when it's rotated.
  81. */
  82. var ieTransforms = {
  83. '0': {
  84. marginLeft: 0,
  85. marginTop: 0,
  86. filter: 'progid:DXImageTransform.Microsoft.Matrix(M11=1, M12=0, M21=0, M22=1, SizingMethod="auto expand")'
  87. },
  88. '90': {
  89. marginLeft: -1,
  90. marginTop: 1,
  91. filter: 'progid:DXImageTransform.Microsoft.Matrix(M11=0, M12=-1, M21=1, M22=0, SizingMethod="auto expand")'
  92. },
  93. '180': {
  94. marginLeft: 0,
  95. marginTop: 0,
  96. filter: 'progid:DXImageTransform.Microsoft.Matrix(M11=-1, M12=0, M21=0, M22=-1, SizingMethod="auto expand")'
  97. },
  98. '270': {
  99. marginLeft: -1,
  100. marginTop: 1,
  101. filter: 'progid:DXImageTransform.Microsoft.Matrix(M11=0, M12=1, M21=-1, M22=0, SizingMethod="auto expand")'
  102. }
  103. },
  104. useIeTransforms = (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 8);
  105. $.widget( "ui.iviewer", $.ui.mouse, {
  106. widgetEventPrefix: "iviewer",
  107. options : {
  108. /**
  109. * start zoom value for image, not used now
  110. * may be equal to "fit" to fit image into container or scale in %
  111. **/
  112. zoom: "fit",
  113. /**
  114. * base value to scale image
  115. **/
  116. zoom_base: 100,
  117. /**
  118. * maximum zoom
  119. **/
  120. zoom_max: 800,
  121. /**
  122. * minimum zoom
  123. **/
  124. zoom_min: 25,
  125. /**
  126. * base of rate multiplier.
  127. * zoom is calculated by formula: zoom_base * zoom_delta^rate
  128. **/
  129. zoom_delta: 1.4,
  130. /**
  131. * whether the zoom should be animated.
  132. */
  133. zoom_animation: true,
  134. /**
  135. * if true plugin doesn't add its own controls
  136. **/
  137. ui_disabled: false,
  138. /**
  139. * if false, plugin doesn't bind resize event on window and this must
  140. * be handled manually
  141. **/
  142. update_on_resize: true,
  143. /**
  144. * event is triggered when zoom value is changed
  145. * @param int new zoom value
  146. * @return boolean if false zoom action is aborted
  147. **/
  148. onZoom: jQuery.noop,
  149. /**
  150. * event is triggered when zoom value is changed after image is set to the new dimensions
  151. * @param int new zoom value
  152. * @return boolean if false zoom action is aborted
  153. **/
  154. onAfterZoom: jQuery.noop,
  155. /**
  156. * event is fired on drag begin
  157. * @param object coords mouse coordinates on the image
  158. * @return boolean if false is returned, drag action is aborted
  159. **/
  160. onStartDrag: jQuery.noop,
  161. /**
  162. * event is fired on drag action
  163. * @param object coords mouse coordinates on the image
  164. **/
  165. onDrag: jQuery.noop,
  166. /**
  167. * event is fired on drag stop
  168. * @param object coords mouse coordinates on the image
  169. **/
  170. onStopDrag: jQuery.noop,
  171. /**
  172. * event is fired when mouse moves over image
  173. * @param object coords mouse coordinates on the image
  174. **/
  175. onMouseMove: jQuery.noop,
  176. /**
  177. * mouse click event
  178. * @param object coords mouse coordinates on the image
  179. **/
  180. onClick: jQuery.noop,
  181. /**
  182. * event is fired when image starts to load
  183. */
  184. onStartLoad: null,
  185. /**
  186. * event is fired, when image is loaded and initially positioned
  187. */
  188. onFinishLoad: null
  189. },
  190. _create: function() {
  191. var me = this;
  192. //drag variables
  193. this.dx = 0;
  194. this.dy = 0;
  195. /* object containing actual information about image
  196. * @img_object.object - jquery img object
  197. * @img_object.orig_{width|height} - original dimensions
  198. * @img_object.display_{width|height} - actual dimensions
  199. */
  200. this.img_object = {};
  201. this.zoom_object = {}; //object to show zoom status
  202. this._angle = 0;
  203. this.current_zoom = this.options.zoom;
  204. if(this.options.src === null){
  205. return;
  206. }
  207. this.container = this.element;
  208. this._updateContainerInfo();
  209. //init container
  210. this.container.css("overflow","hidden");
  211. if(this.options.update_on_resize == true)
  212. {
  213. $(window).resize(function()
  214. {
  215. me._updateContainerInfo();
  216. });
  217. }
  218. this.img_object = new $.ui.iviewer.ImageObject(this.options.zoom_animation);
  219. //init object
  220. this.img_object.object()
  221. //bind mouse events
  222. .click(function(e){return me._click(e)})
  223. .mousewheel(function(ev, delta)
  224. {
  225. //this event is there instead of containing div, because
  226. //at opera it triggers many times on div
  227. var zoom = (delta > 0)?1:-1;
  228. me.zoom_by(zoom);
  229. return false;
  230. })
  231. .prependTo(this.container);
  232. this.container.bind('mousemove', function(ev) { me._handleMouseMove(ev); });
  233. this.loadImage(this.options.src);
  234. if(!this.options.ui_disabled)
  235. {
  236. this.createui();
  237. }
  238. this._mouseInit();
  239. },
  240. destroy: function() {
  241. this._mouseDestroy();
  242. },
  243. _updateContainerInfo: function()
  244. {
  245. this.options.height = this.container.height();
  246. this.options.width = this.container.width();
  247. },
  248. loadImage: function( src )
  249. {
  250. this.current_zoom = this.options.zoom;
  251. var me = this;
  252. this._trigger('onStartLoad', 0, src);
  253. this.img_object.load(src, function() {
  254. me.container.addClass("iviewer_cursor");
  255. if(me.options.zoom == "fit"){
  256. me.fit(true);
  257. }
  258. else {
  259. me.set_zoom(me.options.zoom, true);
  260. }
  261. if(me.options.onFinishLoad)
  262. {
  263. me._trigger('onFinishLoad', 0, src);
  264. }
  265. });
  266. },
  267. /**
  268. * fits image in the container
  269. *
  270. * @param {boolean} skip_animation
  271. **/
  272. fit: function(skip_animation)
  273. {
  274. var aspect_ratio = this.img_object.orig_width() / this.img_object.orig_height();
  275. var window_ratio = this.options.width / this.options.height;
  276. var choose_left = (aspect_ratio > window_ratio);
  277. var new_zoom = 0;
  278. if(choose_left){
  279. new_zoom = this.options.width / this.img_object.orig_width() * 100;
  280. }
  281. else {
  282. new_zoom = this.options.height / this.img_object.orig_height() * 100;
  283. }
  284. this.set_zoom(new_zoom, skip_animation);
  285. },
  286. /**
  287. * center image in container
  288. **/
  289. center: function()
  290. {
  291. this.setCoords(-Math.round((this.img_object.display_width() - this.options.width)/2),
  292. -Math.round((this.img_object.display_height() - this.options.height)/2));
  293. },
  294. /**
  295. * move a point in container to the center of display area
  296. * @param x a point in container
  297. * @param y a point in container
  298. **/
  299. moveTo: function(x, y)
  300. {
  301. var dx = x-Math.round(this.options.width/2);
  302. var dy = y-Math.round(this.options.height/2);
  303. var new_x = this.img_object.x() - dx;
  304. var new_y = this.img_object.y() - dy;
  305. this.setCoords(new_x, new_y);
  306. },
  307. /**
  308. * Get container offset object.
  309. */
  310. getContainerOffset: function() {
  311. return jQuery.extend({}, this.container.offset());
  312. },
  313. /**
  314. * set coordinates of upper left corner of image object
  315. **/
  316. setCoords: function(x,y)
  317. {
  318. //do nothing while image is being loaded
  319. if(!this.img_object.loaded()) { return; }
  320. var coords = this._correctCoords(x,y);
  321. this.img_object.x(coords.x);
  322. this.img_object.y(coords.y);
  323. },
  324. _correctCoords: function( x, y )
  325. {
  326. x = parseInt(x, 10);
  327. y = parseInt(y, 10);
  328. //check new coordinates to be correct (to be in rect)
  329. if(y > 0){
  330. y = 0;
  331. }
  332. if(x > 0){
  333. x = 0;
  334. }
  335. if(y + this.img_object.display_height() < this.options.height){
  336. y = this.options.height - this.img_object.display_height();
  337. }
  338. if(x + this.img_object.display_width() < this.options.width){
  339. x = this.options.width - this.img_object.display_width();
  340. }
  341. if(this.img_object.display_width() <= this.options.width){
  342. x = -(this.img_object.display_width() - this.options.width)/2;
  343. }
  344. if(this.img_object.display_height() <= this.options.height){
  345. y = -(this.img_object.display_height() - this.options.height)/2;
  346. }
  347. return { x: x, y:y };
  348. },
  349. /**
  350. * convert coordinates on the container to the coordinates on the image (in original size)
  351. *
  352. * @return object with fields x,y according to coordinates or false
  353. * if initial coords are not inside image
  354. **/
  355. containerToImage : function (x,y)
  356. {
  357. var coords = { x : x - this.img_object.x(),
  358. y : y - this.img_object.y()
  359. };
  360. coords = this.img_object.toOriginalCoords(coords);
  361. return { x : util.descaleValue(coords.x, this.current_zoom),
  362. y : util.descaleValue(coords.y, this.current_zoom)
  363. };
  364. },
  365. /**
  366. * convert coordinates on the image (in original size, and zero angle) to the coordinates on the container
  367. *
  368. * @return object with fields x,y according to coordinates
  369. **/
  370. imageToContainer : function (x,y)
  371. {
  372. var coords = {
  373. x : util.scaleValue(x, this.current_zoom),
  374. y : util.scaleValue(y, this.current_zoom)
  375. };
  376. return this.img_object.toRealCoords(coords);
  377. },
  378. /**
  379. * get mouse coordinates on the image
  380. * @param e - object containing pageX and pageY fields, e.g. mouse event object
  381. *
  382. * @return object with fields x,y according to coordinates or false
  383. * if initial coords are not inside image
  384. **/
  385. _getMouseCoords : function(e)
  386. {
  387. var containerOffset = this.container.offset();
  388. coords = this.containerToImage(e.pageX - containerOffset.left, e.pageY - containerOffset.top);
  389. return coords;
  390. },
  391. /**
  392. * set image scale to the new_zoom
  393. *
  394. * @param {number} new_zoom image scale in %
  395. * @param {boolean} skip_animation
  396. **/
  397. set_zoom: function(new_zoom, skip_animation)
  398. {
  399. if (this._trigger('onZoom', 0, new_zoom) == false) {
  400. return;
  401. }
  402. //do nothing while image is being loaded
  403. if(!this.img_object.loaded()) { return; }
  404. if(new_zoom < this.options.zoom_min)
  405. {
  406. new_zoom = this.options.zoom_min;
  407. }
  408. else if(new_zoom > this.options.zoom_max)
  409. {
  410. new_zoom = this.options.zoom_max;
  411. }
  412. /* we fake these values to make fit zoom properly work */
  413. if(this.current_zoom == "fit")
  414. {
  415. var old_x = Math.round(this.options.width/2 + this.img_object.orig_width()/2);
  416. var old_y = Math.round(this.options.height/2 + this.img_object.orig_height()/2);
  417. this.current_zoom = 100;
  418. }
  419. else {
  420. var old_x = -this.img_object.x() + Math.round(this.options.width/2);
  421. var old_y = -this.img_object.y() + Math.round(this.options.height/2);
  422. }
  423. var new_width = util.scaleValue(this.img_object.orig_width(), new_zoom);
  424. var new_height = util.scaleValue(this.img_object.orig_height(), new_zoom);
  425. var new_x = util.scaleValue( util.descaleValue(old_x, this.current_zoom), new_zoom);
  426. var new_y = util.scaleValue( util.descaleValue(old_y, this.current_zoom), new_zoom);
  427. new_x = this.options.width/2 - new_x;
  428. new_y = this.options.height/2 - new_y;
  429. this.img_object.display_width(new_width);
  430. this.img_object.display_height(new_height);
  431. var coords = this._correctCoords( new_x, new_y ),
  432. self = this;
  433. this.img_object.setImageProps(new_width, new_height, coords.x, coords.y,
  434. skip_animation, function() {
  435. self._trigger('onAfterZoom', 0, new_zoom );
  436. });
  437. this.current_zoom = new_zoom;
  438. this.update_status();
  439. },
  440. /**
  441. * changes zoom scale by delta
  442. * zoom is calculated by formula: zoom_base * zoom_delta^rate
  443. * @param Integer delta number to add to the current multiplier rate number
  444. **/
  445. zoom_by: function(delta)
  446. {
  447. var closest_rate = this.find_closest_zoom_rate(this.current_zoom);
  448. var next_rate = closest_rate + delta;
  449. var next_zoom = this.options.zoom_base * Math.pow(this.options.zoom_delta, next_rate)
  450. if(delta > 0 && next_zoom < this.current_zoom)
  451. {
  452. next_zoom *= this.options.zoom_delta;
  453. }
  454. if(delta < 0 && next_zoom > this.current_zoom)
  455. {
  456. next_zoom /= this.options.zoom_delta;
  457. }
  458. this.set_zoom(next_zoom);
  459. },
  460. /**
  461. * Rotate image
  462. * @param {num} deg Degrees amount to rotate. Positive values rotate image clockwise.
  463. * Currently 0, 90, 180, 270 and -90, -180, -270 values are supported
  464. *
  465. * @param {boolean} abs If the flag is true if, the deg parameter will be considered as
  466. * a absolute value and relative otherwise.
  467. * @return {num|null} Method will return current image angle if called without any arguments.
  468. **/
  469. angle: function(deg, abs) {
  470. if (arguments.length === 0) { return this.img_object.angle(); }
  471. if (deg < -270 || deg > 270 || deg % 90 !== 0) { return; }
  472. if (!abs) { deg += this.img_object.angle(); }
  473. if (deg < 0) { deg += 360; }
  474. if (deg >= 360) { deg -= 360; }
  475. if (deg === this.img_object.angle()) { return; }
  476. this.img_object.angle(deg);
  477. //the rotate behavior is different in all editors. For now we just center the
  478. //image. However, it will be better to try to keep the position.
  479. this.center();
  480. this._trigger('angle', 0, { angle: this.img_object.angle() });
  481. },
  482. /**
  483. * finds closest multiplier rate for value
  484. * basing on zoom_base and zoom_delta values from settings
  485. * @param Number value zoom value to examine
  486. **/
  487. find_closest_zoom_rate: function(value)
  488. {
  489. if(value == this.options.zoom_base)
  490. {
  491. return 0;
  492. }
  493. function div(val1,val2) { return val1 / val2 };
  494. function mul(val1,val2) { return val1 * val2 };
  495. var func = (value > this.options.zoom_base)?mul:div;
  496. var sgn = (value > this.options.zoom_base)?1:-1;
  497. var mltplr = this.options.zoom_delta;
  498. var rate = 1;
  499. while(Math.abs(func(this.options.zoom_base, Math.pow(mltplr,rate)) - value) >
  500. Math.abs(func(this.options.zoom_base, Math.pow(mltplr,rate+1)) - value))
  501. {
  502. rate++;
  503. }
  504. return sgn * rate;
  505. },
  506. /* update scale info in the container */
  507. update_status: function()
  508. {
  509. if(!this.options.ui_disabled)
  510. {
  511. var percent = Math.round(100*this.img_object.display_height()/this.img_object.orig_height());
  512. if(percent)
  513. {
  514. this.zoom_object.html(percent + "%");
  515. }
  516. }
  517. },
  518. /**
  519. * Get some information about the image.
  520. * Currently orig_(width|height), display_(width|height), angle, zoom and src params are supported.
  521. *
  522. * @param {string} parameter to check
  523. * @param {boolean} withoutRotation if param is orig_width or orig_height and this flag is set to true,
  524. * method will return original image width without considering rotation.
  525. *
  526. */
  527. info: function(param, withoutRotation) {
  528. if (!param) { return; }
  529. switch (param) {
  530. case 'orig_width':
  531. case 'orig_height':
  532. if (withoutRotation) {
  533. return (this.img_object.angle() % 180 === 0 ? this.img_object[param]() :
  534. param === 'orig_width' ? this.img_object.orig_height() :
  535. this.img_object.orig_width());
  536. } else {
  537. return this.img_object[param]();
  538. }
  539. case 'display_width':
  540. case 'display_height':
  541. case 'angle':
  542. return this.img_object[param]();
  543. case 'zoom':
  544. return this.current_zoom;
  545. case 'src':
  546. return this.img_object.object().attr('src');
  547. }
  548. },
  549. /**
  550. * callback for handling mousdown event to start dragging image
  551. **/
  552. _mouseStart: function( e )
  553. {
  554. $.ui.mouse.prototype._mouseStart.call(this, e);
  555. if (this._trigger('onStartDrag', 0, this._getMouseCoords(e)) === false) {
  556. return false;
  557. }
  558. /* start drag event*/
  559. this.container.addClass("iviewer_drag_cursor");
  560. this.dx = e.pageX - this.img_object.x();
  561. this.dy = e.pageY - this.img_object.y();
  562. return true;
  563. },
  564. _mouseCapture: function( e ) {
  565. return true;
  566. },
  567. /**
  568. * Handle mouse move if needed. User can avoid using this callback, because
  569. * he can get the same information through public methods.
  570. * @param {jQuery.Event} e
  571. */
  572. _handleMouseMove: function(e) {
  573. this._trigger('onMouseMove', e, this._getMouseCoords(e));
  574. },
  575. /**
  576. * callback for handling mousemove event to drag image
  577. **/
  578. _mouseDrag: function(e)
  579. {
  580. $.ui.mouse.prototype._mouseDrag.call(this, e);
  581. var ltop = e.pageY - this.dy;
  582. var lleft = e.pageX - this.dx;
  583. this.setCoords(lleft, ltop);
  584. this._trigger('onDrag', e, this._getMouseCoords(e));
  585. return false;
  586. },
  587. /**
  588. * callback for handling stop drag
  589. **/
  590. _mouseStop: function(e)
  591. {
  592. $.ui.mouse.prototype._mouseStop.call(this, e);
  593. this.container.removeClass("iviewer_drag_cursor");
  594. this._trigger('onStopDrag', 0, this._getMouseCoords(e));
  595. },
  596. _click: function(e)
  597. {
  598. this._trigger('onClick', 0, this._getMouseCoords(e));
  599. },
  600. /**
  601. * create zoom buttons info box
  602. **/
  603. createui: function()
  604. {
  605. var me=this;
  606. $("<div>", { 'class': "iviewer_zoom_in iviewer_common iviewer_button"})
  607. .bind('mousedown touchstart',function(){me.zoom_by(1); return false;})
  608. .appendTo(this.container);
  609. $("<div>", { 'class': "iviewer_zoom_out iviewer_common iviewer_button"})
  610. .bind('mousedown touchstart',function(){me.zoom_by(- 1); return false;})
  611. .appendTo(this.container);
  612. $("<div>", { 'class': "iviewer_zoom_zero iviewer_common iviewer_button"})
  613. .bind('mousedown touchstart',function(){me.set_zoom(100); return false;})
  614. .appendTo(this.container);
  615. $("<div>", { 'class': "iviewer_zoom_fit iviewer_common iviewer_button"})
  616. .bind('mousedown touchstart',function(){me.fit(this); return false;})
  617. .appendTo(this.container);
  618. this.zoom_object = $("<div>").addClass("iviewer_zoom_status iviewer_common")
  619. .appendTo(this.container);
  620. $("<div>", { 'class': "iviewer_rotate_left iviewer_common iviewer_button"})
  621. .bind('mousedown touchstart',function(){me.angle(-90); return false;})
  622. .appendTo(this.container);
  623. $("<div>", { 'class': "iviewer_rotate_right iviewer_common iviewer_button" })
  624. .bind('mousedown touchstart',function(){me.angle(90); return false;})
  625. .appendTo(this.container);
  626. this.update_status(); //initial status update
  627. }
  628. } );
  629. /**
  630. * @class $.ui.iviewer.ImageObject Class represents image and provides public api without
  631. * extending image prototype.
  632. * @constructor
  633. * @param {boolean} do_anim Do we want to animate image on dimension changes?
  634. */
  635. $.ui.iviewer.ImageObject = function(do_anim) {
  636. this._img = $("<img>")
  637. //this is needed, because chromium sets them auto otherwise
  638. .css({ position: "absolute", top :"0px", left: "0px"});
  639. this._loaded = false;
  640. this._swapDimensions = false;
  641. this._do_anim = do_anim || false;
  642. this.x(0, true);
  643. this.y(0, true);
  644. this.angle(0);
  645. };
  646. /** @lends $.ui.iviewer.ImageObject.prototype */
  647. (function() {
  648. /**
  649. * Restore initial object state.
  650. *
  651. * @param {number} w Image width.
  652. * @param {number} h Image height.
  653. */
  654. this._reset = function(w, h) {
  655. this._angle = 0;
  656. this._swapDimensions = false;
  657. this.x(0);
  658. this.y(0);
  659. this.orig_width(w);
  660. this.orig_height(h);
  661. this.display_width(w);
  662. this.display_height(h);
  663. };
  664. /**
  665. * Check if image is loaded.
  666. *
  667. * @return {boolean}
  668. */
  669. this.loaded = function() { return this._loaded; };
  670. /**
  671. * Load image.
  672. *
  673. * @param {string} src Image url.
  674. * @param {Function=} loaded Function will be called on image load.
  675. */
  676. this.load = function(src, loaded) {
  677. var self = this;
  678. loaded = loaded || jQuery.noop;
  679. this._loaded = false;
  680. //If we assign new image url to the this._img IE9 fires onload event and image width and
  681. //height are set to zero. So, we create another image object and load image through it.
  682. var img = new Image();
  683. img.onload = function() {
  684. self._loaded = true;
  685. self._reset(this.width, this.height);
  686. self._img[0].src = src;
  687. loaded();
  688. };
  689. img.src = src;
  690. this._img
  691. .removeAttr("src")
  692. .removeAttr("width")
  693. .removeAttr("height")
  694. .removeAttr("style")
  695. .css({ position: "absolute", top :"0px", left: "0px"})
  696. this.angle(0);
  697. };
  698. this._dimension = function(prefix, name) {
  699. var horiz = '_' + prefix + '_' + name,
  700. vert = '_' + prefix + '_' + (name === 'height' ? 'width' : 'height');
  701. return setter(function(val) {
  702. this[this._swapDimensions ? horiz: vert] = val;
  703. },
  704. function() {
  705. return this[this._swapDimensions ? horiz: vert];
  706. });
  707. };
  708. /**
  709. * Getters and setter for common image dimensions.
  710. * display_ means real image tag dimensions
  711. * orig_ means physical image dimensions.
  712. * Note, that dimensions are swapped if image is rotated. It necessary,
  713. * because as little as possible code should know about rotation.
  714. */
  715. this.display_width = this._dimension('display', 'width'),
  716. this.display_height = this._dimension('display', 'height'),
  717. this.display_diff = function() { return Math.floor( this.display_width() - this.display_height() ) };
  718. this.orig_width = this._dimension('orig', 'width'),
  719. this.orig_height = this._dimension('orig', 'height'),
  720. /**
  721. * Setter for X coordinate. If image is rotated we need to additionaly shift an
  722. * image to map image coordinate to the visual position.
  723. *
  724. * @param {number} val Coordinate value.
  725. * @param {boolean} skipCss If true, we only set the value and do not touch the dom.
  726. */
  727. this.x = setter(function(val, skipCss) {
  728. this._x = val;
  729. if (!skipCss) {
  730. this._img.css("left",this._x + (this._swapDimensions ? this.display_diff() / 2 : 0) + "px");
  731. }
  732. },
  733. function() {
  734. return this._x;
  735. });
  736. /**
  737. * Setter for Y coordinate. If image is rotated we need to additionaly shift an
  738. * image to map image coordinate to the visual position.
  739. *
  740. * @param {number} val Coordinate value.
  741. * @param {boolean} skipCss If true, we only set the value and do not touch the dom.
  742. */
  743. this.y = setter(function(val, skipCss) {
  744. this._y = val;
  745. if (!skipCss) {
  746. this._img.css("top",this._y - (this._swapDimensions ? this.display_diff() / 2 : 0) + "px");
  747. }
  748. },
  749. function() {
  750. return this._y;
  751. });
  752. /**
  753. * Perform image rotation.
  754. *
  755. * @param {number} deg Absolute image angle. The method will work with values 0, 90, 180, 270 degrees.
  756. */
  757. this.angle = setter(function(deg) {
  758. var prevSwap = this._swapDimensions;
  759. this._angle = deg;
  760. this._swapDimensions = deg % 180 !== 0;
  761. if (prevSwap !== this._swapDimensions) {
  762. var verticalMod = this._swapDimensions ? -1 : 1;
  763. this.x(this.x() - verticalMod * this.display_diff() / 2, true);
  764. this.y(this.y() + verticalMod * this.display_diff() / 2, true);
  765. };
  766. var cssVal = 'rotate(' + deg + 'deg)',
  767. img = this._img;
  768. jQuery.each(['', '-webkit-', '-moz-', '-o-', '-ms-'], function(i, prefix) {
  769. img.css(prefix + 'transform', cssVal);
  770. });
  771. if (useIeTransforms) {
  772. jQuery.each(['-ms-', ''], function(i, prefix) {
  773. img.css(prefix + 'filter', ieTransforms[deg].filter);
  774. });
  775. img.css({
  776. marginLeft: ieTransforms[deg].marginLeft * this.display_diff() / 2,
  777. marginTop: ieTransforms[deg].marginTop * this.display_diff() / 2
  778. });
  779. }
  780. },
  781. function() { return this._angle; });
  782. /**
  783. * Map point in the container coordinates to the point in image coordinates.
  784. * You will get coordinates of point on image with respect to rotation,
  785. * but will be set as if image was not rotated.
  786. * So, if image was rotated 90 degrees, it's (0,0) point will be on the
  787. * top right corner.
  788. *
  789. * @param {{x: number, y: number}} point Point in container coordinates.
  790. * @return {{x: number, y: number}}
  791. */
  792. this.toOriginalCoords = function(point) {
  793. switch (this.angle()) {
  794. case 0: return { x: point.x, y: point.y }
  795. case 90: return { x: point.y, y: this.display_width() - point.x }
  796. case 180: return { x: this.display_width() - point.x, y: this.display_height() - point.y }
  797. case 270: return { x: this.display_height() - point.y, y: point.x }
  798. }
  799. };
  800. /**
  801. * Map point in the image coordinates to the point in container coordinates.
  802. * You will get coordinates of point on container with respect to rotation.
  803. * Note, if image was rotated 90 degrees, it's (0,0) point will be on the
  804. * top right corner.
  805. *
  806. * @param {{x: number, y: number}} point Point in container coordinates.
  807. * @return {{x: number, y: number}}
  808. */
  809. this.toRealCoords = function(point) {
  810. switch (this.angle()) {
  811. case 0: return { x: this.x() + point.x, y: this.y() + point.y }
  812. case 90: return { x: this.x() + this.display_width() - point.y, y: this.y() + point.x}
  813. case 180: return { x: this.x() + this.display_width() - point.x, y: this.y() + this.display_height() - point.y}
  814. case 270: return { x: this.x() + point.y, y: this.y() + this.display_height() - point.x}
  815. }
  816. };
  817. /**
  818. * @return {jQuery} Return image node. this is needed to add event handlers.
  819. */
  820. this.object = setter(jQuery.noop,
  821. function() { return this._img; });
  822. /**
  823. * Change image properties.
  824. *
  825. * @param {number} disp_w Display width;
  826. * @param {number} disp_h Display height;
  827. * @param {number} x
  828. * @param {number} y
  829. * @param {boolean} skip_animation If true, the animation will be skiped despite the
  830. * value set in constructor.
  831. * @param {Function=} complete Call back will be fired when zoom will be complete.
  832. */
  833. this.setImageProps = function(disp_w, disp_h, x, y, skip_animation, complete) {
  834. complete = complete || jQuery.noop;
  835. this.display_width(disp_w);
  836. this.display_height(disp_h);
  837. this.x(x, true);
  838. this.y(y, true);
  839. var w = this._swapDimensions ? disp_h : disp_w;
  840. var h = this._swapDimensions ? disp_w : disp_h;
  841. var params = {
  842. width: w,
  843. height: h,
  844. top: y - (this._swapDimensions ? this.display_diff() / 2 : 0) + "px",
  845. left: x + (this._swapDimensions ? this.display_diff() / 2 : 0) + "px"
  846. };
  847. if (useIeTransforms) {
  848. jQuery.extend(params, {
  849. marginLeft: ieTransforms[this.angle()].marginLeft * this.display_diff() / 2,
  850. marginTop: ieTransforms[this.angle()].marginTop * this.display_diff() / 2
  851. });
  852. }
  853. var swapDims = this._swapDimensions,
  854. img = this._img;
  855. //here we come: another IE oddness. If image is rotated 90 degrees with a filter, than
  856. //width and height getters return real width and height of rotated image. The bad news
  857. //is that to set height you need to set a width and vice versa. Fuck IE.
  858. //So, in this case we have to animate width and height manually.
  859. if(useIeTransforms && swapDims) {
  860. var ieh = this._img.width(),
  861. iew = this._img.height(),
  862. iedh = params.height - ieh;
  863. iedw = params.width - iew;
  864. delete params.width;
  865. delete params.height;
  866. }
  867. if (this._do_anim && !skip_animation) {
  868. this._img.animate(params, {
  869. duration: 200,
  870. complete: complete,
  871. step: function(now, fx) {
  872. if(useIeTransforms && swapDims && (fx.prop === 'top')) {
  873. var percent = (now - fx.start) / (fx.end - fx.start);
  874. img.height(ieh + iedh * percent);
  875. img.width(iew + iedw * percent);
  876. img.css('top', now);
  877. }
  878. }
  879. });
  880. } else {
  881. this._img.css(params);
  882. setTimeout(complete, 0); //both if branches should behave equally.
  883. }
  884. };
  885. }).apply($.ui.iviewer.ImageObject.prototype);
  886. var util = {
  887. scaleValue: function(value, toZoom)
  888. {
  889. return value * toZoom / 100;
  890. },
  891. descaleValue: function(value, fromZoom)
  892. {
  893. return value * 100 / fromZoom;
  894. }
  895. };
  896. } )( jQuery, undefined );