oxjs/source/UI/js/Calendar/CalendarEditor.js

833 lines
28 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-10-03 16:14:01 +00:00
/*@
Ox.CalendarEditor <f> Calendar Editor
([options[, self]]) -> <o:Ox.SplitPanel> Calendar Editor
options <o> Options
self <o> Shared private variable
2012-06-17 22:38:26 +00:00
loadlist <!> loadlist
2011-10-03 16:14:01 +00:00
@*/
2012-04-09 08:42:00 +00:00
Ox.CalendarEditor = function(options, self) {
2011-10-03 16:14:01 +00:00
self = self || {};
var that = Ox.Element({}, self)
.defaults({
2011-10-09 21:13:16 +00:00
addEvent: null,
2012-02-20 18:31:45 +00:00
collapsible: false,
editEvent: null,
2011-10-03 16:37:05 +00:00
events: [],
2012-02-20 18:31:45 +00:00
hasMatches: false,
2011-10-03 16:14:01 +00:00
height: 256,
2012-02-20 18:31:45 +00:00
mode: 'add',
2011-10-03 16:14:01 +00:00
pageLength: 100,
2012-02-20 18:31:45 +00:00
removeEvent: null,
selected: '',
2011-10-31 12:45:08 +00:00
showControls: false,
2011-10-03 16:37:05 +00:00
sort: [{key: 'name', operator: '+'}],
2011-10-03 16:14:01 +00:00
width: 256
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
height: function() {
// fixme: should be .resizeList
self.$list.size();
self.$calendar.resizeCalendar();
},
width: function() {
self.$calendar.resizeCalendar();
}
})
2011-10-03 16:14:01 +00:00
.css({
width: self.options.width + 'px',
height: self.options.height + 'px'
});
self.durationCache = {};
2012-02-20 18:31:45 +00:00
self.durationSize = {
86400: 10,
31622400: 12
};
2011-10-03 16:14:01 +00:00
self.columns = [
{
2012-02-20 18:31:45 +00:00
format: function(value, data) {
var eventDuration = (Ox.parseDate(data.end) - Ox.parseDate(data.start)) / 1000,
iconSize = 8;
Ox.forEach(self.durationSize, function(size, duration) {
if (eventDuration > duration) {
2012-02-20 18:31:45 +00:00
iconSize = size;
} else {
2012-07-05 08:58:08 +00:00
return false; // break
2012-02-20 18:31:45 +00:00
}
});
return data.type
? $('<div>')
.addClass('OxEvent Ox' + Ox.toTitleCase(data.type))
.css({
width: iconSize + 'px',
height: iconSize + 'px',
margin: [0, 0, 0, -3].map(function(v) {
return v + (14 - iconSize) / 2 + 'px';
}).join(' '),
borderRadius: '2px'
})
: '';
},
id: 'type',
operator: '+',
2013-05-09 13:03:33 +00:00
title: Ox._('Type'),
2012-02-20 18:31:45 +00:00
titleImage: 'icon',
visible: true,
width: 16
},
{
format: function(value, data) {
return data.type
? value
: $('<span>').addClass('OxWarning').html(value);
},
2011-10-03 16:14:01 +00:00
id: 'name',
operator: '+',
removable: false,
2013-05-09 13:03:33 +00:00
title: Ox._('Name'),
2011-10-03 16:14:01 +00:00
visible: true,
width: 144
},
{
editable: false,
format: function(value) {
return value.join('; ');
},
id: 'alternativeNames',
operator: '+',
2013-05-09 13:03:33 +00:00
title: Ox._('Alternative Names'),
2011-10-03 16:14:01 +00:00
visible: true,
width: 144
},
{
id: 'start',
operator: '-',
sort: function(value) {
2011-10-10 12:38:54 +00:00
return Ox.parseDate(value);
},
2013-05-09 13:03:33 +00:00
title: Ox._('Start'),
2011-10-03 16:14:01 +00:00
visible: true,
width: 144
},
{
id: 'end',
operator: '-',
sort: function(value) {
2011-10-10 12:38:54 +00:00
return Ox.parseDate(value);
},
2013-05-09 13:03:33 +00:00
title: Ox._('End'),
2011-10-03 16:14:01 +00:00
visible: true,
width: 144
},
{
2011-10-10 12:38:54 +00:00
format: function(value, data) {
var key = data.start + ' - ' + data.end;
if (!self.durationCache[key]) {
self.durationCache[key] = data.start
? Ox.formatDateRangeDuration(data.start, data.end, true)
: '';
}
2011-11-03 15:42:41 +00:00
return self.durationCache[key];
2011-10-10 12:38:54 +00:00
},
id: 'id',
operator: '-',
sort: function(value, data) {
2011-10-10 12:38:54 +00:00
return Ox.parseDate(data.end) - Ox.parseDate(data.start);
},
2013-05-09 13:03:33 +00:00
title: Ox._('Duration'),
2011-10-03 16:14:01 +00:00
visible: true,
2011-10-10 12:38:54 +00:00
width: 256
2011-10-03 16:14:01 +00:00
},
{
format: function(value) {
return Ox.encodeHTMLEntities(value);
},
2011-10-03 16:14:01 +00:00
id: 'user',
operator: '+',
2013-05-09 13:03:33 +00:00
title: Ox._('User'),
2011-10-03 16:14:01 +00:00
visible: false,
width: 96
},
{
format: function(value) {
return value.replace('T', ' ').replace('Z', '');
},
id: 'created',
operator: '-',
2013-05-09 13:03:33 +00:00
title: Ox._('Date Created'),
2011-10-03 16:14:01 +00:00
visible: false,
2012-05-26 15:48:19 +00:00
width: 128
2011-10-03 16:14:01 +00:00
},
{
format: function(value) {
return value.replace('T', ' ').replace('Z', '');
},
id: 'modified',
operator: '-',
2013-05-09 13:03:33 +00:00
title: Ox._('Date Modified'),
2011-10-03 16:14:01 +00:00
visible: false,
2012-05-26 15:48:19 +00:00
width: 128
2011-10-03 16:14:01 +00:00
}
];
2012-02-20 18:31:45 +00:00
self.options.hasMatches && self.columns.push({
align: 'right',
id: 'matches',
operator: '-',
2013-05-09 13:03:33 +00:00
title: Ox._('Matches'),
2012-02-20 18:31:45 +00:00
visible: true,
2012-05-26 15:48:19 +00:00
width: 64
2012-02-20 18:31:45 +00:00
});
2011-10-03 16:14:01 +00:00
self.$listToolbar = Ox.Bar({
size: 24
});
self.$findElement = Ox.FormElementGroup({
elements: [
self.$findSelect = Ox.Select({
items: [
2013-05-09 13:03:33 +00:00
{id: 'all', title: Ox._('Find: All')},
{id: 'name', title: Ox._('Find: Name')},
{id: 'alternativeNames', title: Ox._('Find: Alternative Names')},
2011-10-03 16:14:01 +00:00
],
overlap: 'right',
type: 'image'
})
.bindEvent({
change: function(data) {
2011-12-21 15:33:52 +00:00
var key = data.value,
2011-10-03 16:14:01 +00:00
value = self.$findInput.value();
value && updateList(key, value);
}
}),
self.$findInput = Ox.Input({
clear: true,
2013-05-09 13:03:33 +00:00
placeholder: Ox._('Find in List'),
2011-10-03 16:14:01 +00:00
width: 234
})
.bindEvent({
submit: function(data) {
var key = self.$findSelect.value(),
value = data.value;
updateList(key, value);
}
})
]
})
.css({float: 'right', margin: '4px'})
.appendTo(self.$listToolbar);
2012-06-27 07:41:10 +00:00
self.$list = Ox.TableList({
2011-10-03 16:14:01 +00:00
columns: self.columns,
columnsRemovable: true,
columnsVisible: true,
2011-10-10 12:38:54 +00:00
// we have to clone so that when self.options.events changes,
// self.$list.options({items: self.options.events}) still
// registers as a change
items: Ox.clone(self.options.events, true),
2012-02-20 18:31:45 +00:00
max: 1,
min: 0,
2011-10-03 16:14:01 +00:00
pageLength: self.options.pageLength,
scrollbarVisible: true,
2012-02-20 18:31:45 +00:00
selected: self.options.selected ? [self.options.selected] : [],
sort: self.options.sort,
unique: 'id'
2011-10-03 16:14:01 +00:00
})
.bindEvent({
2011-10-10 12:38:54 +00:00
'delete': removeEvent,
2011-10-03 16:37:05 +00:00
init: initList, // fixme: won't fire from static list
2011-10-03 16:14:01 +00:00
key_0: function() {
self.$calendar.panToEvent();
},
key_equal: function() {
self.$calendar.zoom(1);
},
key_minus: function() {
self.$calendar.zoom(-1);
},
key_shift_0: function() {
2012-02-20 18:31:45 +00:00
self.$calendar.zoomToEvent();
2011-10-03 16:14:01 +00:00
},
load: function() {
that.triggerEvent('loadlist');
},
open: openItem,
2012-06-10 11:01:06 +00:00
select: function(data) {
selectItem(data);
}
2011-10-03 16:14:01 +00:00
});
self.$listStatusbar = Ox.Bar({
size: 16
});
self.$status = Ox.Element()
.css({paddingTop: '2px', margin: 'auto', fontSize: '9px', textAlign: 'center'})
2011-10-03 16:37:05 +00:00
.html(
2013-05-09 13:03:33 +00:00
Ox.formatCount(self.options.events, Ox._('Event'), Ox._('Events'))
2011-10-03 16:37:05 +00:00
)
2011-10-03 16:14:01 +00:00
.appendTo(self.$listStatusbar);
2011-10-09 21:13:16 +00:00
self.$calendar = Ox.Calendar({
2012-02-20 18:31:45 +00:00
date: new Date(0),
//events: Ox.clone(self.options.events, true),
events: self.options.events.filter(function(event) {
return !!event.type;
}),
height: self.options.height,
showControls: self.options.showControls,
showToolbar: true,
showZoombar: true,
width: self.options.width - 514,
zoom: 4
})
.bindEvent({
resize: function(data) {
// triggered by SplitPanel
self.$calendar.resizeCalendar();
},
select: selectEvent
});
2011-10-03 16:14:01 +00:00
2011-10-09 21:13:16 +00:00
self.$eventTitlebar = Ox.Bar({
2011-10-03 16:14:01 +00:00
size: 24
});
2011-10-09 21:13:16 +00:00
self.$eventTitle = $('<div>')
.hide()
.appendTo(self.$eventTitlebar);
self.$eventName = Ox.Label({
title: '',
width: 228
})
.css({float: 'left', margin: '4px'})
.bindEvent({
singleclick: function() {
self.$calendar.panToEvent();
},
doubleclick: function() {
self.$calendar.zoomToEvent();
}
})
.appendTo(self.$eventTitle);
2011-10-09 21:13:16 +00:00
self.$deselectEventButton = Ox.Button({
title: 'close',
2013-05-09 13:03:33 +00:00
tooltip: Ox._('Done'),
2011-10-09 21:13:16 +00:00
type: 'image'
})
.css({float: 'left', margin: '4px 4px 4px 0'})
.bindEvent({
click: function() {
2012-02-20 18:31:45 +00:00
self.$list.options({selected: []});
// FIXME: list doesn't fire select event
selectItem({ids: []});
2011-10-09 21:13:16 +00:00
}
})
.appendTo(self.$eventTitle);
2011-10-03 16:14:01 +00:00
2012-02-20 18:31:45 +00:00
self.$eventData = Ox.Element();
2011-10-09 21:13:16 +00:00
self.$eventForm = Ox.Form({
items: [
self.$nameInput = Ox.Input({
id: 'name',
2013-05-09 13:03:33 +00:00
label: Ox._('Name'),
2011-10-09 21:13:16 +00:00
labelWidth: 64,
width: 240
}),
self.$alternativeNamesInput = Ox.ArrayInput({
id: 'alternativeNames',
2013-05-09 13:03:33 +00:00
label: Ox._('Alternative Names'),
2011-10-09 21:13:16 +00:00
max: 10,
values: [],
width: 240
}),
Ox.Select({
id: 'type',
items: [
2013-05-09 13:03:33 +00:00
{id: 'date', title: Ox._('Date')},
{id: 'place', title: Ox._('Place')},
{id: 'person', title: Ox._('Person')},
{id: 'other', title: Ox._('Other')}
2011-10-09 21:13:16 +00:00
],
2013-05-09 13:03:33 +00:00
label: Ox._('Type'),
2011-10-09 21:13:16 +00:00
labelWidth: 64,
width: 240
}),
self.$startInput = Ox.Input({
id: 'start',
2013-05-09 13:03:33 +00:00
label: Ox._('Start'),
2011-10-09 21:13:16 +00:00
labelWidth: 64,
width: 240
}),
self.$endInput = Ox.Input({
id: 'end',
2013-05-09 13:03:33 +00:00
label: Ox._('End'),
2011-10-09 21:13:16 +00:00
labelWidth: 64,
width: 240
}),
self.$durationInput = Ox.Input({
disabled: true,
id: 'durationText',
2013-05-09 13:03:33 +00:00
label: Ox._('Duration'),
2011-10-09 21:13:16 +00:00
labelWidth: 64,
width: 240
})
],
2011-10-03 16:14:01 +00:00
width: 240
})
.css({margin: '8px'})
2011-10-09 21:13:16 +00:00
.hide()
.bindEvent({
change: function(data) {
2012-01-13 16:25:47 +00:00
var exists = false, values;
2011-10-09 21:13:16 +00:00
if (['name', 'alternativeNames'].indexOf(data.id) > -1) {
exists = '';
values = data.id == 'name' ? [data.data.value] : data.data.value;
Ox.forEach(self.options.events, function(event) {
Ox.forEach(values, function(value) {
if (event.type && (
event.name == data.data.value
|| event.alternativeNames.indexOf(data.data.value) > -1
)) {
2011-10-09 21:13:16 +00:00
exists = value;
2012-07-05 08:58:08 +00:00
return false; // break
2011-10-09 21:13:16 +00:00
}
});
2012-07-05 08:58:08 +00:00
if (exists) {
return false; // break
}
2011-10-09 21:13:16 +00:00
});
}
if (data.id == 'name') {
if (!exists) {
// FIXME: can we change this to data.value?
editEvent('name', data.data.value);
} else {
self.$nameInput.addClass('OxError');
}
} else if (data.id == 'alternativeNames') {
if (!exists) {
editEvent('alternativeNames', data.data.value);
} else {
self.$alternativeNamesInput.setErrors([exists]);
}
} else if (data.id == 'type') {
2011-12-21 15:33:52 +00:00
editEvent('type', data.data.value);
2011-10-09 21:13:16 +00:00
} else if (data.id == 'start') {
editEvent('start', data.data.value);
} else if (data.id == 'end') {
editEvent('end', data.data.value);
}
}
2012-02-20 18:31:45 +00:00
})
.appendTo(self.$eventData);
if (self.options.hasMatches) {
self.$matchesInput = Ox.Input({
disabled: true,
id: 'matches',
2013-05-09 13:03:33 +00:00
label: Ox._('Matches'),
2012-02-20 18:31:45 +00:00
labelWidth: 64,
type: 'int',
width: 240
})
.css({margin: '8px'})
.hide()
2012-02-20 18:31:45 +00:00
.appendTo(self.$eventData);
}
2011-10-03 16:14:01 +00:00
2011-10-09 21:13:16 +00:00
self.$eventStatusbar = Ox.Bar({
2011-10-03 16:14:01 +00:00
size: 24
});
self.$removeEventButton = Ox.Button({
2013-05-09 13:03:33 +00:00
title: Ox._('Remove Event'),
width: 90
2011-10-09 21:13:16 +00:00
})
2012-02-20 18:31:45 +00:00
.css({float: 'left', margin: '4px'})
2011-10-09 21:13:16 +00:00
.bindEvent({
click: removeEvent
2011-10-09 21:13:16 +00:00
})
.hide()
2011-10-09 21:13:16 +00:00
.appendTo(self.$eventStatusbar);
self.$newEventButton = Ox.Button({
2013-05-09 13:03:33 +00:00
title: Ox._('New Event'),
width: 70
2011-10-09 21:13:16 +00:00
})
2012-02-20 18:31:45 +00:00
.css({float: 'right', margin: '4px'})
2011-10-09 21:13:16 +00:00
.bindEvent({
click: addEvent
2011-10-09 21:13:16 +00:00
})
.appendTo(self.$eventStatusbar);
2012-02-20 18:31:45 +00:00
if (self.options.mode == 'define') {
self.$defineEventButton = Ox.Button({
2013-05-09 13:03:33 +00:00
title: Ox._('Define Event'),
2012-02-20 18:31:45 +00:00
width: 80
})
.css({float: 'right', margin: '4px 0 4px 0'})
.bindEvent({
click: function() {
2013-05-09 13:03:33 +00:00
if (this.options('title') == Ox._('Define Event')) {
2012-02-20 18:31:45 +00:00
defineEvent();
} else {
clearEvent();
2011-10-09 21:13:16 +00:00
}
2012-02-20 18:31:45 +00:00
}
})
.hide()
.appendTo(self.$eventStatusbar);
}
that.setElement(
Ox.SplitPanel({
2012-02-20 18:31:45 +00:00
elements: [
{
collapsible: self.options.collapsible,
element: Ox.SplitPanel({
elements: [
{
element: self.$listToolbar,
size: 24
},
{
element: self.$list
},
{
element: self.$listStatusbar,
size: 16
}
],
orientation: 'vertical'
}),
resizable: true,
resize: [256, 384, 512],
size: 256
},
{
2012-05-26 15:48:19 +00:00
element: self.$calendar
2012-02-20 18:31:45 +00:00
},
{
collapsible: self.options.collapsible,
element: Ox.SplitPanel({
elements: [
{
element: self.$eventTitlebar,
size: 24
},
{
element: self.$eventData
},
{
element: self.$eventStatusbar,
size: 24
}
],
orientation: 'vertical'
})
.bindEvent({
resize: function(data) {
self.$eventName.options({width: data.size - 28});
2012-02-20 18:31:45 +00:00
// fixme: pass width through form
self.$eventForm.options('items').forEach(function($item) {
2012-02-20 18:31:45 +00:00
$item.options({width: data.size - 16});
});
self.$matchesInput.options({width: data.size - 16});
2012-02-20 18:31:45 +00:00
}
}),
resizable: true,
resize: [256, 384],
size: 256
}
],
orientation: 'horizontal'
})
.addClass('OxCalendarEditor')
2012-02-20 18:31:45 +00:00
);
2011-10-03 16:14:01 +00:00
// if loaded with selection, set calendar and form
self.options.selected && self.$list.triggerEvent({
select: {ids: [self.options.selected]}
});
2011-10-09 21:13:16 +00:00
function addEvent() {
2011-11-04 15:54:28 +00:00
Ox.Log('Calendar', 'ADD', self.$calendar.getBounds())
2011-10-09 21:13:16 +00:00
var bounds = self.$calendar.getBounds(),
middle = +self.$calendar.options('date'),
2011-10-10 12:38:54 +00:00
startTime = +new Date((+bounds.startTime + middle) / 2),
endTime = +new Date((+bounds.endTime + middle) / 2),
2011-10-09 21:13:16 +00:00
event = {},
i = 1;
2013-05-10 10:45:24 +00:00
event.name = Ox._('Untitled');
2011-10-09 21:13:16 +00:00
while (nameExists(event.name)) {
2013-05-10 10:45:24 +00:00
event.name = Ox._('Untitled') + ' [' + (++i) + ']';
2011-10-09 21:13:16 +00:00
};
event.alternativeNames = [];
event.type = 'other';
2011-10-10 12:38:54 +00:00
event.start = Ox.formatDate(startTime, '%Y-%m-%d %H:%M:%S', true);
event.end = Ox.formatDate(endTime, '%Y-%m-%d %H:%M:%S', true);
2011-11-04 15:54:28 +00:00
Ox.Log('Calendar', event);
2012-06-10 11:01:06 +00:00
self.$newEventButton.options({disabled: true});
self.$removeEventButton.options({disabled: true});
self.options.addEvent(encodeValues(event), function(result) {
if (result.status.code == '200') {
event.id = result.data.id;
event.created = result.data.created;
event.modified = result.data.modified;
2012-06-10 11:01:06 +00:00
if (self.options.hasMatches) {
event.matches = result.data.matches;
}
self.options.events.push(event);
2012-06-10 11:01:06 +00:00
// var time0 = +new Date()
self.$list.options({items: Ox.clone(self.options.events, true)});
2012-06-10 11:01:06 +00:00
// Ox.Log('Calendar', 'TIME TO SET LIST OPTIONS:', +new Date() - time0);
self.$calendar.addEvent(event);
selectEvent(event);
2011-12-18 09:44:11 +00:00
self.$nameInput.focusInput(true);
2012-06-10 11:01:06 +00:00
self.$newEventButton.options({disabled: false});
self.$removeEventButton.options({disabled: false});
} else {
// FIXME
2012-02-20 18:31:45 +00:00
// alert(result.status.text);
}
2011-10-09 21:13:16 +00:00
});
}
2012-02-20 18:31:45 +00:00
function clearEvent() {
var event = Ox.getObjectById(self.options.events, self.options.selected),
values = {
id: self.options.selected,
alternativeNames: [], type: '',
start: '', end: ''
};
2013-05-09 13:03:33 +00:00
self.$defineEventButton.options({disabled: true, title: Ox._('Clear Event')});
self.options.editEvent(encodeValues(values), function() {
2012-02-20 18:31:45 +00:00
Ox.forEach(values, function(value, key) {
self.$list.value(self.options.selected, key, value);
});
self.$list.reloadList();
self.$calendar.removeEvent();
self.$eventForm.hide();
2013-05-09 13:03:33 +00:00
self.$defineEventButton.options({disabled: false, title: Ox._('Define Event')});
2012-02-20 18:31:45 +00:00
});
}
function decodeValues(place) {
return Ox.map(place, function(value) {
var type = Ox.typeOf(value);
return type == 'string' ? Ox.decodeHTMLEntities(value)
: type == 'array' ? Ox.map(value, function(value) {
return decodeValues(value);
})
: value;
});
}
2012-02-20 18:31:45 +00:00
function defineEvent() {
var bounds = self.$calendar.getBounds(),
middle = +self.$calendar.options('date'),
startTime = +new Date((+bounds.startTime + middle) / 2),
endTime = +new Date((+bounds.endTime + middle) / 2),
event = Ox.getObjectById(self.options.events, self.options.selected);
event.name = self.$list.value(self.options.selected, 'name');
event.alternativeNames = [];
event.type = 'other';
event.start = Ox.formatDate(startTime, '%Y-%m-%d %H:%M:%S', true);
event.end = Ox.formatDate(endTime, '%Y-%m-%d %H:%M:%S', true);
self.$list.options({items: Ox.clone(self.options.events, true)});
self.$calendar.addEvent(event);
2013-05-09 13:03:33 +00:00
self.$defineEventButton.options({title: Ox._('Clear Event')});
2012-02-20 18:31:45 +00:00
}
2011-10-09 21:13:16 +00:00
function editEvent(key, value) {
2011-10-10 12:38:54 +00:00
var id = self.selectedEvent,
index = Ox.getIndexById(self.options.events, id),
2011-10-10 12:38:54 +00:00
data = {id: id};
data[key] = value;
self.options.editEvent(encodeValues(data), function(result) {
if (result.status.code == 200) {
self.options.events[index][key] = value;
if (self.options.hasMatches) {
self.options.events[index].matches = result.data.matches;
}
self.$list.options({items: Ox.clone(self.options.events, true)});
2012-01-30 23:27:27 +00:00
self.$calendar.editEvent(id, key, value);
if (key == 'name') {
self.$eventName.options({title: value});
} else if (['start', 'end'].indexOf(key) > -1) {
2011-12-21 15:33:52 +00:00
self.$durationInput.value(
Ox.formatDateRangeDuration(
self.$startInput.value(),
self.$endInput.value()
|| Ox.formatDate(new Date(), '%Y-%m-%d %H%:%M:%S'),
true
)
2011-12-21 15:33:52 +00:00
);
}
self.options.hasMatches && self.$matchesInput.value(result.data.matches);
2012-02-20 18:31:45 +00:00
self.options.mode == 'define' && self.$removeEventButton.options({
disabled: !!result.data.matches
});
} else {
2012-01-30 23:27:27 +00:00
// ...
}
2011-10-09 21:13:16 +00:00
});
}
2011-10-03 16:14:01 +00:00
function encodeValues(place) {
return Ox.map(place, function(value) {
var type = Ox.typeOf(value);
return type == 'string' ? Ox.encodeHTMLEntities(value)
: type == 'array' ? Ox.map(value, function(value) {
return encodeValues(value);
})
: value;
});
}
2011-10-03 16:37:05 +00:00
function initList(data) {
self.$status.html(
2013-05-09 13:03:33 +00:00
Ox.formatCount(data.items, Ox._('Event'), Ox._('Events'))
2011-10-03 16:37:05 +00:00
);
2011-10-03 16:14:01 +00:00
}
2011-10-09 21:13:16 +00:00
function nameExists(name) {
var exists = false;
Ox.forEach(self.options.events, function(event) {
if (
event.name == name
|| event.alternativeNames.indexOf(name) > -1
) {
exists = true;
2012-07-05 08:58:08 +00:00
return false; // break
2011-10-09 21:13:16 +00:00
}
});
return exists;
2011-10-03 16:14:01 +00:00
}
2011-10-09 21:13:16 +00:00
function openItem(data) {
selectItem(data);
self.$calendar.zoomToEvent(data.ids[0]);
2011-10-03 16:37:05 +00:00
}
2011-10-09 21:13:16 +00:00
function removeEvent() {
2011-10-10 12:38:54 +00:00
var id = self.selectedEvent,
index = Ox.getIndexById(self.options.events, id);
2012-06-10 11:01:06 +00:00
self.$newEventButton.options({disabled: true});
self.$removeEventButton.options({disabled: true});
2011-10-10 12:38:54 +00:00
self.options.removeEvent({id: id}, function(result) {
if (result.status.code == '200') {
2012-06-10 11:01:06 +00:00
selectEvent({});
self.options.events.splice(index, 1);
var time0 = +new Date();
self.$list.options({items: Ox.clone(self.options.events, true)});
2011-11-04 15:54:28 +00:00
Ox.Log('Calendar', 'TIME TO SET LIST OPTIONS:', +new Date() - time0);
self.$calendar.removeEvent();
2012-06-10 11:01:06 +00:00
self.$newEventButton.options({disabled: false});
self.$removeEventButton.options({disabled: false});
} else {
2012-02-20 18:31:45 +00:00
// FIXME
// alert(result.status.text);
}
2011-10-09 21:13:16 +00:00
});
}
function selectEvent(event) {
2012-02-20 18:31:45 +00:00
// Select event on calendar
var isUndefined = !!self.options.selected
&& !self.$list.value(self.options.selected, 'type');
self.selectedEvent = event.id || '';
if (!self.selectedEvent && isUndefined) {
2012-02-20 18:31:45 +00:00
// deselect triggered by selecting an undefined item,
// so do nothing
} else {
self.options.selected = self.selectedEvent;
self.$list.options({
selected: self.options.selected ? [self.options.selected] : []
});
selectItem({ids: self.$list.options('selected')}, event);
2012-02-20 18:31:45 +00:00
}
}
function selectItem(data, event) {
2012-02-20 18:31:45 +00:00
// Select item in list
var fromCalendar = !!event, isUndefined, selectedEvent;
2012-02-20 18:31:45 +00:00
self.options.selected = data.ids.length ? data.ids[0] : '';
event = event || (
self.options.selected
2012-06-30 15:06:58 +00:00
? Ox.getObjectById(self.options.events, self.options.selected)
: {}
2012-06-10 11:01:06 +00:00
);
2012-06-30 15:06:58 +00:00
isUndefined = !fromCalendar && !!self.options.selected && !event.type;
if (!fromCalendar) {
selectedEvent = self.options.selected && !isUndefined
? self.options.selected
: '';
self.$calendar.options({selected: selectedEvent});
selectedEvent && self.$calendar.panToEvent();
}
2012-02-20 18:31:45 +00:00
if (self.options.selected) {
self.$eventName.options({title: event.name || ''});
self.$eventTitle.show();
2012-02-20 18:31:45 +00:00
if (!isUndefined) {
self.$eventForm.values(
decodeValues(Ox.extend({}, event, {
end: event.current ? '' : event.end,
durationText: Ox.formatDateRangeDuration(
event.start, event.end, true
)
}))
).show();
2012-02-20 18:31:45 +00:00
} else {
self.$eventForm.hide();
}
self.options.hasMatches && self.$matchesInput.value(event.matches || 0).show();
self.options.mode == 'define' && self.$defineEventButton.options({
disabled: !event.matches,
title: isUndefined ? 'Define Event' : 'Clear Event'
}).show();
self.$removeEventButton.options({
disabled: self.options.mode == 'define' && !!event.matches
}).show();
2011-10-09 21:13:16 +00:00
} else {
self.$eventTitle.hide();
self.$eventForm.hide();
2012-02-20 18:31:45 +00:00
self.options.hasMatches && self.$matchesInput.hide();
self.options.mode == 'define' && self.$defineEventButton.hide();
2011-10-09 21:13:16 +00:00
self.$removeEventButton.hide();
}
2011-10-03 16:14:01 +00:00
}
function updateList(key, value) {
var events;
if (value === '') {
events = Ox.clone(self.options.events);
} else {
events = [];
self.options.events.forEach(function(event) {
if ((
2011-10-03 16:14:01 +00:00
['all', 'name'].indexOf(key) > -1
&& event.name.toLowerCase().indexOf(value) > -1
) || (
2011-10-03 16:14:01 +00:00
['all', 'alternativeNames'].indexOf(key) > -1
&& event.alternativeNames.join('\n').toLowerCase().indexOf(value) > -1
)) {
events.push(event)
}
});
}
self.$list.options({items: events});
2011-10-03 16:14:01 +00:00
}
2011-10-03 16:37:05 +00:00
return that;
2012-01-13 16:25:47 +00:00
};