~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to src/js/Media/MediaManager.js

  • Committer: Michael Lynch
  • Date: 2008-02-01 06:21:07 UTC
  • Revision ID: mike@mike-desktop-20080201062107-uhqip0rcpsfc91mq
Initial import

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 = 25;
21
 
        } else {
22
 
            this._historyLimit = 10;
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, currentGroup, name = "";
34
 
 
35
 
        layerArray = layerStringToLayerArray(layerString).sort();
36
 
 
37
 
        $.each(layerArray, function (i, layer) {
38
 
            layer = extractLayerName(this);
39
 
 
40
 
            // Add instrument or detector if its not already present, otherwise
41
 
            // add a backslash and move onto the right-hand side
42
 
            if (currentGroup === layer[1] || currentGroup === layer[2]) {
43
 
                name += "/";
44
 
            } else {
45
 
                // For STEREO use detector for the Left-hand side
46
 
                if (layer[1] === "SECCHI") {
47
 
                    currentGroup = layer[2];
48
 
                    // Add "A" or "B" to differentiate spacecraft
49
 
                    name += ", " + layer[2] + "-" +
50
 
                            layer[0].substr(-1) + " ";
51
 
                } else {
52
 
                    // Otherwise use the instrument name
53
 
                    currentGroup = layer[1];
54
 
                    name += ", " + layer[1] + " ";
55
 
                }
56
 
            }
57
 
 
58
 
            // For LASCO, use the detector for the right-hand side
59
 
            if (layer[1] === "LASCO") {
60
 
                name += layer[2];
61
 
            } else if (layer[2].substr(0, 3) === "COR") {
62
 
                // For COR1 & 2 images
63
 
 
64
 
            } else {
65
 
                // For AIA
66
 
                name += layer[2];
67
 
            }
68
 
        });
69
 
 
70
 
        return name.slice(2); // Get rid of the extra ", " at the front
71
 
    },
72
 
 
73
 
    /**
74
 
     * Adds an item
75
 
     */
76
 
    add: function (item) {
77
 
        if (this._history.unshift(item) > this._historyLimit) {
78
 
            this._history = this._history.slice(0, this._historyLimit);
79
 
        }
80
 
 
81
 
        this._save();
82
 
    },
83
 
 
84
 
    /**
85
 
     * Returns the item with the specified id if it exists
86
 
     */
87
 
    get: function (id) {
88
 
        var index = null;
89
 
 
90
 
        // Find the index in the history array
91
 
        $.each(this._history, function (i, item) {
92
 
            if (item.id === id) {
93
 
                index = i;
94
 
            }
95
 
        });
96
 
 
97
 
        return this._history[index];
98
 
    },
99
 
 
100
 
    /**
101
 
     * Removes all items
102
 
     */
103
 
    empty: function () {
104
 
        var self = this;
105
 
 
106
 
        $.each(this._history, function (i, item) {
107
 
            self._history[i] = null;
108
 
        });
109
 
 
110
 
        self._history = [];
111
 
        self._save();
112
 
    },
113
 
 
114
 
    /**
115
 
     * Check to see if an entry exists
116
 
     */
117
 
    has: function (id) {
118
 
        var exists = false;
119
 
 
120
 
        $.each(this._history, function (i, item) {
121
 
            if (item.id === id) {
122
 
                exists = true;
123
 
            }
124
 
        });
125
 
 
126
 
        return exists;
127
 
    },
128
 
 
129
 
    /**
130
 
     * Removes a item
131
 
     *
132
 
     * @param {String} id Item to be removed
133
 
     */
134
 
    remove: function (id) {
135
 
        var self = this;
136
 
 
137
 
        $.each(this._history, function (i, item) {
138
 
            if (item.id === id) {
139
 
                self._history[i] = null;
140
 
                self._history.splice(i, 1);
141
 
                self._save();
142
 
                return;
143
 
            }
144
 
        });
145
 
    },
146
 
 
147
 
    /**
148
 
     * Returns an array containing objects for the items currently being tracked
149
 
     */
150
 
    toArray: function () {
151
 
        return $.extend([], this._history);
152
 
    }
153
 
});