oxjs/source/Ox.UI/js/Video/Ox.AnnotationPanel.js

217 lines
6.9 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-05-16 08:24:46 +00:00
2011-11-05 16:46:53 +00:00
'use strict';
2011-05-16 08:24:46 +00:00
/*@
Ox.AnnotationPanel <f:Ox.Element> AnnotationPanel Object
() -> <f> AnnotationPanel Object
(options) -> <f> AnnotationPanel Object
(options, self) -> <f> AnnotationPanel Object
options <o> Options object
editable <b|false> If true, annotations can be added
2011-05-16 10:49:48 +00:00
id <s> id
items <a|[]> items
2011-05-19 10:18:39 +00:00
title <s> title
type <s|'text'> panel type
2011-05-16 10:49:48 +00:00
width <n|0>
2011-05-16 08:24:46 +00:00
self <o> shared private variable
@*/
2011-04-22 22:03:10 +00:00
Ox.AnnotationPanel = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
2012-01-02 14:05:14 +00:00
.defaults({
2012-01-11 07:52:01 +00:00
collapsed: false,
editable: false,
2012-01-02 14:05:14 +00:00
id: '',
'in': 0,
item: '',
2012-01-02 14:05:14 +00:00
items: [],
out: 0,
position: 0,
range: 'all',
selected: '',
sort: 'position',
2012-01-02 14:05:14 +00:00
title: '',
type: 'text',
width: 0
})
.options(options || {});
2011-04-22 22:03:10 +00:00
2012-01-11 11:11:13 +00:00
self.sort = getSort();
2011-04-22 22:03:10 +00:00
that.setElement(
Ox.CollapsePanel({
2012-01-11 07:52:01 +00:00
collapsed: self.options.collapsed,
2011-05-19 12:37:19 +00:00
extras: self.options.editable ? [
Ox.Button({
2011-04-22 22:03:10 +00:00
id: 'add',
style: 'symbol',
2011-05-19 10:18:39 +00:00
title: 'add',
tooltip: 'Add ' + self.options.item,
2011-04-22 22:03:10 +00:00
type: 'image'
}).bindEvent({
click: function(data) {
2011-04-22 22:03:10 +00:00
that.triggerEvent('add', {value: ''});
}
})
2011-05-19 12:37:19 +00:00
] : [],
2011-04-22 22:03:10 +00:00
size: 16,
title: self.options.title
})
.addClass('OxAnnotationPanel')
.bindEvent({
toggle: togglePanel
})
);
2011-04-22 22:03:10 +00:00
that.$content = that.$element.$content;
if (self.options.type == 'event') {
self.$annotations = Ox.Element();
} else if (self.options.type == 'place') {
self.$annotations = Ox.Element();
} else if (['string', 'text'].indexOf(self.options.type) > -1) {
self.$annotations = Ox.ArrayEditable({
editable: self.options.editable,
items: getAnnotations(),
2012-01-06 11:58:53 +00:00
selected: self.options.selected,
sort: self.sort,
2012-01-09 20:25:38 +00:00
submitOnBlur: false,
2012-01-10 14:49:28 +00:00
width: self.options.type == 'text' ? self.options.width + 8 : self.options.width,
type: self.options.type == 'text' ? 'textarea' : 'input'
})
.bindEvent({
add: function(data) {
2012-01-10 14:49:28 +00:00
that.triggerEvent('add', {value: data.value || ''});
},
2012-01-09 20:25:38 +00:00
blur: function() {
that.triggerEvent('blur');
},
'delete': function(data) {
that.triggerEvent('remove', {id: data.id});
},
2012-01-09 20:25:38 +00:00
edit: function() {
self.editing = true;
that.triggerEvent('edit');
},
select: selectAnnotation,
submit: submitAnnotation,
key_space: function() {
that.triggerEvent('paused');
}
});
}
self.$annotations.appendTo(that.$content);
2011-04-22 22:03:10 +00:00
2012-01-06 11:58:53 +00:00
self.options.selected && setTimeout(function() {
selectAnnotation({id: self.options.selected});
}, 0);
function getAnnotations() {
return self.options.items.filter(function(item) {
return self.options.range == 'all' || (
self.options.range == 'selection'
&& item['in'] < self.options.out
&& item.out > self.options['in']
) || (
self.options.range == 'position'
&& item['in'] <= self.options.position
2012-01-06 11:58:53 +00:00
&& item.out >= self.options.position
2012-01-09 20:25:38 +00:00
) || self.editing && item.id == self.options.selected
});
}
2012-01-11 11:11:13 +00:00
function getSort() {
return ({
duration: ['-duration', '+in', '+value'],
position: ['+in', '-duration', '+value'],
text: ['+value', '+in', '-duration']
})[self.options.sort];
}
function selectAnnotation(data) {
var item = Ox.getObjectById(self.options.items, data.id);
2012-01-09 20:25:38 +00:00
self.options.selected = item ? data.id : '';
that.triggerEvent('select', Ox.extend(data, item ? {
2011-04-22 22:03:10 +00:00
'in': item['in'],
out: item.out,
layer: self.options.id
} : {}));
2011-04-22 22:03:10 +00:00
}
2012-01-09 20:25:38 +00:00
function submitAnnotation(data) {
var item = Ox.getObjectById(self.options.items, data.id);
item.value = data.value;
self.editing = false;
2012-01-11 11:54:58 +00:00
self.$annotations.options({items: self.options.items});
2012-01-09 20:25:38 +00:00
that.triggerEvent('submit', item);
}
2011-04-22 22:03:10 +00:00
function togglePanel() {
2012-01-11 07:52:01 +00:00
self.options.collapsed = !self.options.collapsed;
that.triggerEvent('toggle', {collapsed: self.options.collapsed});
2011-04-22 22:03:10 +00:00
}
self.setOption = function(key, value) {
2012-01-09 20:25:38 +00:00
if (['in', 'out'].indexOf(key) > -1 && self.editing) {
2012-01-11 11:11:13 +00:00
var index = Ox.getIndexById(self.options.items, self.options.selected);
self.options.items[index][key] = value;
self.options.items[index].duration = self.options.out - self.options['in'];
2012-01-09 20:25:38 +00:00
}
if (key == 'in') {
2012-01-09 20:25:38 +00:00
//fixme: array editable should support item updates while editing
self.editing || self.options.range == 'selection' && self.$annotations.options({
items: getAnnotations()
});
} else if (key == 'out') {
2012-01-09 20:25:38 +00:00
self.editing || self.options.range == 'selection' && self.$annotations.options({
items: getAnnotations()
});
} else if (key == 'position') {
2012-01-09 20:25:38 +00:00
self.editing || self.options.range == 'position' && self.$annotations.options({
items: getAnnotations()
});
} else if (key == 'range') {
self.$annotations.options({
items: getAnnotations()
});
2012-01-09 20:25:38 +00:00
} else if (key == 'selected') {
2012-01-11 11:54:58 +00:00
self.$annotations.options({selected: value});
} else if (key == 'sort') {
2012-01-11 11:11:13 +00:00
self.sort = getSort();
2012-01-11 11:54:58 +00:00
self.$annotations.options({sort: self.sort});
}
};
2011-05-16 10:49:48 +00:00
/*@
addItem <f> addItem
@*/
2011-04-22 22:03:10 +00:00
that.addItem = function(item) {
var pos = 0;
self.options.items.splice(pos, 0, item);
self.$annotations.addItem(pos, item);
2012-01-09 20:25:38 +00:00
self.$annotations.editItem(item.id);
2011-06-15 14:36:01 +00:00
};
2011-04-22 22:03:10 +00:00
2012-01-09 20:25:38 +00:00
that.blurItem = function() {
self.$annotations.blurItem();
2011-06-15 14:36:01 +00:00
};
2011-05-16 10:49:48 +00:00
2012-01-10 14:49:28 +00:00
that.editItem = function(id) {
self.$annotations.editItem(id);
};
2011-05-16 10:49:48 +00:00
/*@
2012-01-03 10:26:15 +00:00
removeItems <f> removeItems
2011-05-16 10:49:48 +00:00
@*/
2012-01-03 10:26:15 +00:00
that.removeItem = function(id) {
var pos = Ox.getIndexById(self.options.items, id);
self.options.items.splice(pos, 1);
self.$annotations.removeItems && self.$annotations.removeItems([id]);
};
2011-05-19 10:18:39 +00:00
2011-04-22 22:03:10 +00:00
return that;
};