Skip to content

Commit

Permalink
added support for json resource feed
Browse files Browse the repository at this point in the history
  • Loading branch information
ikelin committed May 8, 2012
1 parent b6810b3 commit 261a653
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.mkd
Expand Up @@ -47,4 +47,4 @@ Here is an example of doing it within an element having an `id` of `calendar`:
To see a full list of all available options, please consult the [FullCalendar documentation »](http://arshaw.com/fullcalendar/docs/)

*Note* This particular git fork adds a resourceDay view that shows resources horizontally and
each resource's calendar vertically. See `demos/resorue-views.html` for usage.
each resource's calendar vertically. See `demos/resource-views.html` and `demos/json-resource-views.html` for usage.
17 changes: 2 additions & 15 deletions demos/json-resource-views.html
Expand Up @@ -52,22 +52,9 @@
console.log("@@ drag/drop event " + event.title + ", start " + event.start + ", end " + event.end + ", resource " + event.resourceId);
},
editable: true,
resources: [
{
'id': 1,
'name': 'resource1'
},
{
'id': 2,
'name': 'resource2'
},
{
'id': 3,
'name': 'resource3'
}
],
resources: 'json-resources.php',
events: 'json-resource-events.php'
});
});
});

</script>
Expand Down
22 changes: 22 additions & 0 deletions demos/json-resources.php
@@ -0,0 +1,22 @@
<?php

echo json_encode(array(

array(
'id' => 1,
'name' => "JSON Resource 1"
),

array(
'id' => 2,
'name' => "JSON Resource 2"
),

array(
'id' => 3,
'name' => "JSON Resource 3"
)

));

?>
7 changes: 4 additions & 3 deletions src/Calendar.js
@@ -1,6 +1,6 @@


function Calendar(element, options, eventSources) {
function Calendar(element, options, eventSources, resourceSources) {
var t = this;


Expand Down Expand Up @@ -34,7 +34,8 @@ function Calendar(element, options, eventSources) {
EventManager.call(t, options, eventSources);
var isFetchNeeded = t.isFetchNeeded;
var fetchEvents = t.fetchEvents;


ResourceManager.call(t, options);

// locals
var _element = element[0];
Expand All @@ -52,7 +53,7 @@ function Calendar(element, options, eventSources) {
var date = new Date();
var events = [];
var _dragElement;


/* Main Rendering
Expand Down
136 changes: 136 additions & 0 deletions src/ResourceManager.js
@@ -0,0 +1,136 @@
/*
* Responsible for resources. Resource source object is anything that provides
* data about resources. It can be function, a JSON object or URL to a JSON
* feed.
*/


function ResourceManager(options) {
var t = this;

// exports
t.fetchResources = fetchResources;

// local
var sources = []; // source array
var cache; // cached resources

_addResourceSources(options['resources']);


/**
* ----------------------------------------------------------------
* Categorize and add the provided sources
* ----------------------------------------------------------------
*/
function _addResourceSources(_sources) {
var source = {};

if ($.isFunction(_sources)) {
// is it a function?
source = {
resources: _sources
};
sources.push(source);
} else if (typeof _sources == 'string') {
// is it a URL string?
source = {
url: _sources
};
sources.push(source);
} else if (typeof _sources == 'object') {
// is it json object?
for (var i=0; i<_sources.length; i++) {
var s = _sources[i];
normalizeSource(s);
source = {
resources: s
};
sources.push(source);
}
}
}


/**
* ----------------------------------------------------------------
* Fetch resources from source array
* ----------------------------------------------------------------
*/
function fetchResources(useCache) {
// if useCache is not defined, default to true
useCache = typeof useCache !== 'undefined' ? useCache : true;

if (cache != undefined && useCache) {
// get from cache
return cache;
} else {
// do a fetch resource from source, rebuild cache
cache = [];
var len = sources.length;
for (var i = 0; i < len; i++) {
var resources = _fetchResourceSource(sources[i]);
cache = cache.concat(resources);
}
return cache;
}
}


/**
* ----------------------------------------------------------------
* Fetch resources from each source. If source is a function, call
* the function and return the resource. If source is a URL, get
* the data via synchronized ajax call. If the source is an
* object, return it as is.
* ----------------------------------------------------------------
*/
function _fetchResourceSource(source) {
var resources = source.resources;
if (resources) {
if ($.isFunction(resources)) {
return resources();
}
} else {
var url = source.url;
if (url) {
$.ajax({
url: url,
dataType: 'json',
cache: false,
success: function(res) {
res = res || [];
resources = res;
},
error: function() {
console.error("ajax error");
},
async: false // too much work coordinating callbacks so dumb it down
});
}
}
return resources;
}


/**
* ----------------------------------------------------------------
* normalize the source object
* ----------------------------------------------------------------
*/
function normalizeSource(source) {
if (source.className) {
if (typeof source.className == 'string') {
source.className = source.className.split(/\s+/);
}
}else{
source.className = [];
}
var normalizers = fc.sourceNormalizers;
for (var i=0; i<normalizers.length; i++) {
normalizers[i](source);
}
}


}
1 change: 1 addition & 0 deletions src/_loader.js
Expand Up @@ -42,6 +42,7 @@ js('main.js');
js('Calendar.js');
js('Header.js');
js('EventManager.js');
js('ResourceManager.js');
js('date_util.js');
js('util.js');

Expand Down
17 changes: 5 additions & 12 deletions src/resource/ResourceView.js
Expand Up @@ -59,8 +59,8 @@ function ResourceView(element, calendar, viewName) {
t.dragStart = dragStart;
t.dragStop = dragStop;
t.resourceCol = resourceCol;
t.resources = getResources();
t.resources = fetchResources();

// imports
View.call(t, element, calendar, viewName);
Expand All @@ -77,7 +77,7 @@ function ResourceView(element, calendar, viewName) {
//var daySelectionMousedown = t.daySelectionMousedown; // redefine here
var slotSegHtml = t.slotSegHtml;
var formatDate = calendar.formatDate;

// locals

Expand Down Expand Up @@ -124,15 +124,15 @@ function ResourceView(element, calendar, viewName) {
var minMinute, maxMinute;
var colFormat;
var resources = t.resources;


/* Rendering
-----------------------------------------------------------------------------*/


disableTextSelection(element.addClass('fc-agenda'));


function renderResource() {
colCnt = resources.length;
Expand Down Expand Up @@ -826,11 +826,4 @@ function ResourceView(element, calendar, viewName) {
}
}


/* get calendar resources
--------------------------------------------------------------------------------*/

function getResources() {
return calendar.options['resources'];
}
}

0 comments on commit 261a653

Please sign in to comment.