~keith-hughitt/helioviewer.org/2.0

« back to all changes in this revision

Viewing changes to src/Media/MediaManager.js

  • Committer: Keith Hughitt
  • Date: 2011-12-02 19:39:20 UTC
  • Revision ID: keith.hughitt@nasa.gov-20111202193920-29g5cn177se3vfph
Helioviewer.org 2.2.2 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * MediaManager class definition
 
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: false, strict: true, newcap: true, immed: true, maxlen: 120, sub: true */
 
7
/*global Class, $, setTimeout, window, Media, extractLayerName, layerStringToLayerArray */
 
8
"use strict";
 
9
var MediaManager = Class.extend(
 
10
    /** @lends MediaManager.prototype */
 
11
    {
 
12
    /**
 
13
     * @constructs
 
14
     * @param {Array} history Items saved in the user's history
 
15
     */    
 
16
    init: function (savedItems) {
 
17
        this._history = savedItems;
 
18
        
 
19
        if ($.support.localStorage) {
 
20
            this._historyLimit = 20;    
 
21
        } else {
 
22
            this._historyLimit = 5;
 
23
        }
 
24
        
 
25
    },
 
26
    
 
27
    /**
 
28
     * Creates the name that will be displayed in the history.
 
29
     * Groups layers together by detector, e.g. "EIT 171/304, LASCO C2/C3"
 
30
     * Will crop names that are too long and append ellipses.
 
31
     */
 
32
    _getName: function (layerString) {
 
33
        var layer, layerArray, observatory, instrument, detector, measurement, 
 
34
            currentGroup, name = "";
 
35
        
 
36
        layerArray = layerStringToLayerArray(layerString).sort();
 
37
        
 
38
        $.each(layerArray, function (i, layer) {
 
39
            layer = extractLayerName(this);
 
40
            
 
41
            observatory = layer[0];
 
42
            instrument  = layer[1];
 
43
            detector    = layer[2];
 
44
            measurement = layer[3];
 
45
 
 
46
            // Add instrument or detector if its not already present, otherwise
 
47
            // add a backslash and move onto the right-hand side
 
48
            if (currentGroup === instrument || currentGroup === detector) {
 
49
                name += "/";
 
50
            } else {
 
51
                // For STEREO use detector for the Left-hand side
 
52
                if (instrument === "SECCHI") {
 
53
                    currentGroup = detector;
 
54
                    // Add "A" or "B" to differentiate spacecraft
 
55
                    name += ", " + detector + "-" + 
 
56
                            observatory.substr(-1) + " ";
 
57
                } else {
 
58
                    // Otherwise use the instrument name
 
59
                    currentGroup = instrument;
 
60
                    name += ", " + instrument + " ";
 
61
                }
 
62
            }
 
63
 
 
64
            // For LASCO, use the detector for the right-hand side
 
65
            if (instrument === "LASCO") {
 
66
                name += detector;
 
67
            } else if (detector.substr(0, 3) === "COR") {
 
68
                // For COR1 & 2 images
 
69
                
 
70
            } else {
 
71
                name += measurement;
 
72
            }
 
73
        });
 
74
        
 
75
        return name.slice(2); // Get rid of the extra ", " at the front
 
76
    },
 
77
    
 
78
    /**
 
79
     * Adds an item
 
80
     */
 
81
    add: function (item) {
 
82
        if (this._history.unshift(item) > this._historyLimit) {
 
83
            this._history = this._history.slice(0, this._historyLimit);            
 
84
        }
 
85
 
 
86
        this._save();  
 
87
    },
 
88
    
 
89
    /**
 
90
     * Returns the item with the specified id if it exists
 
91
     */
 
92
    get: function (id) {
 
93
        var index = null;
 
94
 
 
95
        // Find the index in the history array
 
96
        $.each(this._history, function (i, item) {
 
97
            if (item.id === id) {
 
98
                index = i;
 
99
            }
 
100
        });
 
101
 
 
102
        return this._history[index];
 
103
    },
 
104
    
 
105
    /**
 
106
     * Removes all items
 
107
     */
 
108
    empty: function () {
 
109
        var self = this;
 
110
 
 
111
        $.each(this._history, function (i, item) {
 
112
            self._history[i] = null;
 
113
        });
 
114
        
 
115
        self._history = [];
 
116
        self._save();
 
117
    },
 
118
    
 
119
    /**
 
120
     * Check to see if an entry exists
 
121
     */
 
122
    has: function (id) {
 
123
        var exists = false;
 
124
 
 
125
        $.each(this._history, function (i, item) {
 
126
            if (item.id === id) {
 
127
                exists = true;
 
128
            }
 
129
        });
 
130
        
 
131
        return exists;
 
132
    },
 
133
    
 
134
    /**
 
135
     * Removes a item
 
136
     * 
 
137
     * @param {String} id Item to be removed
 
138
     */
 
139
    remove: function (id) {
 
140
        var self = this;
 
141
 
 
142
        $.each(this._history, function (i, item) {
 
143
            if (item.id === id) {
 
144
                self._history[i] = null;
 
145
                self._history.splice(i, 1);
 
146
                self._save();
 
147
                return;
 
148
            }
 
149
        });
 
150
    },
 
151
 
 
152
    /**
 
153
     * Returns an array containing objects for the items currently being tracked
 
154
     */
 
155
    toArray: function () {
 
156
        return $.extend([], this._history);
 
157
    }
 
158
});