pandora/static/js/news.js

218 lines
7.1 KiB
JavaScript
Raw Permalink Normal View History

2012-02-18 13:58:49 +00:00
'use strict';
2012-02-18 16:58:53 +00:00
pandora.ui.news = function(width, height) {
2012-02-18 13:58:49 +00:00
var that = Ox.Element(),
$left = $('<div>')
.css({position: 'absolute', width: width - 512})
.appendTo(that),
$right = $('<div>')
.css({position: 'absolute', top: '16px', right: '16px', width: '192px'})
.appendTo(that),
backgroundColor = Ox.Theme() == 'oxlight' ? 'rgb(224, 224, 224)'
2013-07-11 18:16:31 +00:00
: Ox.Theme() == 'oxmedium' ? 'rgb(128, 128, 128)'
2018-10-13 10:21:36 +00:00
: 'rgb(32, 32, 32)',
2017-11-04 09:53:27 +00:00
isEditable = pandora.hasCapability('canEditSitePages'),
2012-02-18 13:58:49 +00:00
items = [],
2012-02-20 05:55:53 +00:00
$text;
2012-02-18 13:58:49 +00:00
2013-07-11 18:16:31 +00:00
pandora.api.getNews(function(result) {
2012-02-18 13:58:49 +00:00
items = result.data.items;
2013-07-11 18:16:31 +00:00
selectItem();
2012-02-18 13:58:49 +00:00
});
function addItem() {
pandora.api.addNews({
2013-05-09 10:13:58 +00:00
title: Ox._('Untitled'),
2012-02-18 13:58:49 +00:00
date: Ox.formatDate(new Date(), '%Y-%m-%d'),
2012-02-18 16:58:53 +00:00
text: ''
2012-02-18 13:58:49 +00:00
}, function(result) {
items.splice(0, 0, result.data);
2013-07-11 18:16:31 +00:00
pandora.UI.set({'part.news': result.data.id});
2012-02-18 13:58:49 +00:00
});
}
function editItem(key, value) {
2013-07-11 18:16:31 +00:00
var data = {id: pandora.user.ui.part.news},
index = Ox.getIndexById(items, pandora.user.ui.part.news);
if (index == -1) {
index = 0;
}
2012-02-18 13:58:49 +00:00
data[key] = value;
pandora.api.editNews(data, function(result) {
items[index] = result.data;
['title', 'date'].indexOf(key) > -1 && renderList();
});
}
function removeItem() {
2013-07-11 18:16:31 +00:00
var index = Ox.getIndexById(items, pandora.user.ui.part.news);
if (index == -1) {
index = 0;
}
2012-02-18 13:58:49 +00:00
items.splice(index, 1);
2013-07-11 18:16:31 +00:00
pandora.api.removeNews({id: pandora.user.ui.part.news}, function(result) {
pandora.UI.set({
'part.news': items.length == 0 ? ''
: items[Math.min(index, items.length - 1)].id
});
2012-02-18 13:58:49 +00:00
});
}
function renderItem() {
2012-02-20 05:55:53 +00:00
var $title, $date,
2013-07-11 18:16:31 +00:00
index = Ox.getIndexById(items, pandora.user.ui.part.news);
if (index == -1) {
index = 0;
}
$left.empty();
if (items.length) {
$title = Ox.Editable({
editable: isEditable,
tooltip: isEditable ? pandora.getEditTooltip() : '',
value: items[index].title
})
.addClass('OxSelectable')
2013-07-11 18:16:31 +00:00
.css({
display: 'inline-block',
fontWeight: 'bold',
fontSize: '16px',
})
.bindEvent({
submit: function(data) {
editItem('title', data.value);
}
})
.appendTo($left);
$('<div>').css({height: '2px'}).appendTo($left);
$date = Ox.Editable({
editable: isEditable,
format: function(value) {
return Ox.formatDate(value, '%B %e, %Y');
},
tooltip: isEditable ? pandora.getEditTooltip() : '',
value: items[index].date
})
.addClass('OxSelectable')
2013-07-11 18:16:31 +00:00
.css({
display: 'inline-block',
fontSize: '9px'
2013-07-11 18:16:31 +00:00
})
.bindEvent({
submit: function(data) {
editItem('date', data.value);
}
})
.appendTo($left);
$('<div>').css({height: '8px'}).appendTo($left);
$text = Ox.Editable({
clickLink: pandora.clickLink,
editable: isEditable,
maxHeight: height - 96,
placeholder: Ox._('No text'),
tooltip: isEditable ? pandora.getEditTooltip() : '',
type: 'textarea',
value: items[index].text,
width: width - 512
})
.addClass('OxSelectable')
2013-07-11 18:16:31 +00:00
.bindEvent({
submit: function(data) {
editItem('text', data.value);
}
})
.appendTo($left);
2013-08-06 11:01:06 +00:00
$('<div>').css({height: '16px'}).appendTo($left);
2013-07-11 18:16:31 +00:00
}
2012-02-18 13:58:49 +00:00
}
function renderList() {
2018-10-13 10:21:36 +00:00
var $addButton = Ox.Button({
title: Ox._('Add'),
width: 92
})
.css({float: 'left', margin: '0 4px 0 0'})
.bindEvent({
click: function() {
$removeButton.options({disabled: false});
addItem();
}
});
var $removeButton = Ox.Button({
disabled: items.length == 0,
title: Ox._('Remove'),
width: 92
})
.css({float: 'left', margin: '0 0 0 4px'})
.bindEvent({
click: function() {
$removeButton.options({disabled: items.length == 1});
removeItem();
}
})
2012-02-18 13:58:49 +00:00
$right.empty();
if (isEditable) {
$('<div>')
.css({height: '16px', marginBottom: '8px'})
2018-10-13 10:21:36 +00:00
.append($addButton)
.append($removeButton)
2012-02-18 13:58:49 +00:00
.appendTo($right);
}
items.sort(function(a, b) {
return a.date < b.date ? 1 : a.date > b.date ? -1 : 0;
}).forEach(function(item, i) {
Ox.Element()
.addClass('item')
.css({
2012-02-18 16:58:53 +00:00
width: '172px',
2013-08-06 10:56:11 +00:00
padding: '4px 8px 3px 8px',
2012-02-18 13:58:49 +00:00
borderRadius: '8px',
2012-02-18 16:58:53 +00:00
margin: '2px',
backgroundColor: (item.id == pandora.user.ui.part.news)
|| (!pandora.user.ui.part.news && i == 0)
2013-07-11 18:16:31 +00:00
? backgroundColor : '',
2012-02-18 13:58:49 +00:00
cursor: 'pointer'
})
.html(
'<span style="font-size: 14px; font-weight: bold">' + item.title + '</span>'
+ '<br><span style="font-size: 9px">'
+ Ox.formatDate(item.date, '%B %e, %Y')
+ '</span>'
)
.bindEvent({
anyclick: function() {
2013-07-11 18:16:31 +00:00
pandora.UI.set('part.news', item.id);
2012-02-18 13:58:49 +00:00
}
})
.appendTo($right);
});
2013-08-06 11:01:06 +00:00
$('<div>').css({height: '16px'}).appendTo($right);
2012-02-18 13:58:49 +00:00
}
2013-07-11 18:16:31 +00:00
function selectItem() {
renderItem();
renderList();
}
that.resizeElement = function(data) {
2012-02-20 05:55:53 +00:00
width = data.width;
height = data.height;
$left.css({width: width - 512});
$text && $text.css({width: width - 512});
2012-02-18 13:58:49 +00:00
};
that.selectItem = function(direction) {
var index = Ox.getIndexById(items, pandora.user.ui.part.news) + direction;
if (index > -1 && index < items.length) {
pandora.UI.set('part.news', items[index].id);
}
};
2013-07-11 18:16:31 +00:00
that.bindEvent({
'pandora_part.news': selectItem
});
2012-02-18 13:58:49 +00:00
return that;
2012-02-20 05:55:53 +00:00
};