summaryrefslogtreecommitdiffstats
path: root/js/build/app/services/models/itemmodel.js
blob: 3f2e996a7aa9aa7070b72f33518c43119b8756da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Generated by CoffeeScript 1.6.3
/*

ownCloud - News

@author Bernhard Posselt
@copyright 2012 Bernhard Posselt dev@bernhard-posselt.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
License as published by the Free Software Foundation; either
version 3 of the License, or any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU AFFERO GENERAL PUBLIC LICENSE for more details.

You should have received a copy of the GNU Affero General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/


(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  angular.module('News').factory('ItemModel', [
    '_Model', '_MinimumQuery', 'StatusFlag', function(_Model, _MinimumQuery, StatusFlag) {
      var ItemModel;
      ItemModel = (function(_super) {
        __extends(ItemModel, _super);

        function ItemModel() {
          this._guidFeedIdHash = {};
          ItemModel.__super__.constructor.call(this);
        }

        ItemModel.prototype.clear = function() {
          this._guidFeedIdHash = {};
          return ItemModel.__super__.clear.call(this);
        };

        ItemModel.prototype.add = function(data, clearCache) {
          var entry, hash;
          if (clearCache == null) {
            clearCache = true;
          }
          this._bindMethods(data);
          hash = data.feedId + '_' + data.guidHash;
          entry = this._guidFeedIdHash[hash];
          if (angular.isDefined(entry)) {
            return this.update(data, clearCache);
          } else {
            this._guidFeedIdHash[hash] = data;
            return ItemModel.__super__.add.call(this, data, clearCache);
          }
        };

        ItemModel.prototype._bindMethods = function(data) {
          data.isRead = function() {
            return !((this.status & StatusFlag.UNREAD) === StatusFlag.UNREAD);
          };
          data.setRead = function() {
            return this.status &= ~StatusFlag.UNREAD;
          };
          data.setUnread = function() {
            return this.status |= StatusFlag.UNREAD;
          };
          data.isStarred = function() {
            return (this.status & StatusFlag.STARRED) === StatusFlag.STARRED;
          };
          data.setStarred = function() {
            return this.status |= StatusFlag.STARRED;
          };
          return data.setUnstarred = function() {
            return this.status &= ~StatusFlag.STARRED;
          };
        };

        ItemModel.prototype.update = function(data, clearCache) {
          var entry, hash, key, value;
          if (clearCache == null) {
            clearCache = true;
          }
          hash = data.feedId + '_' + data.guidHash;
          entry = this._guidFeedIdHash[hash];
          delete this._dataMap[entry.id];
          this._dataMap[data.id] = entry;
          for (key in data) {
            value = data[key];
            if (key === 'feedId' || key === 'guidHash') {
              continue;
            } else {
              entry[key] = value;
            }
          }
          return ItemModel.__super__.update.call(this, entry, clearCache);
        };

        ItemModel.prototype.getByGuidHashAndFeedId = function(guidHash, feedId) {
          var hash;
          hash = feedId + '_' + guidHash;
          return this._guidFeedIdHash[hash];
        };

        ItemModel.prototype.removeById = function(id) {
          var hash, item;
          item = this.getById(id);
          hash = item.feedId + '_' + item.guidHash;
          delete this._guidFeedIdHash[hash];
          return ItemModel.__super__.removeById.call(this, id);
        };

        ItemModel.prototype.getLowestId = function() {
          var lowestId, query;
          query = new _MinimumQuery('id');
          lowestId = this.get(query);
          if (angular.isDefined(lowestId)) {
            return lowestId.id;
          } else {
            return 0;
          }
        };

        return ItemModel;

      })(_Model);
      return new ItemModel();
    }
  ]);

}).call(this);