~keith-hughitt/helioviewer.org/2.0

« back to all changes in this revision

Viewing changes to src/Tiling/LayerManager.js

  • Committer: Keith Hughitt
  • Date: 2010-04-17 16:54:20 UTC
  • Revision ID: keith.hughitt@nasa.gov-20100417165420-z3dpv7j9hul14785
Helioviewer.orgĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @fileOverview Contains class definition for a simple layer manager
 
3
 * @author <a href="mailto:keith.hughitt@nasa.gov">Keith Hughitt</a>
 
4
 */
 
5
/*jslint browser: true, white: true, onevar: true, undef: true, nomen: false, eqeqeq: true, plusplus: true, 
 
6
bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxlen: 120, sub: true */
 
7
/*global Class, $ */
 
8
"use strict";
 
9
var LayerManager = Class.extend(
 
10
    /** @lends LayerManager.prototype */
 
11
    {
 
12
    /**
 
13
     * @constructs
 
14
     * @description Creates a new LayerManager
 
15
     * @param {Object} A Rseference to the main application controller
 
16
     */
 
17
    init: function (controller) {
 
18
        this.controller = controller;
 
19
        this._layers    = [];
 
20
    },
 
21
 
 
22
    /**
 
23
     * @description Add a new layer
 
24
     */
 
25
    addLayer: function (layer) {
 
26
        this._layers.push(layer);
 
27
    },
 
28
   
 
29
    /**
 
30
     * @description Gets the number of layers currently loaded 
 
31
     * @return {Integer} Number of layers present.
 
32
     */
 
33
    size: function () {
 
34
        return this._layers.length;
 
35
    },
 
36
    
 
37
    /**
 
38
     * Returns the index of the given layer if it exists, and -1 otherwise
 
39
     */
 
40
    indexOf: function (id) {
 
41
        var index = -1;
 
42
        
 
43
        $.each(this._layers, function (i, item) {
 
44
            if (item.id === id) {
 
45
                index = i;
 
46
            }
 
47
        });
 
48
        
 
49
        return index;
 
50
    },
 
51
    
 
52
    /**
 
53
     * @description Returns the largest width and height of any layers (does not have to be from same layer)
 
54
     * @return {Object} The width and height of the largest layer
 
55
     */
 
56
    getMaxDimensions: function () {
 
57
        var maxLeft   = 0,
 
58
            maxTop    = 0,
 
59
            maxBottom = 0,
 
60
            maxRight  = 0;
 
61
        
 
62
        $.each(this._layers, function () {
 
63
            var d = this.getDimensions();
 
64
            
 
65
            maxLeft   = Math.max(maxLeft, d.left);
 
66
            maxTop    = Math.max(maxTop, d.top);
 
67
            maxBottom = Math.max(maxBottom, d.bottom);
 
68
            maxRight  = Math.max(maxRight, d.right);
 
69
        });
 
70
        
 
71
        return {width: maxLeft + maxRight, height: maxTop + maxBottom};
 
72
    },
 
73
 
 
74
    /**
 
75
     * @description Removes a layer
 
76
     * @param {Object} The layer to remove
 
77
     */
 
78
    removeLayer: function (layer) {
 
79
        layer.domNode.remove();
 
80
        this._layers = $.grep(this._layers, function (e, i) {
 
81
            return (e.id !== layer.id);
 
82
        });
 
83
        layer = null;
 
84
        this.controller.viewport.updateSandbox();
 
85
    },
 
86
    
 
87
    /**
 
88
     * @description Reload layers (For tile layers, finds closest image)
 
89
     */
 
90
    reloadLayers: function () {
 
91
        $.each(this._layers, function () {
 
92
            this.reload();
 
93
        });
 
94
    },
 
95
 
 
96
    /**
 
97
     * @description Resets each of the layers
 
98
     */
 
99
    resetLayers: function () {
 
100
        $.each(this._layers, function () {
 
101
            this.reset(true);
 
102
        });
 
103
    },
 
104
    
 
105
    /**
 
106
     * @description Iterates through layers
 
107
     */
 
108
    each: function (fn) {
 
109
        $.each(this._layers, fn);
 
110
    },
 
111
    
 
112
    /**
 
113
     * @description Returns a string representation of the layers currently being displayed
 
114
     */
 
115
    serialize: function () {
 
116
        var layers = "";
 
117
 
 
118
        $.each(this._layers, function () {
 
119
            layers += "[" + this.serialize() + "],";
 
120
        });
 
121
        
 
122
        // Remove trailing comma
 
123
        layers = layers.slice(0, -1);
 
124
        
 
125
        return layers;
 
126
    },
 
127
    
 
128
    /**
 
129
     * @description Returns a JSON representation of the layers currently being displayed
 
130
     */
 
131
    toJSON: function () {
 
132
        var layers = [];
 
133
        
 
134
        $.each(this._layers, function () {
 
135
            layers.push(this.toJSON());
 
136
        });
 
137
        
 
138
        return layers;       
 
139
    }
 
140
});