static/tiles2.js
author Tero Marttila <terom@fixme.fi>
Sat, 10 Apr 2010 22:30:00 +0300
branchunscaled-coordinates
changeset 128 66c95c2d212c
parent 127 df89d13f2354
permissions -rw-r--r--
partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
// A source of tile images of a specific width/height, zoom level range, and some other attributes
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     2
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     3
/**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     4
 * A source of image tiles.
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     5
 *
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     6
 * The image source is expected to serve fixed-size tiles (tile_width × tile_height) of image data based on the 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     7
 * x, y, zl URL query parameters. 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     8
 *
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     9
 *  x, y        - the pixel coordinates of the top-left corner
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    10
 *                XXX: these are scaled from the image coordinates by the zoom level
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    11
 *
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    12
 *  zl          - the zoom level used, in < zl < out.
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    13
 *                The image pixels are scaled by powers-of-two, so a 256x256 tile at zl=1 shows a 512x512 area of the
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    14
 *                1:1 image.
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    15
 */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
var Source = Class.create({
43
fcd818eb5a71 center initial view
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    17
    initialize: function (path, tile_width, tile_height, zoom_min, zoom_max, img_width, img_height) {
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
        this.path = path;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        this.tile_width = tile_width;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        this.tile_height = tile_height;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        this.zoom_min = zoom_min;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        this.zoom_max = zoom_max;
43
fcd818eb5a71 center initial view
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    23
        this.img_width = img_width;
fcd818eb5a71 center initial view
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    24
        this.img_height = img_height;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        this.refresh = false;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        this.opt_key = this.opt_value = null;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    30
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    31
     * Return an URL representing the tile at the given viewport (row, col) at the given zl.
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    32
     *
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    33
     * XXX: sw/sh = screen-width/height, used to choose an appropriate output size for dynamic image sources...
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    34
     */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    35
    build_url: function (col, row, zl /*, sw, sh */) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    36
        // XXX: distribute tile requests across tile*.foo.bar
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    37
        if (0) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    38
            // two-bit hash (0-4) based on the (col, row)
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    39
            var hash = ( (col % 2) << 1 | (row % 2) ) + 1;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    40
            
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    41
            // the subdomain to use
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    42
            var subdomain = "";
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    43
            
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
            subdomain = "tile" + hash + ".";
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    45
        }
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        // the (x, y) co-ordinates
128
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
    48
        var x = scaleByZoomDelta(col * this.tile_width, zl);
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
    49
        var y = scaleByZoomDelta(row * this.tile_height, zl);
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    51
        var url = this.path + "?x=" + x + "&y=" + y + "&zl=" + zl; // + "&sw=" + sw + "&sh=" + sh;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    52
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    53
        // refresh the tile each time it is loaded?
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        if (this.refresh)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
            url += "&ts=" + new Date().getTime();
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    56
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    57
        // XXX: additional parameters, not used
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        if (this.opt_key && this.opt_value)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            url += "&" + this.opt_key + "=" + this.opt_value;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        return url;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    },
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    63
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    64
    // build an URL for a full image
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    65
    build_image_url: function (cx, cy, w, h, zl) {
128
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
    66
        return (this.path + "?cx=" + scaleByZoomDelta(cx, zl) + "&cy=" + scaleByZoomDelta(cy, zl) + "&w=" + w + "&h=" + h + "&zl=" + zl);
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
    67
    }
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
});
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    70
/**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    71
 * Viewport implements the tiles-UI. It consists of a draggable substrate, which in turn consists of several
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    72
 * ZoomLayers, which then contain the actual tile images.
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    73
 *
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    74
 * Vars:
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    75
 *      scroll_x/y      - the visible pixel offset of the top-left corner
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    76
 */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
var Viewport = Class.create({
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    initialize: function (source, viewport_id) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        this.source = source;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    80
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    81
        // get a handle on the UI elements
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
        this.id = viewport_id;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
        this.div = $(viewport_id);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        this.substrate = this.div.down("div.substrate");
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
        // make the substrate draggable
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
        this.draggable = new Draggable(this.substrate, {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
            onStart: this.on_scroll_start.bind(this),
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
            onDrag: this.on_scroll_move.bind(this),
49
9dedd0e30b6b don't use zindex for draggable
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    90
            onEnd: this.on_scroll_end.bind(this),
9dedd0e30b6b don't use zindex for draggable
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    91
9dedd0e30b6b don't use zindex for draggable
Tero Marttila <terom@fixme.fi>
parents: 48
diff changeset
    92
            zindex: false
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        });
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
    95
        // register event handlers for other UI functions
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
        Event.observe(this.substrate, "dblclick", this.on_dblclick.bindAsEventListener(this));
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        Event.observe(this.substrate, "mousewheel", this.on_mousewheel.bindAsEventListener(this));
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
        Event.observe(this.substrate, "DOMMouseScroll", this.on_mousewheel.bindAsEventListener(this));     // mozilla
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        Event.observe(document, "resize", this.on_resize.bindAsEventListener(this));
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   100
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   101
        // init zoom UI buttons
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   102
        this.btn_zoom_in = $("btn-zoom-in");
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   103
        this.btn_zoom_out = $("btn-zoom-out");
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   104
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   105
        if (this.btn_zoom_in)
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   106
            Event.observe(this.btn_zoom_in, "click", this.zoom_in.bindAsEventListener(this));
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   107
        
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   108
        if (this.btn_zoom_out)
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   109
            Event.observe(this.btn_zoom_out, "click", this.zoom_out.bindAsEventListener(this));
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   110
           
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   111
        // initial view location (centered)
50
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   112
        var cx = this.source.img_width / 2;
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   113
        var cy = this.source.img_height / 2;
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   114
        var zl = 0; // XXX: would need to scale x/y for this: (this.source.zoom_min + this.source.zoom_max) / 2;
32
aa168c7da551 implement x:y:z tuple in URL
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   115
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   116
        // from link?
32
aa168c7da551 implement x:y:z tuple in URL
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   117
        if (document.location.hash) {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   118
            // parse x:y:z tuple
32
aa168c7da551 implement x:y:z tuple in URL
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   119
            var pt = document.location.hash.substr(1).split(":");
aa168c7da551 implement x:y:z tuple in URL
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   120
            
43
fcd818eb5a71 center initial view
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   121
            // unpack    
50
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   122
            if (pt.length) cx = parseInt(pt.shift()) || cx;
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   123
            if (pt.length) cy = parseInt(pt.shift()) || cy;
ba9d03e46925 ignore invalid #hash values for cx:cy:zl
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   124
            if (pt.length) zl = parseInt(pt.shift()) || zl;
43
fcd818eb5a71 center initial view
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   125
        }
32
aa168c7da551 implement x:y:z tuple in URL
Tero Marttila <terom@fixme.fi>
parents: 30
diff changeset
   126
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   127
        // initialize zoom state to given zl
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   128
        this._init_zoom(zl);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   129
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   130
        // initialize viewport size
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   131
        this.update_size();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   132
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   133
        // initialize scroll offset
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   134
        this._init_scroll(cx, cy);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   135
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   136
        // this comes after update_size, so that the initial update_size doesn't try and update the image_link,
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   137
        // since this only works once we have the zoom layers set up...
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   138
        this.image_link = $("lnk-image");
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   139
        this.update_image_link();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   140
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   141
        // display tiles!
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   142
        this.update_tiles();
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    },
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   144
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   145
/*
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   146
 * Initializers - only run once
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   147
 */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   148
                    
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   149
    /** Initialize the zoom state to show the given zoom level */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   150
    _init_zoom: function (zl) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   151
        // the stack of zoom levels
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   152
        this.zoom_layers = [];
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   153
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   154
        // populate the zoom-layers stack based on the number of zoom levels defined for the source
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   155
        for (var zoom_level = this.source.zoom_min; zoom_level <= this.source.zoom_max; zoom_level++) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   156
            var zoom_layer = new ZoomLayer(this.source, zoom_level);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   157
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   158
            this.substrate.appendChild(zoom_layer.div);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   159
            this.zoom_layers[zoom_level] = zoom_layer;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   160
        }
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   161
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   162
        // is the new zoom level valid?
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   163
        if (!this.zoom_layers[zl])
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   164
            // XXX: nasty, revert to something else?
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   165
            return false;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   166
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   167
        // set the zoom layyer
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   168
        this.zoom_layer = this.zoom_layers[zl];
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   169
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   170
        // enable it with initial z-index
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   171
        this.zoom_layer.enable(11);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   172
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   173
        // init the UI accordingly
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   174
        this.update_zoom_ui();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   175
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   176
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   177
    /** Initialize the scroll state to show the given (scaled) centered coordinates */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   178
    _init_scroll: function (cx, cy) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   179
        // scroll center
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   180
        this.scroll_center_to(cx, cy);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   181
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   182
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   184
/*
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   185
 * Handle input events
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   186
 */    
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   188
    /** Viewport resized */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
    on_resize: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
        this.update_size();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
        this.update_tiles();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   194
    /** Double-click to zoom and center */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
    on_dblclick: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
        var offset = this.event_offset(ev);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   198
        // center view and zoom in
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   199
        this.center_and_zoom_in(
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
            this.scroll_x + offset.x,
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   201
            this.scroll_y + offset.y
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
        );
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
    // mousewheel handler
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
    on_mousewheel: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
        // this works in very weird ways, so it's based on code from http://adomas.org/javascript-mouse-wheel/
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
        // (it didn't include any license, so this is written out manually)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
        var delta;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
        // this is browser-dependant...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
        if (ev.wheelDelta) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
            // IE + Opera
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
            delta = ev.wheelDelta;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
            if (window.opera) {  
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
                // Opera, but apparently not newer versions?
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
                //delta = -delta;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
            }
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
        } else if (ev.detail) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
            // Mozilla
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
            delta = -ev.detail;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
        } else {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
            // mousewheel not supported...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
            return;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
        }
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
        // don't scroll the page
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
        if (ev.preventDefault)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
            ev.preventDefault();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
        // delta > 0 : scroll up, zoom in
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
        // delta < 0 : scroll down, zoom out
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   237
        delta = delta < 0 ? 1 : -1;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
        // Firefox's DOMMouseEvent's pageX/Y attributes are broken. layerN is for mozilla, offsetN for IE, seems to work
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
        // absolute location of the cursor
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   242
        var x = parseInt(ev.target.style.left) + (ev.layerX ? ev.layerX : ev.offsetX);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
        var y = parseInt(ev.target.style.top) + (ev.layerY ? ev.layerY : ev.offsetY);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
        // zoom \o/
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
        this.zoom_center_to(x, y, delta);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   249
    /** Substrate scroll was started */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
    on_scroll_start: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   254
    /** Substrate was scrolled, update scroll_{x,y}, and then update tiles after 100ms */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
    on_scroll_move: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
        this.update_scroll();
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   257
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   258
        // fast-update at 100ms intervals
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   259
        this.update_after_timeout(true);
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   262
    /** Substrate scroll was ended, update tiles now */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
    on_scroll_end: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
        this.update_now();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   265
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   267
    /** Calculate the absolute (x, y) coords of the given event inside the viewport */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   268
    event_offset: function (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   269
        var offset = this.div.cumulativeOffset();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
        return {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
            x: ev.pointerX() - offset.left, 
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
            y: ev.pointerY() - offset.top
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
        };
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   276
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   277
/*
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   278
 * Change view - scroll/zoom
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   279
 */                  
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   281
    /** Scroll the view to place the given absolute (x, y) co-ordinate at the top left */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   282
    scroll_to: function (x, y) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
        // update it via the style
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   284
        this.substrate.style.top = "-" + y + "px";
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
        this.substrate.style.left = "-" + x + "px";
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   286
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   287
        // update these as well
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
        this.scroll_x = x;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
        this.scroll_y = y;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   291
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   292
    /** Scroll the view to place the given absolute (x, y) co-ordinate at the center */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
    scroll_center_to: function (x, y) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
        return this.scroll_to(
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
            x - this.center_offset_x,
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
            y - this.center_offset_y
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
        );
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
 
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   300
    /** Zoom à la delta such that the given (zoomed) absolute (x, y) co-ordinates will be at the top left */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301
    zoom_scaled: function (x, y, delta) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
        if (!this.update_zoom(delta))
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   303
            // couldn't zoom, scaled coords are wrong
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
            return false;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
        // scroll to the new position
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
        this.scroll_to(x, y);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   308
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   309
        // update view after 100ms - in case we zoom again?
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
        this.update_after_timeout();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
        
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   312
        return true;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   313
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
   
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   315
    /** Zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the top left */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   316
    zoom_to: function (x, y, delta) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   317
        return this.zoom_scaled(
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   318
            scaleByZoomDelta(x, -delta),
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   319
            scaleByZoomDelta(y, -delta),
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   320
            delta
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
        );
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   322
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   323
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   324
    /** Zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the center */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   325
    zoom_center_to: function (x, y, delta) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   326
        return this.zoom_scaled(
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   327
            scaleByZoomDelta(x, -delta) - this.center_offset_x,
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   328
            scaleByZoomDelta(y, -delta) - this.center_offset_y,
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   329
            delta
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   330
        );
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   331
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   332
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   333
    /** Zoom à la delta, keeping the view centered */
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   334
    zoom_centered: function (delta) {
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   335
        return this.zoom_center_to(
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   336
            this.scroll_x + this.center_offset_x,
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   337
            this.scroll_y + this.center_offset_y,
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   338
            delta
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   339
        );
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   340
    },
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   341
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   342
    /** Zoom in one level, keeping the view centered */
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   343
    zoom_in: function () {
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   344
        return this.zoom_centered(-1);
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   345
    },
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   346
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   347
    /** Zoom out one level, keeping the view centered */
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   348
    zoom_out: function () {
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   349
        return this.zoom_centered(+1);
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   350
    },
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   352
    /** Center the view on the given coords, and zoom in, if possible */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   353
    center_and_zoom_in: function (cx, cy) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   354
        // try and zoom in
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   355
        if (this.update_zoom(-1)) {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   356
            // scaled coords
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   357
            cx = scaleByZoomDelta(cx, 1);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   358
            cy = scaleByZoomDelta(cy, 1);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   359
        }
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   360
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   361
        // re-center
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   362
        this.scroll_center_to(cx, cy);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   363
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   364
        // update view after 100ms - in case we zoom again?
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   365
        this.update_after_timeout();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   366
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   367
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   368
/*
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   369
 * Update view state
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   370
 */              
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   371
 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   372
    /** Update the view_* / center_offset_* vars, and any dependent items */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   373
    update_size: function () {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   374
        this.view_width = this.div.getWidth();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   375
        this.view_height = this.div.getHeight();
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   376
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   377
        this.center_offset_x = Math.floor(this.view_width / 2);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   378
        this.center_offset_y = Math.floor(this.view_height / 2);
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   379
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   380
        // the link-to-image uses the current view size
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   381
        this.update_image_link();
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   382
    },
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   383
   
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   384
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   385
     * Update the scroll_x/y state
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   386
     */    
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   387
    update_scroll: function() {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   388
        this.scroll_x = -parseInt(this.substrate.style.left);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   389
        this.scroll_y = -parseInt(this.substrate.style.top);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   390
    },
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   391
    
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   392
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   393
     * Switch zoom layers
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   394
     */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   395
    update_zoom: function (delta) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   396
        // is the new zoom level valid?
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   397
        if (!this.zoom_layers[this.zoom_layer.level + delta])
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   398
            return false;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   399
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   400
        var zoom_old = this.zoom_layer;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   401
        var zoom_new = this.zoom_layers[this.zoom_layer.level + delta];
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   402
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   403
        // XXX: clear hide-zoom-after-timeout
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   404
        if (this.zoom_timer) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   405
            clearTimeout(this.zoom_timer);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   406
            this.zoom_timer = null;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   407
        }
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   408
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   409
        // get other zoom layers out of the way
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   410
        // XXX: u
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   411
        this.zoom_layers.each(function (zl) {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   412
            zl.disable();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   413
        });
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   414
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   415
        // update the current zoom layer
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   416
        this.zoom_layer = zoom_new;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   417
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   418
        // layer them such that the old on remains visible underneath the new one
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   419
        zoom_new.enable(11);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   420
        zoom_old.enable(10);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   421
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   422
        // resize the tiles in the two zoom layers
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   423
        zoom_new.update_tiles(zoom_new.level);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   424
        zoom_old.update_tiles(zoom_new.level);
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   425
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   426
        // disable the old zoom layer after 1000ms - after the new zoom layer has loaded - not an optimal solution
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   427
        this.zoom_timer = setTimeout(function () { zoom_old.disable()}, 1000);
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   428
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   429
        // update UI state
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   430
        this.update_zoom_ui();
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   431
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   432
        return true;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   433
    },
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   434
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   435
    /** Run update_tiles() at 500ms intervals */
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   436
    update_after_timeout: function (fast) {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   437
        // have not called update_tiles() yet
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   438
        this._update_idle = false;
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   439
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   440
        // cancel old timeout
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   441
        if (!fast && this._update_timeout) {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   442
            clearTimeout(this._update_timeout);
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   443
            this._update_timeout = null;
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   444
        }
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   445
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   446
        if (!this._update_timeout)
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   447
            // trigger after delay
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   448
            this._update_timeout = setTimeout(this._update_timeout_trigger.bind(this), fast ? 500 : 100);
39
eeedb6c2f7c0 zoom button UI
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
   449
    },
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   450
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   451
    _update_timeout_trigger: function () {
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   452
        this._update_timeout = null;
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   453
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   454
        // have called update_tiles()
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   455
        this._update_idle = true;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   456
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   457
        this.update_tiles();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   458
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   459
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   460
    /** 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   461
     * Unschedule the call to update_tiles() and call it now, unless it's already been triggered by the previous call to
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   462
     * update_after_timeout
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   463
     */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   464
    update_now: function () {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   465
        // abort trigger if active
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   466
        if (this._update_timeout) {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   467
            clearTimeout(this._update_timeout);
51
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   468
            this._update_timeout = null;
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   469
        }
866eb1aad566 update_tiles() every 500ms during scrolling
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   470
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   471
        // update now unless already done
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   472
        if (!this._update_idle)
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   473
            this.update_tiles();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   474
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   475
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   476
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   477
     * Determine the set of visible tiles, and ensure they are loaded
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   478
     */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   479
    update_tiles: function () {
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   480
        // short names for some vars...
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   481
        var x = this.scroll_x;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   482
        var y = this.scroll_y;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   483
        var sw = this.view_width;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   484
        var sh = this.view_height;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   485
        var tw = this.source.tile_width;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   486
        var th = this.source.tile_height;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   487
        var zl = this.zoom_layer.level;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   488
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   489
        // figure out which set of cols/rows are visible 
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   490
        var start_col = Math.max(0, Math.floor(x / tw));
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   491
        var start_row = Math.max(0, Math.floor(y / th));
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   492
        var end_col = Math.floor((x + sw) / tw);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   493
        var end_row = Math.floor((y + sh) / th);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   494
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   495
        // loop through all visible tiles
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   496
        for (var col = start_col; col <= end_col; col++) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   497
            for (var row = start_row; row <= end_row; row++) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   498
                // the tile's id
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   499
                var id = "tile_" + zl + "_" + col + "_" + row;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   500
                
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   501
                // does the element exist already?
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   502
                // XXX: use basic document.getElementById for perf?
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   503
                var t = $(id);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   504
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   505
                if (!t) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   506
                    // build a new tile
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   507
                    t = Builder.node("img", {
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   508
                            src:    this.source.build_url(col, row, zl /* , sw, sh */),
46
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   509
                            id:     id //,
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   510
                            // title:  "(" + col + ", " + row + ")",
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   511
                            // style set later
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   512
                        }
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   513
                    );
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   514
                    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   515
                    // position
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   516
                    t.style.top = th * row;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   517
                    t.style.left = tw * col;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   518
                    t.style.display = "none";
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   519
                    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   520
                    // display once loaded
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   521
                    Event.observe(t, "load", _tile_loaded.bindAsEventListener(t));
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   522
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   523
                    // remember the col/row
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   524
                    t.__col = col;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   525
                    t.__row = row;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   526
                    
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   527
                    // add it to the zoom layer
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   528
                    this.zoom_layer.add_tile(t);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   529
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   530
                } else if (this.source.reload) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   531
                    // force the tile to reload
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   532
                    touch_tile(t, col, row);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   533
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   534
                }
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   535
            }
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   536
        }
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   537
        
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   538
        this.update_scroll_ui();
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   539
    }, 
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   540
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   541
/*
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   542
 * UI state
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   543
 */                  
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   544
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   545
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   546
     * Update any zl-dependant UI elements
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   547
     */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   548
    update_zoom_ui: function () {
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   549
        // deactivate zoom-in button if zoomed in
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   550
        if (this.btn_zoom_in)
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   551
            (this.zoom_layer.level <= this.source.zoom_min) ? this.btn_zoom_in.disable() : this.btn_zoom_in.enable();
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   552
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   553
        // deactivate zoom-out button if zoomed out
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   554
        if (this.btn_zoom_out)
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   555
            (this.zoom_layer.level >= this.source.zoom_max) ? this.btn_zoom_out.disable() : this.btn_zoom_out.enable();
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   556
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   557
        // link-to-image
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   558
        this.update_image_link();
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   559
    },
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   560
 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   561
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   562
     * Update any position-dependant UI elements
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   563
     */ 
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   564
    update_scroll_ui: function () {
128
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   565
        var zl = this.zoom_layer.level;
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   566
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   567
        // update the link-to-this-page thing
128
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   568
        document.location.hash = (
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   569
                scaleByZoomDelta(this.scroll_x + this.center_offset_x, zl) 
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   570
            +   ":" 
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   571
            +   scaleByZoomDelta(this.scroll_y + this.center_offset_y, zl) 
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   572
            +   ":" 
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   573
            +   zl
66c95c2d212c partial implementation of unscaled coordinates in URLs, but broken for url hashes and view images
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   574
        );
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   575
        
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   576
        // update link-to-image
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   577
        this.update_image_link();
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   578
    },
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   579
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   580
    /**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   581
     * Update the link-to-image-of-this-view link with dimensions, zoom, position
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   582
     */
40
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   583
    update_image_link: function () {
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   584
        if (!this.image_link)
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   585
            return;
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   586
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   587
        this.image_link.href = this.source.build_image_url(
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   588
            this.scroll_x + this.center_offset_x,
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   589
            this.scroll_y + this.center_offset_y,
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   590
            this.view_width, this.view_height,
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   591
            this.zoom_layer.level
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   592
        );
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   593
5454d2e2f633 add link-to-image feature
Tero Marttila <terom@fixme.fi>
parents: 39
diff changeset
   594
        this.image_link.innerHTML = this.view_width + "x" + this.view_height + "@" + this.zoom_layer.level;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   595
    }
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   596
});
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   597
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   598
/** Used by Viewport.update_tiles to make a tile visible after it has loaded */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   599
function _tile_loaded (ev) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   600
    this.style.display = "block";
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   601
}
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   602
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   603
/**
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   604
 * A zoom layer contains a (col, row) grid of tiles at a specific zoom level. 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   605
 */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   606
var ZoomLayer = Class.create({
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   607
    initialize: function (source, zoom_level) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   608
        this.source = source;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   609
        this.level = zoom_level;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   610
        this.div = Builder.node("div", { id: "zl_" + this.level, style: "position: relative; display: none;"});
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   611
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   612
        // our tiles
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   613
        this.tiles = [];
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   614
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   615
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   616
    /** Add a tile to this zoom layer's grid */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   617
    add_tile: function (tile) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   618
        this.div.appendChild(tile);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   619
        this.tiles.push(tile);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   620
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   621
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   622
    /** Make this zoom layer visible with the given z-index */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   623
    enable: function (z_index) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   624
        this.div.style.zIndex = z_index;
46
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   625
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   626
        // XXX: IE8 needs this for some reason
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   627
        $(this.div).show();
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   628
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   629
   
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   630
    /** Hide this zoom layer */
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   631
    disable: function () {
46
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   632
        // XXX: IE8
82d7b4d64cc6 fixes for IE8
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   633
        $(this.div).hide();
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   634
    },
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   635
    
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   636
    /** 
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   637
     * Update the tile grid in this zoom layer such the tiles are in the correct position and of the correct size
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   638
     * when viewed with the given zoom level.
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   639
     *
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   640
     * For zoom levels different than this layer's level, this will resize the tiles!
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   641
     */
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   642
    update_tiles: function (zoom_level) {
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   643
        var zd = this.level - zoom_level;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   644
        
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   645
        // desired tile size
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   646
        var tw = scaleByZoomDelta(this.source.tile_width, zd);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   647
        var th = scaleByZoomDelta(this.source.tile_height, zd);
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   648
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   649
        var tiles = this.tiles;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   650
        var tiles_len = tiles.length;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   651
        var t, ts;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   652
        
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   653
        // XXX: *all* tiles? :/
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   654
        for (var i = 0; i < tiles_len; i++) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   655
            t = tiles[i];
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   656
            ts = t.style;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   657
            
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   658
            // reposition
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   659
            ts.width = tw;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   660
            ts.height = th;
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   661
            ts.top = th * t.__row;
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   662
            ts.left = tw * t.__col;
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   663
        }
45
0ce4064c428e cleanup tiles2.js, fix scaling of old layer when zooming, and have double-click always center
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   664
    }
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   665
});
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   666
127
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   667
// scale the given co-ordinate by a zoom delta. If we zoom out (dz > 0), n will become larger, and if we zoom 
df89d13f2354 invert zl meaning; zl=0 is 100%, zl=+n is zoomed out
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   668
// in (dz < 0), n will become smaller.
30
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   669
function scaleByZoomDelta (n, dz) {
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   670
    if (dz > 0)
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   671
        return n << dz;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   672
    else
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   673
        return n >> -dz;
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   674
}
53e99e552122 move the python/web code in
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   675