jquery.iviewer.js 33 KB

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