diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-02-07 00:21:02 +0100 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-02-07 00:21:02 +0100 |
commit | 467f320d98fd9279d234b9aea1b8fbf19f710900 (patch) | |
tree | 347b6edf614236e3a239b9382f89720b3215eced /coffee/lib | |
parent | 8c1ee2f48b040b8d679c2fc1d38e8b9582b6b691 (diff) |
moved from cakefile to grunt
Diffstat (limited to 'coffee/lib')
-rw-r--r-- | coffee/lib/owncloud.coffee | 29 | ||||
-rw-r--r-- | coffee/lib/services/model.coffee | 131 | ||||
-rw-r--r-- | coffee/lib/services/publisher.coffee | 54 | ||||
-rw-r--r-- | coffee/lib/services/request.coffee | 74 | ||||
-rw-r--r-- | coffee/lib/services/router.coffee | 16 |
5 files changed, 304 insertions, 0 deletions
diff --git a/coffee/lib/owncloud.coffee b/coffee/lib/owncloud.coffee new file mode 100644 index 000000000..c4cc2a336 --- /dev/null +++ b/coffee/lib/owncloud.coffee @@ -0,0 +1,29 @@ +### +# ownCloud +# +# @author Bernhard Posselt +# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +# +# This file is licensed under the Affero General Public License version 3 or later. +# See the COPYING-README file +# +### + +### +# Various config stuff for owncloud +### +angular.module('OC', []).config ['$httpProvider', ($httpProvider) -> + + # Always send the CSRF token by default + $httpProvider.defaults.get['requesttoken'] = oc_requesttoken + $httpProvider.defaults.post['requesttoken'] = oc_requesttoken + + # needed because crap PHP does not understand JSON + $httpProvider.defaults.post['Content-Type'] = 'application/x-www-form-urlencoded' + $httpProvider.defaults.get['Content-Type'] = 'application/x-www-form-urlencoded' + $httpProvider.defaults.transformRequest = (data) -> + if angular.isDefined(data) + return data + else + return $.param(data) +]
\ No newline at end of file diff --git a/coffee/lib/services/model.coffee b/coffee/lib/services/model.coffee new file mode 100644 index 000000000..a1316e3bb --- /dev/null +++ b/coffee/lib/services/model.coffee @@ -0,0 +1,131 @@ +### +# ownCloud +# +# @author Bernhard Posselt +# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +# +# This file is licensed under the Affero General Public License version 3 or later. +# See the COPYING-README file +# +### + +angular.module('OC').factory '_Model', -> + + # Parent model: inherit your model from this object + class Model + + constructor: -> + @foreignKeys = {} + @data = [] + @ids = {} + + + handle: (data) -> + if data['create'] != undefined + for item in data['create'] + @create(item) + + if data['update'] != undefined + for item in data['update'] + @update(item) + + if data['delete'] != undefined + for item in data['delete'] + @delete(item) + + + # @brief add a new foreign key name which caches data by foreign key + # @param string name: the name of the foreign key property on the object + # Foreign keys are caching items in a structure like + # name -> id -> [item1, item2] + hasForeignKey: (name) -> + @foreignKeys[name] = {} + + + # @brief adds a new object to the dataset + # @param object data: the data that we want to store + create: (data) -> + if @ids[data.id] != undefined + @update(data) + else + @data.push(data) + @ids[data.id] = data + + # fill indizes of foreign keys + for name, ids of @foreignKeys + id = data[name] + @foreignKeys[name][id] or= [] + @foreignKeys[name][id].push(data) + + + # @brief updates an existing item, the id must not change + # @param object item: the item which should be updated + update: (item) -> + currentItem = @ids[item.id] + for key, value of item + # if the foreignkey changed, we need to update the cache + if @foreignKeys[key] != undefined + if value != currentItem[key] + @_updateForeignKeyCache(key, currentItem, item) + if key != 'id' + currentItem[key] = value + + + delete: (item) -> + if @getById(item.id) != undefined + @removeById(item.id) + + + _updateForeignKeyCache: (name, currentItem, toItem) -> + fromValue = currentItem[name] + toValue = toItem[name] + foreignKeyItems = @foreignKeys[name][fromValue] + @_removeForeignKeyCacheItem(foreignKeyItems, currentItem) + @foreignKeys[name][toValue].push(item) + + + _removeForeignKeyCacheItem: (foreignKeyItems, item) -> + for fkItem, index in foreignKeyItems + if fkItem.id == id + @foreignKeys[key][item[key]].splice(index, 1) + + + # @brief removes an object + # @param int id: the id of the object + removeById: (id) -> + item = @getById(id) + + # remove from foreign key cache + for key, ids of @foreignKeys + foreignKeyItems = ids[item[key]] + @_removeForeignKeyCacheItem(foreignKeyItems, item) + + # remove from array + for item, index in @data + if item.id == id + @data.splice(index, 1) + + delete @ids[id] + + + # @brief returns a data object by its id + # @param int id: the id of the object that we want to fetch + getById: (id) -> + return @ids[id] + + + # @brief returns all stored data objects + getAll: -> + return @data + + + # @brief access the foreign key cache + # @param string foreignKeyName: the name of the foreign key that we want to + # look up + # @param string foreignKeyId: the id from that foreign key that we want to + # get + getAllOfForeignKeyWithId: (foreignKeyName, foreignKeyId) -> + return @foreignKeys[foreignKeyName][foreignKeyId] + + + return Model
\ No newline at end of file diff --git a/coffee/lib/services/publisher.coffee b/coffee/lib/services/publisher.coffee new file mode 100644 index 000000000..4d8788726 --- /dev/null +++ b/coffee/lib/services/publisher.coffee @@ -0,0 +1,54 @@ +### +# ownCloud +# +# @author Bernhard Posselt +# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +# +# This file is licensed under the Affero General Public License version 3 or later. +# See the COPYING-README file +# +### + +### +# Used for properly distributing received model data from the server +### +angular.module('OC').factory '_Publisher', -> + + + class Publisher + + constructor: -> + @subscriptions = {} + + + # Use this to subscribe to a certain hashkey in the returned json data + # dictionary. + # If you send JSON from the server, you'll receive something like this + # + # { + # data: { + # modelName: { + # create: [{id: 1, name: 'john'}, {id: 2, name: 'ron'}], + # update: [], + # delete: [] + # } + # } + # } + # + # To get the array ['one', 'two'] passed to your model, just subscribe + # to the key: + # Publisher.subscribeModelTo('modelName', myModelInstance) + # + subscribeModelTo: (model, name) -> + @subscriptions[name] or= [] + @subscriptions[name].push(model) + + + # This will publish data from the server to all registered subscribers + # The parameter 'name' is the name under which subscribers have registered + publishDataTo: (data, name) -> + for subscriber in @subscriptions[name] || [] + subscriber.handle(data) + + + return Publisher
\ No newline at end of file diff --git a/coffee/lib/services/request.coffee b/coffee/lib/services/request.coffee new file mode 100644 index 000000000..15b4b9013 --- /dev/null +++ b/coffee/lib/services/request.coffee @@ -0,0 +1,74 @@ +### +# ownCloud +# +# @author Bernhard Posselt +# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +# +# This file is licensed under the Affero General Public License version 3 or later. +# See the COPYING-README file +# +### + +angular.module('OC').factory '_Request', -> + + class Request + + constructor: (@_$http, @_$rootScope, @_publisher, @_token, @_router) -> + @_initialized = false + @_shelvedRequests = [] + + @_$rootScope.$on 'routesLoaded', => + @_executeShelvedRequests() + @_initialized = true + @_shelvedRequests = [] + + + request: (route, routeParams={}, data={}, onSuccess=null, onFailure=null, config={}) -> + # if routes are not ready yet, save the request + if not @_initialized + @_shelveRequest(route, routeParams, data, method, config) + return + + url = @_router.generate(route, routeParams) + + defaultConfig = + method: 'GET' + url: url + data: data + + # overwrite default values from passed in config + for key, value of config + defaultConfig[key] = value + + @_$http(config) + .success (data, status, headers, config) => + if onSuccess + onSuccess(data, status, headers, config) + + # publish data to models + for name, value of data.data + @publisher.publishDataTo(name, value) + + .error (data, status, headers, config) -> + if onFailure + onFailure(data, status, headers, config) + + + _shelveRequest: (route, routeParams, data, method, config) -> + request = + route: route + routeParams: routeParams + data: data + config: config + method: method + + @_shelvedRequests.push(request) + + + _executeShelvedRequests: -> + for req in @_shelvedRequests + @post(req.route, req.routeParams, req.data, req.method, req.config) + + + + return Request diff --git a/coffee/lib/services/router.coffee b/coffee/lib/services/router.coffee new file mode 100644 index 000000000..71ed24332 --- /dev/null +++ b/coffee/lib/services/router.coffee @@ -0,0 +1,16 @@ +### +# ownCloud +# +# @author Bernhard Posselt +# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +# +# This file is licensed under the Affero General Public License version 3 or later. +# See the COPYING-README file +# +### + +### +# Inject router into angular to make testing easier +### +angular.module('OC').factory 'Router', -> + return OC.Router
\ No newline at end of file |