Skip to content

Commit

Permalink
Resources can be loaded with ajax etc
Browse files Browse the repository at this point in the history
Resources can be loaded with ajax and resources can be arrays in event
objects.
Thanks for thebandit for the pull request.

Example options:

Resources via ajax:
resources: 'http://localhost/fullcalendar/tests/json-resources.php',

Resources as arrays in events:
events: [
{
title: 'All Day Event in resource 2 and 3',
start: new Date(y, m, 1),
resource: ['resource2','resource3']
}
],
  • Loading branch information
Jarno Kurlin committed Sep 16, 2012
1 parent d0743d3 commit 0542538
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 43 deletions.
4 changes: 4 additions & 0 deletions demos/json-resources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
header('Content-type: application/json');
echo '[{"name":"Resource 2","id":"resource2"},{"name":"Resource 1","id":"resource1"}]';
?>
13 changes: 2 additions & 11 deletions demos/resourceView.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,14 @@
minTime: 8,
maxTime:16,
selectHelper: true,
resources: [
{
name: 'Resource 2',
id: 'resource2'
},
{
name: 'Resource 1',
id: 'resource1'
}
],
resources: 'json-resources.php',
events: [
{
title: 'Lunch 12.15-14.45',
start: new Date(y, m, d, 12, 15),
end: new Date(y, m, d, 14, 45),
allDay: false,
resource: 'resource1'
resource: ['resource1','resource2']
},
{
title: 'Meeting',
Expand Down
9 changes: 8 additions & 1 deletion src/Calendar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


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


Expand All @@ -10,6 +10,7 @@ function Calendar(element, options, eventSources) {
t.destroy = destroy;
t.refetchEvents = refetchEvents;
t.reportEvents = reportEvents;
t.refetchResources = refetchResources;
t.reportEventChange = reportEventChange;
t.rerenderEvents = rerenderEvents;
t.changeView = changeView;
Expand All @@ -35,6 +36,9 @@ function Calendar(element, options, eventSources) {
var isFetchNeeded = t.isFetchNeeded;
var fetchEvents = t.fetchEvents;

// fetch resources
ResourceManager.call(t, options);
var fetchResources = t.fetchResources;

// locals
var _element = element[0];
Expand Down Expand Up @@ -326,6 +330,9 @@ function Calendar(element, options, eventSources) {
fetchEvents(currentView.visStart, currentView.visEnd); // will call reportEvents
}

function refetchResources() {
fetchResources(false);
}

// called when event data arrives
function reportEvents(_events) {
Expand Down
134 changes: 134 additions & 0 deletions src/ResourceManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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() {
alert("ajax error getting json from "+url);
},
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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
4 changes: 2 additions & 2 deletions src/resource/ResourceDayView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ResourceDayView(element, calendar) {
var opt = t.opt;
var renderBasic = t.renderBasic;
var formatDates = calendar.formatDates;

var getResources = t.getResources;


function render(date, delta) {
Expand Down Expand Up @@ -43,7 +43,7 @@ function ResourceDayView(element, calendar) {

var cols = Math.round((visEnd - visStart) / 1000 / 60 / opt('slotMinutes'));

renderBasic(opt('resources').length, opt('resources').length, cols, false);
renderBasic(getResources.length, getResources.length, cols, false);
}


Expand Down
17 changes: 12 additions & 5 deletions src/resource/ResourceEventRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function ResourceEventRenderer() {
var clearOverlays = t.clearOverlays;
var getRowCnt = t.getRowCnt;
var getColCnt = t.getColCnt;
var getResources = t.getResources;
var getViewName = t.getViewName;
var renderDaySegs = t.renderDaySegs;
var dateCell = t.dateCell;
Expand Down Expand Up @@ -61,14 +60,14 @@ function ResourceEventRenderer() {
function compileSegs(events) {
var rowCnt = getRowCnt(),
colCnt = getColCnt(),
resources = getResources(),
resources = t.getResources,
d1 = cloneDate(t.visStart),
d2 = addDays(cloneDate(d1), colCnt),
visEventsEnds = $.map(events, exclEndDay),
i, row,
j, level,
k, seg, currentResource, viewName = getViewName(),
segs=[];
l, segs=[];

if (viewName == 'resourceDay') {
d2 = cloneDate(t.visEnd);
Expand All @@ -88,9 +87,17 @@ function ResourceEventRenderer() {
seg = level[k];
seg.row = i;
seg.level = j; // not needed anymore
if(currentResource == seg['event'].resource) {
segs.push(seg);

// Let's be backwards compatitle. If event resource is not array, then we convert it.
if (!$.isArray(seg.event.resource)) {
seg.event.resource = [seg.event.resource];
}

for (l=0; l<seg.event.resource.length; l++) {
if(currentResource == seg.event.resource[l]) {
segs.push(seg);
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/resource/ResourceMonthView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ResourceMonthView(element, calendar) {
var opt = t.opt;
var renderBasic = t.renderBasic;
var formatDates = calendar.formatDates;

var getResources = t.getResources;


function render(date, delta) {
Expand Down Expand Up @@ -42,7 +42,7 @@ function ResourceMonthView(element, calendar) {
t.visStart = visStart;
t.visEnd = visEnd;
var cols = Math.round((visEnd - visStart) / (DAY_MS));
renderBasic(opt('resources').length, opt('resources').length, cols, false);
renderBasic(getResources.length, getResources.length, cols, false);
}


Expand Down
4 changes: 2 additions & 2 deletions src/resource/ResourceNextWeeksView.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function ResourceNextWeeksView(element, calendar) {
var opt = t.opt;
var renderBasic = t.renderBasic;
var formatDates = calendar.formatDates;

var getResources = t.getResources;


function render(date, delta) {
Expand All @@ -42,7 +42,7 @@ function ResourceNextWeeksView(element, calendar) {
t.end = end;
t.visStart = visStart;
t.visEnd = visEnd;
renderBasic(opt('resources').length, opt('resources').length, weekends ? opt('numberOfWeeks') * 7 : opt('numberOfWeeks') * 5, false);
renderBasic(getResources.length, getResources.length, weekends ? opt('numberOfWeeks') * 7 : opt('numberOfWeeks') * 5, false);
}


Expand Down
6 changes: 3 additions & 3 deletions src/resource/ResourceView.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function ResourceView(element, calendar, viewName) {
t.allDayBounds = allDayBounds;
t.getRowCnt = function() { return rowCnt };
t.getColCnt = function() { return colCnt };
t.getResources = function() { return opt('resources') };
t.getResources = calendar.fetchResources();
t.getColWidth = function() { return colWidth };
t.getViewName = function() { return viewName };
t.getDaySegmentContainer = function() { return daySegmentContainer };
Expand Down Expand Up @@ -67,7 +67,7 @@ function ResourceView(element, calendar, viewName) {
var viewHeight;
var colWidth;

var rowCnt, colCnt, resources;
var rowCnt, colCnt, getResources;
var coordinateGrid;
var hoverListener;
var colContentPositions;
Expand Down Expand Up @@ -126,7 +126,7 @@ function ResourceView(element, calendar, viewName) {
var contentClass = tm + "-widget-content";
var i, j, id, resourceName;
var table;
var resources = opt('resources');
var resources = t.getResources;
s =
"<table class='fc-border-separate' style='width:100%' cellspacing='0'>" +
"<thead>" +
Expand Down
4 changes: 2 additions & 2 deletions src/resource/ResourceWeekView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ResourceWeekView(element, calendar) {
var opt = t.opt;
var renderBasic = t.renderBasic;
var formatDates = calendar.formatDates;

var getResources = t.getResources;


function render(date, delta) {
Expand All @@ -39,7 +39,7 @@ function ResourceWeekView(element, calendar) {
t.end = end;
t.visStart = visStart;
t.visEnd = visEnd;
renderBasic(opt('resources').length, opt('resources').length, weekends ? 7 : 5, false);
renderBasic(getResources.length, getResources.length, weekends ? 7 : 5, false);
}


Expand Down
4 changes: 4 additions & 0 deletions tests/json-resources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
header('Content-type: application/json');
echo '[{"name":"Resource 3","id":"resource3"},{"name":"Resource 2","id":"resource2"},{"name":"Resource 1","id":"resource1"}]';
?>
17 changes: 2 additions & 15 deletions tests/resourceView.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,14 @@
minTime: 7,
maxTime:17,
selectHelper: true,
resources: [
{
name: 'Resource Name 2',
id: 'resource2'
},
{
name: 'Resource Name 1',
id: 'resource1'
},
{
name: 'Resource Name 3',
id: 'resource3'
}
],
resources: 'json-resources.php',
events: [
{
title: 'Meeting',
start: new Date(y, m, d, 10, 30),
end: new Date(y, m, d+4, 11, 00),
allDay: false,
resource: 'resource1'
resource: ['resource1','resource3']
},
{
title: 'All Day Event',
Expand Down

0 comments on commit 0542538

Please sign in to comment.