terom@30: // A source of tile images of a specific width/height, zoom level range, and some other attributes terom@30: var Source = Class.create({ terom@30: initialize: function (path, tile_width, tile_height, zoom_min, zoom_max) { terom@30: this.path = path; terom@30: this.tile_width = tile_width; terom@30: this.tile_height = tile_height; terom@30: this.zoom_min = zoom_min; terom@30: this.zoom_max = zoom_max; terom@30: terom@30: this.refresh = false; terom@30: this.opt_key = this.opt_value = null; terom@30: }, terom@30: terom@30: // build a URL for the given tile image terom@30: build_url: function (col, row, zl, sw, sh) { terom@30: // two-bit hash (0-4) based on the (col, row) terom@30: var hash = ( (col % 2) << 1 | (row % 2) ) + 1; terom@30: terom@30: // the subdomain to use terom@30: var subdomain = ""; terom@30: terom@30: if (0) terom@30: subdomain = "tile" + hash + "."; terom@30: terom@30: // the (x, y) co-ordinates terom@30: var x = col * this.tile_width; terom@30: var y = row * this.tile_height; terom@30: terom@40: var url = this.path + "?x=" + x + "&y=" + y + "&zl=" + zl; // + "&sw=" + sw + "&sh=" + sh; terom@30: terom@30: if (this.refresh) terom@30: url += "&ts=" + new Date().getTime(); terom@30: terom@30: if (this.opt_key && this.opt_value) terom@30: url += "&" + this.opt_key + "=" + this.opt_value; terom@30: terom@30: return url; terom@30: }, terom@40: terom@40: // build an URL for a full image terom@40: build_image_url: function (cx, cy, w, h, zl) { terom@40: return (this.path + "?cx=" + cx + "&cy=" + cy + "&w=" + w + "&h=" + h + "&zl=" + zl); terom@40: } terom@30: }); terom@30: terom@30: // a viewport that contains a substrate which contains several zoom layers which contain many tiles terom@30: var Viewport = Class.create({ terom@30: initialize: function (source, viewport_id) { terom@30: this.source = source; terom@30: terom@30: this.id = viewport_id; terom@30: this.div = $(viewport_id); terom@30: this.substrate = this.div.down("div.substrate"); terom@30: terom@30: // the stack of zoom levels terom@30: this.zoom_layers = []; terom@30: terom@30: // pre-populate the stack terom@30: for (var zoom_level = source.zoom_min; zoom_level <= source.zoom_max; zoom_level++) { terom@30: var zoom_layer = new ZoomLayer(source, zoom_level); terom@30: terom@30: this.substrate.appendChild(zoom_layer.div); terom@30: this.zoom_layers[zoom_level] = zoom_layer; terom@30: } terom@30: terom@30: // make the substrate draggable terom@30: this.draggable = new Draggable(this.substrate, { terom@30: onStart: this.on_scroll_start.bind(this), terom@30: onDrag: this.on_scroll_move.bind(this), terom@30: onEnd: this.on_scroll_end.bind(this), terom@30: }); terom@30: terom@30: // event handlers terom@30: Event.observe(this.substrate, "dblclick", this.on_dblclick.bindAsEventListener(this)); terom@30: Event.observe(this.substrate, "mousewheel", this.on_mousewheel.bindAsEventListener(this)); terom@30: Event.observe(this.substrate, "DOMMouseScroll", this.on_mousewheel.bindAsEventListener(this)); // mozilla terom@30: Event.observe(document, "resize", this.on_resize.bindAsEventListener(this)); terom@39: terom@39: // zoom buttons terom@39: this.btn_zoom_in = $("btn-zoom-in"); terom@39: this.btn_zoom_out = $("btn-zoom-out"); terom@39: terom@39: if (this.btn_zoom_in) terom@39: Event.observe(this.btn_zoom_in, "click", this.zoom_in.bindAsEventListener(this)); terom@39: terom@39: if (this.btn_zoom_out) terom@39: Event.observe(this.btn_zoom_out, "click", this.zoom_out.bindAsEventListener(this)); terom@32: terom@32: // set viewport size terom@30: this.update_size(); terom@32: terom@40: // this comes after update_size, since it must be updated once we have the size and zoom layer... terom@40: this.image_link = $("lnk-image"); terom@40: terom@32: // initial location? terom@32: if (document.location.hash) { terom@32: // x:y:z tuple terom@32: var pt = document.location.hash.substr(1).split(":"); terom@32: terom@32: // unpack terom@32: var cx = 0, cy = 0, z = 0; terom@32: terom@32: if (pt.length) cx = parseInt(pt.shift()); terom@32: if (pt.length) cy = parseInt(pt.shift()); terom@32: if (pt.length) z = parseInt(pt.shift()); terom@30: terom@32: // initial view terom@38: this.zoom_scaled( terom@38: cx - this.center_offset_x, terom@38: cy - this.center_offset_y, terom@38: z terom@38: ); terom@32: terom@32: } else { terom@32: // this sets the scroll offsets, zoom level, and loads the tiles terom@32: this.zoom_to(0, 0, 0); terom@32: } terom@30: }, terom@30: terom@30: /* event handlers */ terom@30: terom@30: // window resized terom@30: on_resize: function (ev) { terom@30: this.update_size(); terom@30: this.update_tiles(); terom@30: }, terom@30: terom@30: // double-click handler terom@30: on_dblclick: function (ev) { terom@30: var offset = this.event_offset(ev); terom@30: terom@30: this.zoom_center_to( terom@30: this.scroll_x + offset.x, terom@30: this.scroll_y + offset.y, terom@30: 1 // zoom in terom@30: ); terom@30: }, terom@30: terom@30: // mousewheel handler terom@30: on_mousewheel: function (ev) { terom@30: // this works in very weird ways, so it's based on code from http://adomas.org/javascript-mouse-wheel/ terom@30: // (it didn't include any license, so this is written out manually) terom@30: var delta; terom@30: terom@30: // this is browser-dependant... terom@30: if (ev.wheelDelta) { terom@30: // IE + Opera terom@30: delta = ev.wheelDelta; terom@30: terom@30: if (window.opera) { terom@30: // Opera, but apparently not newer versions? terom@30: //delta = -delta; terom@30: } terom@30: terom@30: } else if (ev.detail) { terom@30: // Mozilla terom@30: delta = -ev.detail; terom@30: terom@30: } else { terom@30: // mousewheel not supported... terom@30: return; terom@30: terom@30: } terom@30: terom@30: // don't scroll the page terom@30: if (ev.preventDefault) terom@30: ev.preventDefault(); terom@30: terom@30: // delta > 0 : scroll up, zoom in terom@30: // delta < 0 : scroll down, zoom out terom@30: delta = delta < 0 ? -1 : 1; terom@30: terom@30: // Firefox's DOMMouseEvent's pageX/Y attributes are broken. layerN is for mozilla, offsetN for IE, seems to work terom@30: terom@30: // absolute location of the cursor terom@30: var x = parseInt(ev.target.style.left) + (ev.layerX ? ev.layerX : ev.offsetX); terom@30: var y = parseInt(ev.target.style.top) + (ev.layerY ? ev.layerY : ev.offsetY); terom@30: terom@30: // zoom \o/ terom@30: this.zoom_center_to(x, y, delta); terom@30: }, terom@30: terom@30: // substrate scroll was started terom@30: on_scroll_start: function (ev) { terom@30: terom@30: }, terom@30: terom@30: // substrate was scrolled, update scroll_{x,y}, and then update tiles after 100ms terom@30: on_scroll_move: function (ev) { terom@30: this.update_scroll(); terom@30: this.update_after_timeout(); terom@30: }, terom@30: terom@30: // substrate scroll was ended, update tiles now terom@30: on_scroll_end: function (ev) { terom@30: this.update_now(); terom@30: }, terom@30: terom@30: /* get state */ terom@30: terom@30: // return the absolute (x, y) coords of the given event inside the viewport terom@30: event_offset: function (ev) { terom@30: var offset = this.div.cumulativeOffset(); terom@30: terom@30: return { terom@30: x: ev.pointerX() - offset.left, terom@30: y: ev.pointerY() - offset.top terom@30: }; terom@30: }, terom@30: terom@30: /* modify state */ terom@30: terom@30: // scroll the view to place the given absolute (x, y) co-ordinate at the top left terom@30: scroll_to: function (x, y) { terom@30: // update it via the style terom@30: this.substrate.style.top = "-" + y + "px"; terom@30: this.substrate.style.left = "-" + x + "px"; terom@30: terom@30: // update these as well terom@30: this.scroll_x = x; terom@30: this.scroll_y = y; terom@30: }, terom@30: terom@30: // scroll the view to place the given absolute (x, y) co-ordinate at the center terom@30: scroll_center_to: function (x, y) { terom@30: return this.scroll_to( terom@30: x - this.center_offset_x, terom@30: y - this.center_offset_y terom@30: ); terom@30: }, terom@30: terom@30: // zoom à la delta such that the given (zoomed) absolute (x, y) co-ordinates will be at the top left terom@30: zoom_scaled: function (x, y, delta) { terom@30: if (!this.update_zoom(delta)) terom@30: return false; terom@30: terom@30: // scroll to the new position terom@30: this.scroll_to(x, y); terom@30: terom@30: // update view terom@30: // XXX: ... terom@30: this.update_after_timeout(); terom@30: terom@30: return true; terom@30: }, terom@30: terom@30: // zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the top left terom@30: zoom_to: function (x, y, delta) { terom@30: return this.zoom_scaled( terom@30: scaleByZoomDelta(x, delta), terom@30: scaleByZoomDelta(y, delta), terom@30: delta terom@30: ); terom@30: }, terom@30: terom@30: // zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the center terom@30: zoom_center_to: function (x, y, delta) { terom@30: return this.zoom_scaled( terom@30: scaleByZoomDelta(x, delta) - this.center_offset_x, terom@30: scaleByZoomDelta(y, delta) - this.center_offset_y, terom@30: delta terom@30: ); terom@30: }, terom@30: terom@39: // zoom à la delta, keeping the view centered terom@39: zoom_centered: function (delta) { terom@39: return this.zoom_center_to( terom@39: this.scroll_x + this.center_offset_x, terom@39: this.scroll_y + this.center_offset_y, terom@39: delta terom@39: ); terom@39: }, terom@39: terom@39: // zoom in one level, keeping the view centered terom@39: zoom_in: function () { terom@39: return this.zoom_centered(+1); terom@39: }, terom@39: terom@39: // zoom out one leve, keeping the view centered terom@39: zoom_out: function () { terom@39: return this.zoom_centered(-1); terom@39: }, terom@30: terom@30: /* update view/state to reflect reality */ terom@30: terom@30: // figure out the viewport dimensions terom@30: update_size: function () { terom@30: this.view_width = this.div.getWidth(); terom@30: this.view_height = this.div.getHeight(); terom@30: terom@30: this.center_offset_x = Math.floor(this.view_width / 2); terom@30: this.center_offset_y = Math.floor(this.view_height / 2); terom@40: terom@40: this.update_image_link(); terom@30: }, terom@30: terom@30: // figure out the scroll offset as absolute pixel co-ordinates at the top left terom@30: update_scroll: function() { terom@30: this.scroll_x = -parseInt(this.substrate.style.left); terom@30: this.scroll_y = -parseInt(this.substrate.style.top); terom@30: }, terom@30: terom@30: // wiggle the ZoomLevels around to match the current zoom level terom@30: update_zoom: function(delta) { terom@30: if (!this.zoom_layer) { terom@30: // first zoom operation terom@30: terom@30: // is the new zoom level valid? terom@30: if (!this.zoom_layers[delta]) terom@30: return false; terom@30: terom@30: // set the zoom layyer terom@30: this.zoom_layer = this.zoom_layers[delta]; terom@30: terom@30: // enable it terom@30: this.zoom_layer.enable(11); terom@30: terom@30: // no need to .update_tiles or anything like that terom@30: terom@30: } else { terom@30: // is the new zoom level valid? terom@30: if (!this.zoom_layers[this.zoom_layer.level + delta]) terom@30: return false; terom@30: terom@30: var zoom_old = this.zoom_layer; terom@30: var zoom_new = this.zoom_layers[this.zoom_layer.level + delta]; terom@30: terom@30: // XXX: ugly hack terom@30: if (this.zoom_timer) { terom@30: clearTimeout(this.zoom_timer); terom@30: this.zoom_timer = null; terom@30: } terom@30: terom@30: // get other zoom layers out of the way terom@30: this.zoom_layers.each(function (zl) { terom@30: zl.disable(); terom@30: }); terom@30: terom@30: // update the zoom layer terom@30: this.zoom_layer = zoom_new; terom@30: terom@30: // apply new z-indexes, preferr the current one over the new one terom@30: zoom_new.enable(11); terom@30: zoom_old.enable(10); terom@30: terom@30: // resize the tiles in the two zoom layers terom@30: zoom_new.update_tiles(zoom_new.level); terom@30: zoom_old.update_tiles(zoom_old.level); terom@30: terom@30: // XXX: ugly hack terom@30: this.zoom_timer = setTimeout(function () { zoom_old.disable()}, 1000); terom@30: } terom@30: terom@39: // update UI terom@39: this.update_zoom_ui(); terom@39: terom@30: return true; terom@30: }, terom@39: terom@39: // keep the zoom buttons, if any, updated terom@39: update_zoom_ui: function () { terom@39: if (this.btn_zoom_in) terom@39: (this.zoom_layer.level >= this.source.zoom_max) ? this.btn_zoom_in.disable() : this.btn_zoom_in.enable(); terom@39: terom@39: if (this.btn_zoom_out) terom@39: (this.zoom_layer.level <= this.source.zoom_min) ? this.btn_zoom_out.disable() : this.btn_zoom_out.enable(); terom@40: terom@40: this.update_image_link(); terom@39: }, terom@30: terom@30: // ensure that all tiles that are currently visible are loaded terom@30: update_tiles: function() { terom@30: // short names for some vars... terom@30: var x = this.scroll_x; terom@30: var y = this.scroll_y; terom@30: var sw = this.view_width; terom@30: var sh = this.view_height; terom@30: var tw = this.source.tile_width; terom@30: var th = this.source.tile_height; terom@30: var zl = this.zoom_layer.level; terom@30: terom@30: // figure out what set of columns are visible terom@30: var start_col = Math.max(0, Math.floor(x / tw)); terom@30: var start_row = Math.max(0, Math.floor(y / th)); terom@30: var end_col = Math.floor((x + sw) / tw); terom@30: var end_row = Math.floor((y + sh) / th); terom@30: terom@30: // loop through all those tiles terom@30: for (var col = start_col; col <= end_col; col++) { terom@30: for (var row = start_row; row <= end_row; row++) { terom@30: // the tile's id terom@30: var id = "tile_" + this.zoom_layer.level + "_" + col + "_" + row; terom@30: terom@30: // does the element exist already? terom@30: var t = $(id); terom@30: terom@30: if (!t) { terom@30: // build a new tile terom@30: t = Builder.node("img", { terom@30: src: this.source.build_url(col, row, zl, sw, sh), terom@30: id: id, terom@30: title: "(" + col + ", " + row + ")", terom@30: // style set later terom@30: } terom@30: ); terom@30: terom@30: // set the CSS style stuff terom@30: t.style.top = th * row; terom@30: t.style.left = tw * col; terom@30: t.style.display = "none"; terom@30: terom@30: // wait for it to load terom@30: Event.observe(t, "load", _tile_loaded.bindAsEventListener(t)); terom@30: terom@30: // store the col/row terom@30: t.__col = col; terom@30: t.__row = row; terom@30: terom@30: // add it to the zoom layer terom@30: this.zoom_layer.add_tile(t); terom@30: terom@30: } else if (this.source.reload) { terom@30: // force the tile to reload terom@30: touch_tile(t, col, row); terom@30: terom@30: } terom@30: } terom@30: } terom@40: terom@40: this.update_scroll_ui(); terom@40: }, terom@40: terom@40: // update scroll-dependant UI elements terom@40: update_scroll_ui: function () { terom@40: // update link-to-image terom@40: this.update_image_link(); terom@30: terom@30: // update the link-to-this-page thing terom@32: document.location.hash = "#" + (this.scroll_x + this.center_offset_x) + ":" + (this.scroll_y + this.center_offset_y) + ":" + this.zoom_layer.level; terom@40: }, terom@40: terom@40: // update image link with size, zoom, pos terom@40: update_image_link: function () { terom@40: if (!this.image_link) terom@40: return; terom@40: terom@40: this.image_link.href = this.source.build_image_url( terom@40: this.scroll_x + this.center_offset_x, terom@40: this.scroll_y + this.center_offset_y, terom@40: this.view_width, this.view_height, terom@40: this.zoom_layer.level terom@40: ); terom@40: terom@40: this.image_link.innerHTML = this.view_width + "x" + this.view_height + "@" + this.zoom_layer.level; terom@40: }, terom@30: terom@30: // do update_tiles after 100ms, unless we are called again terom@30: update_after_timeout: function () { terom@30: this._update_idle = false; terom@30: terom@30: if (this._update_timeout) terom@30: clearTimeout(this._update_timeout); terom@30: terom@30: this._update_timeout = setTimeout(this._update_timeout_trigger.bind(this), 100); terom@30: }, terom@30: terom@30: _update_timeout_trigger: function () { terom@30: this._update_idle = true; terom@30: terom@30: this.update_tiles(); terom@30: }, terom@30: terom@30: // call update_tiles if it hasn't been called due to update_after_timeout terom@30: update_now: function () { terom@30: if (this._update_timeout) terom@30: clearTimeout(this._update_timeout); terom@30: terom@30: if (!this._update_idle) terom@30: this.update_tiles(); terom@30: }, terom@30: terom@30: }); terom@30: terom@30: // used by Viewport.update_tiles to make a tile visible after it has loaded terom@30: function _tile_loaded (ev) { terom@30: this.style.display = "block"; terom@30: } terom@30: terom@30: // a zoom layer containing the tiles for one zoom level terom@30: var ZoomLayer = Class.create({ terom@30: initialize: function (source, zoom_level) { terom@30: this.source = source; terom@30: this.level = zoom_level; terom@30: this.div = Builder.node("div", { id: "zl_" + this.level, style: "position: relative; display: none;"}); terom@30: terom@30: // our tiles terom@30: this.tiles = []; terom@30: }, terom@30: terom@30: // add a tile to this zoom layer terom@30: add_tile: function (tile) { terom@30: this.div.appendChild(tile); terom@30: this.tiles.push(tile); terom@30: }, terom@30: terom@30: // make this zoom layer visible with the given z-index terom@30: enable: function (z_index) { terom@30: this.div.style.zIndex = z_index; terom@30: this.div.show(); terom@30: }, terom@30: terom@30: // hide this zoom layer terom@30: disable: function (z_index) { terom@30: this.div.hide(); terom@30: }, terom@30: terom@30: // update the tiles in this zoom layer so that they are in the correct position and of the correct size when terom@30: // viewed with the given zoom level terom@30: update_tiles: function (zoom_level) { terom@30: var zd = zoom_level - this.level; terom@30: terom@30: var tw = scaleByZoomDelta(this.source.tile_width, zd); terom@30: var th = scaleByZoomDelta(this.source.tile_height, zd); terom@30: terom@30: var tiles = this.tiles; terom@30: var tiles_len = tiles.length; terom@30: var t, ts; terom@30: terom@30: for (var i = 0; i < tiles_len; i++) { terom@30: t = tiles[i]; terom@30: ts = t.style; terom@30: terom@30: ts.width = tw; terom@30: ts.height = th; terom@30: ts.top = th*t.__row; terom@30: ts.left = tw*t.__col; terom@30: } terom@30: }, terom@30: terom@30: terom@30: terom@30: }); terom@30: terom@30: // scale the given co-ordinate by a zoom delta. If we zoom in (dz > 0), n will become larger, and if we zoom terom@30: // out (dz < 0), n will become smaller. terom@30: function scaleByZoomDelta (n, dz) { terom@30: if (dz > 0) terom@30: return n << dz; terom@30: else terom@30: return n >> -dz; terom@30: } terom@30: