/**
* ownCloud - News app
*
* @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
*
*/
/**
* This file includes objects for binding and accessing the feed menu
*/
/**
* HOWTO
*
We create a new instance of the menu. Then we need to bind it on an ul which contains
all the items:
var updateIntervalMiliseconds = 2000;
var items = new News.Items('#feed_items');
var menu = new News.Menu(updateIntervalMiliseconds, items);
menu.bindOn('#feeds ul');
Updating nodes (you dont have to set all values in data):
var nodeType = News.MenuNodeType.Feed;
var nodeId = 2;
var nodeData = {
unreadCount: 4,
title: 'The verge'
}
menu.updateNode(nodeType, nodeId, nodeData);
Deleting nodes:
var id = 2;
var type = News.MenuNodeType.Feed;
var removedObject = menu.removeNode(type, id);
Creating nodes:
var parentId = 0;
var html = '<nodehtml>';
menu.addNode(parentId, html);
If you want to show all feeds, also feeds which contain only read items, use
menu.setShowAll(true);
If you want to hide feeds and folders with only read items, use
menu.setShowAll(false);
The default value is false. If you want to toggle this behaviour, theres a shortcut
menu.toggleShowAll();
To hide all articles with read feeds, the setShowAll has to be set to false. The
hiding is only triggered after a new feed/folder was being loaded. If you wish to
trigger this manually, use:
menu.triggerHideRead();
If you want to load a feed or folder directly, use
var id = 2;
var type = News.MenuNodeType.Folder;
menu.load(type, id);
*/
var News = News || {};
(function(){
/*##########################################################################
* MenuNodeType
*########################################################################*/
/**
* Enumeration for menu items
*/
MenuNodeType = {
'Feed': 0,
'Folder': 1,
'Starred': 2,
'Subscriptions': 3
};
// map css classes to MenuNodeTypes
MenuNodeTypeClass = {};
MenuNodeTypeClass[MenuNodeType.Feed] = 'feed';
MenuNodeTypeClass[MenuNodeType.Folder] = 'folder';
MenuNodeTypeClass[MenuNodeType.Starred] = 'starred';
MenuNodeTypeClass[MenuNodeType.Subscriptions] = 'subscriptions';
News.MenuNodeType = MenuNodeType;
/*##########################################################################
* Menu
*########################################################################*/
/**
* This is the basic menu used to construct and maintain the menu
* @param updateIntervalMiliseconds how often the menu should refresh
* @param items the items object
*/
Menu = function(updateIntervalMiliseconds, items){
var self = this;
this._updatingCount = 0;
this._updateInterval = updateIntervalMiliseconds;
setInterval(function(){
self._updateUnreadCountAll();
}, self._updateInterval);
this._items = items;
this._showAll = $('#view').hasClass('show_all');
this._unreadCount = {
Feed: {},
Folder: {},
Starred: 0,
Subscriptions: 0
};
};
News.Menu = Menu;
/**
* Adds a node to the menu. A node can only be added to a folder or to the root
* @param parentId the id of the parent folder, 0 for root
* @param html the html to add
*/
Menu.prototype.addNode = function(parentId, html){
parentId = parseInt(parentId);
var $parentNode;
var $html = $(html);
console.log($html);
if(parentId === 0){
$parentNode = this._$root;
} else {
$parentNode = this._getNodeFromTypeAndId(MenuNodeType.Folder, parentId).children('ul');
// every folder we add to should be opened again
$parentNode.parent().addClass('open');
$parentNode.show();
$parentNode.siblings('.collapsable_trigger').removeClass('triggered');
}
switch(this._getIdAndTypeFromNode($html).type){
case MenuNodeType.Feed:
this._bindFeed($html);
break;
case MenuNodeType.Folder:
this._bindFolder($html);
break;
}
$parentNode.append($html);
this._resetOpenFolders();
};
/**
* Updates the title and/or unread count of a node
* @param type the type (MenuNodeType)
* @param id the id
* @param data a json array with the data for the node {title: '', 'unreadCount': 3}
*/
Menu.prototype.updateNode = function(type, id, data){
var $node = this._getNodeFromTypeAndId(type, id);
id = parseInt(id);
if(data.title !== undefined){
// prevent xss
var title = $('<div>').text(data.title).html();
$node.children('.title').html(title);
}
if