summaryrefslogtreecommitdiffstats
path: root/js/build/app.js
blob: 3a72dc27749e58435dd7b4da9c1c2a8d95d4da05 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(function(angular, $, OC, undefined){

'use strict';


var app = angular.module('News', [
    'ngRoute',
    'ngSanitize',
    'ngAnimate'
  ]);
app.config([
  '$routeProvider',
  '$provide',
  function ($routeProvider, $provide) {
    'use strict';
    $provide.constant('baseUrl', OC.generateUrl(''));
    $routeProvider.when('/items', {
      controller: 'AllItemsController',
      templateUrl: 'content.html',
      resolve: {}
    }).when('/items/starred', {
      controller: 'StarredItemsController',
      templateUrl: 'content.html',
      resolve: {}
    }).when('/items/feeds/:id', {
      controller: 'FeedItemsController',
      templateUrl: 'content.html',
      resolve: {}
    }).when('/items/folders/:id', {
      controller: 'FolderItemsController',
      templateUrl: 'content.html',
      resolve: {}
    }).otherwise({ redirectTo: '/items' });
  }
]);
app.run([
  '$rootScope',
  '$location',
  'Loading',
  'Setup',
  'Item',
  'Feed',
  'Folder',
  'Publisher',
  'Settings',
  function ($rootScope, $location, Loading, Setup, Item, Feed, Folder, Publisher, Settings) {
    'use strict';
    // listen to keys in returned queries to automatically distribute the
    // incoming values to models
    Publisher.subscribe(Item).toChannel('items');
    Publisher.subscribe(Folder).toChannel('folders');
    Publisher.subscribe(Feed).toChannel('feeds');
    Publisher.subscribe(Settings).toChannel('settings');
    // load feeds, settings and last read feed
    Setup.load();
    $rootScope.$on('$routeChangeStart', function () {
      Loading.setLoading('content', true);
    });
    $rootScope.$on('$routeChangeSuccess', function () {
      Loading.setLoading('content', false);
    });
    // in case of wrong id etc show all items
    $rootScope.$on('$routeChangeError', function () {
      $location.path('/items');
    });
  }
]);
app.factory('Feed', [
  'Model',
  function (Model) {
    'use strict';
    var Feed = function () {
      Model.call(this, 'url');
    };
    Feed.prototype = Object.create(Model.prototype);
    return new Feed();
  }
]);
app.factory('Folder', [
  'Model',
  function (Model) {
    'use strict';
    var Folder = function () {
      Model.call(this, 'name');
    };
    Folder.prototype = Object.create(Model.prototype);
    return new Folder();
  }
]);
app.factory('Item', [
  'Model',
  function (Model) {
    'use strict';
    var Item = function () {
      Model.call(this, 'id');
    };
    Item.prototype = Object.create(Model.prototype);
    return new Item();
  }
]);
app.service('Loading', function () {
  'use strict';
  this.loading = {
    global: false,
    content: false
  };
  this.setLoading = function (area, isLoading) {
    this.loading[area] = isLoading;
  };
  this.isLoading = function (area) {
    return this.loading[area];
  };
});
app.factory('Model', function () {
  'use strict';
  var Model = function (id) {
    this.id = id;
    this.values = [];
    this.hashMap = {};
  };
  Model.prototype = {
    receive: function (values) {
      var self = this;
      values.forEach(function (value) {
        self.add(value);
      });
    },
    add: function (value) {
      var key, existing;
      existing = this.hashMap[value[this.id]];
      if (existing === undefined) {
        this.values.push(value);
        this.hashMap[value[this.id]] = value;
      } else {
        // copy values from new to old object if it exists already
        for (key in value) {
          if (value.hasOwnProperty(key)) {
            existing[key] = value[key];
          }
        }
      }
    },
    size: function () {
      return this.values.length;
    },
    get: function (id) {
      return this.hashMap[id];
    },
    delete: function (id) {
      // find index of object that should be deleted
      var i, deleteAtIndex;
      for (i = 0; i < this.values.length; i += 1) {
        if (this.values[i][this.id] === id) {
          deleteAtIndex = i;
          break;
        }
      }
      if (deleteAtIndex !== undefined) {
        this.values.splice(deleteAtIndex, 1);
      }
      if (this.hashMap[id] !== undefined) {
        delete this.hashMap[id];
      }
    },
    clear: function () {
      this.hashMap = {};
      // http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript
      // this is the fastes way to empty an array when you want to keep the
      // reference around
      while (this.values.length > 0) {
        this.values.pop();
      }
    }
  };
  return Model;
});
app.service('Publisher', function () {
  'use strict';
  var self = this;
  this.channels = {};
  this.subscribe = function (object) {
    return {
      toChannel: function (channel) {
        self.channels[channel] = self.channels[channel] || [];
        self.channels[channel].push(object);
      }
    };
  };
  this.publishAll = function (data) {
    var channel, counter;
    for (channel in data) {
      if (data.hasOwnProperty(channel)) {
        for (counter = 0; counter < this.channels[channel].length; counter += 1) {
          this.channels[channel][counter].receive(data[channel]);
        }
      }
    }
  };
});
app.service('Setup', function () {
  'use strict';
  this.load = function () {
    console.log('init');
  };
});

})(angular, jQuery, OC);