pandora/static/js/pandora/usersDialog.js

986 lines
37 KiB
JavaScript
Raw Normal View History

// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-11-05 17:04:10 +00:00
'use strict';
pandora.ui.usersDialog = function() {
2011-10-03 09:31:20 +00:00
2012-03-21 13:07:19 +00:00
var browsers = [
2012-10-27 19:35:20 +00:00
'Camino', 'Chrome', 'Chrome Frame', 'Chromium', 'Epiphany',
'Firefox', 'Internet Explorer', 'Konqueror', 'Nokia Browser',
'Opera', 'Safari', 'WebKit'
2012-03-21 13:07:19 +00:00
],
dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
2011-12-18 09:27:15 +00:00
dialogWidth = Math.round(window.innerWidth * 0.9),
formWidth = 256,
2011-10-03 09:31:20 +00:00
numberOfUsers = 0,
2012-03-21 13:07:19 +00:00
systems = [
2012-10-27 16:57:28 +00:00
'Android', 'BlackBerry', 'BSD', 'iOS', 'Java', 'Linux', 'Mac OS X',
'Nokia', 'PlayStation', 'Unix', 'Windows'
2012-03-21 13:07:19 +00:00
],
2012-05-24 08:22:56 +00:00
userLevels = pandora.site.userLevels.map(function(userLevel) {
return Ox.toTitleCase(userLevel);
}).concat(['Robot']),
2011-10-03 09:31:20 +00:00
$reloadButton = Ox.Button({
disabled: true,
title: 'redo',
tooltip: 'Reload',
type: 'image'
})
.css({float: 'left', margin: '4px 2px 4px 4px'})
.bindEvent({
click: function() {
$reloadButton.options({disabled: true});
Ox.Request.clearCache('findUsers');
$list.reloadList(true);
}
}),
$guestsCheckbox = Ox.Checkbox({
title: 'Include Guests',
value: false
})
.css({float: 'left', margin: '4px 2px 4px 2px'})
.bindEvent({
change: function(data) {
data.value
? $robotsCheckbox.show()
: $robotsCheckbox.hide().options({value: false});
updateList();
}
}),
$robotsCheckbox = Ox.Checkbox({
title: 'Include Robots',
value: false
})
.css({float: 'left', margin: '4px 2px 4px 2px'})
.hide()
.bindEvent({
change: updateList
}),
2011-10-03 09:31:20 +00:00
$findSelect = Ox.Select({
items: [
2011-12-22 15:48:48 +00:00
{id: 'all', title: 'Find: All'},
2011-10-03 09:31:20 +00:00
{id: 'username', title: 'Find: Username'},
{id: 'email', title: 'Find: E-Mail-Address'}
],
overlap: 'right',
type: 'image'
})
.bindEvent({
change: function(data) {
$findInput.value() && updateList();
2011-10-03 09:31:20 +00:00
$findInput.options({
2011-12-21 15:34:28 +00:00
placeholder: data.title
2011-10-03 09:31:20 +00:00
});
}
}),
$findInput = Ox.Input({
changeOnKeypress: true,
clear: true,
placeholder: 'Find: All',
width: 192
})
.bindEvent({
change: updateList
2011-10-03 09:31:20 +00:00
}),
$findElement = Ox.FormElementGroup({
elements: [
$findSelect,
$findInput
]
})
.css({float: 'right', margin: '4px'}),
2012-06-27 07:41:39 +00:00
$list = Ox.TableList({
2011-10-03 09:31:20 +00:00
columns: [
{
format: function(value, data) {
2011-10-03 09:31:20 +00:00
return $('<img>')
.attr({
src: Ox.UI.getImageURL('symbolCheck')
})
.css({
width: '10px',
height: '10px',
padding: '3px',
opacity: value || [
'guest', 'robot'
].indexOf(data.level) > -1 ? 0 : 1
2011-10-03 09:31:20 +00:00
});
},
id: 'disabled',
operator: '-',
2011-12-18 09:27:15 +00:00
title: 'Enabled',
titleImage: 'check',
2011-10-03 09:31:20 +00:00
visible: true,
width: 16
},
2012-04-19 14:26:52 +00:00
{
format: function(value) {
return $('<img>')
.attr({
src: Ox.UI.getImageURL('symbolMail')
})
.css({
width: '10px',
height: '10px',
padding: '3px',
opacity: +value
});
},
id: 'newsletter',
title: 'Newsletter',
titleImage: 'mail',
operator: '-',
visible: true,
width: 16
},
{
format: function(value, data) {
return '<span style="opacity: ' + (
data.disabled ? 0.5 : 1
) + '">' + Ox.encodeHTMLEntities(value) + '</span>';
},
id: 'username',
operator: '+',
removable: false,
title: 'Username',
visible: true,
width: 128
},
{
format: function(value, data) {
return '<span style="opacity: ' + (
data.disabled ? 0.5 : 1
) + '">' + value + '</span>';
},
id: 'email',
operator: '+',
title: 'E-Mail Address',
visible: true,
width: 192
},
{
align: 'center',
format: function(value) {
return Ox.Theme.formatColorLevel(
userLevels.indexOf(Ox.toTitleCase(value)),
userLevels,
[0, 300]
);
},
id: 'level',
operator: '-',
title: 'Level',
type: 'label',
visible: true,
width: 64
},
{
format: function(value) {
2012-03-21 14:01:18 +00:00
return Ox.Element({
2012-03-21 13:07:19 +00:00
element: '<img>',
tooltip: value
})
.attr({
src: Ox.getFlagByGeoname(value, 16)
2012-03-21 13:07:19 +00:00
})
.css({
width: '14px',
height: '14px',
borderRadius: '4px',
marginLeft: '-3px',
marginTop: 0
2012-03-21 14:01:18 +00:00
});
2012-03-21 13:07:19 +00:00
},
id: 'location',
operator: '+',
title: 'Location',
titleImage: 'flag',
visible: true,
width: 16
},
{
format: function(value) {
2012-03-21 14:01:18 +00:00
var system;
Ox.forEach(systems, function(s) {
if (new RegExp('^' + s).test(value)) {
system = s;
2012-07-04 11:40:37 +00:00
return false;
2012-03-21 13:07:19 +00:00
}
});
2012-03-21 14:01:18 +00:00
return system ? Ox.Element({
2012-03-21 13:07:19 +00:00
element: '<img>',
tooltip: value
2012-03-27 10:29:32 +00:00
.replace(/BSD \((.+)\)/, '$1')
.replace(/Linux \((.+)\)/, '$1')
.replace(/Unix \((.+)\)/, '$1')
.replace(/Windows (NT \d+\.\d+) \((.+)\)/, 'Windows $2 ($1)')
2012-03-21 13:07:19 +00:00
})
.attr({
2012-03-21 14:01:18 +00:00
src: Ox.UI.PATH + 'png/system'
+ system.replace(/ /g, '') + '128.png'
2012-03-21 13:07:19 +00:00
})
.css({
width: '14px',
height: '14px',
marginLeft: '-3px',
marginTop: 0
}) : '';
},
2012-03-21 14:01:18 +00:00
id: 'system',
2012-03-21 13:07:19 +00:00
operator: '+',
2012-03-21 14:01:18 +00:00
title: 'System',
2012-04-16 09:22:56 +00:00
titleImage: 'square',
2012-03-21 13:07:19 +00:00
visible: true,
2012-03-21 14:01:18 +00:00
width: 16
2012-03-21 13:07:19 +00:00
},
{
format: function(value) {
2012-03-21 14:01:18 +00:00
var browser;
Ox.forEach(browsers, function(b) {
if (new RegExp('^' + b).test(value)) {
browser = b;
2012-07-04 11:40:37 +00:00
return false;
2012-03-21 13:07:19 +00:00
}
});
2012-03-21 14:01:18 +00:00
return browser ? Ox.Element({
2012-03-21 13:07:19 +00:00
element: '<img>',
tooltip: value
})
.attr({
2012-03-21 14:01:18 +00:00
src: Ox.UI.PATH + 'png/browser'
+ browser.replace(/ /g, '') + '128.png'
2012-03-21 13:07:19 +00:00
})
.css({
width: '14px',
height: '14px',
marginLeft: '-3px',
marginTop: 0
}) : '';
},
2012-03-21 14:01:18 +00:00
id: 'browser',
2012-03-21 13:07:19 +00:00
operator: '+',
2012-03-21 14:01:18 +00:00
title: 'Browser',
2012-04-16 09:22:56 +00:00
titleImage: 'circle',
2012-03-21 13:07:19 +00:00
visible: true,
2012-03-21 14:01:18 +00:00
width: 16
2012-03-21 13:07:19 +00:00
},
2011-12-18 09:27:15 +00:00
{
align: 'right',
2011-12-18 09:27:15 +00:00
format: function(value) {
return Ox.formatNumber(value);
2011-12-18 09:27:15 +00:00
},
2011-10-03 09:31:20 +00:00
id: 'timesseen',
operator: '-',
title: 'Times Seen',
visible: true,
width: 80
2011-10-03 09:31:20 +00:00
},
{
align: 'right',
2011-10-03 11:37:46 +00:00
format: function(value) {
2012-10-31 23:54:09 +00:00
return Ox.formatDate(value, '%F %T');
2011-10-03 11:37:46 +00:00
},
2011-10-03 09:31:20 +00:00
id: 'firstseen',
operator: '-',
title: 'First Seen',
visible: true,
width: 144
2011-10-03 09:31:20 +00:00
},
{
align: 'right',
2011-10-03 11:37:46 +00:00
format: function(value) {
2012-10-31 23:54:09 +00:00
return Ox.formatDate(value, '%F %T');
2011-10-03 11:37:46 +00:00
},
2011-10-03 09:31:20 +00:00
id: 'lastseen',
operator: '-',
title: 'Last Seen',
visible: true,
width: 144
2011-10-03 09:31:20 +00:00
},
{
align: 'right',
format: function(value, data) {
return ['guest', 'robot'].indexOf(data.level) > -1
? '' : value;
},
id: 'numberoflists',
operator: '-',
title: 'Lists',
visible: true,
width: 64
},
{
id: 'groups',
operator: '+',
title: 'Groups',
visible: true,
width: 64
},
2011-10-03 09:31:20 +00:00
{
id: 'screensize',
align: 'right',
operator: '-',
title: 'Screen Size',
visible: true,
width: 80
2011-10-03 09:31:20 +00:00
},
{
align: 'right',
id: 'windowsize',
operator: '-',
title: 'Window Size',
visible: true,
width: 80
2011-10-03 09:31:20 +00:00
},
2011-12-03 16:17:07 +00:00
{
2012-03-21 13:07:19 +00:00
align: 'right',
id: 'ip',
2011-12-03 16:17:07 +00:00
operator: '+',
2012-03-21 13:07:19 +00:00
title: 'IP Address',
2011-12-03 16:17:07 +00:00
visible: true,
width: 128
2011-12-03 16:17:07 +00:00
},
2011-10-03 09:31:20 +00:00
{
id: 'useragent',
2011-10-03 16:38:07 +00:00
operator: '+',
2011-10-03 09:31:20 +00:00
title: 'User Agent',
visible: true,
width: 768
2011-10-03 09:31:20 +00:00
}
],
columnsRemovable: true,
columnsVisible: true,
2012-06-20 10:47:15 +00:00
items: pandora.api.findUsers,
query: {
conditions: [
{key: 'level', value: 'guest', operator: '!='},
{key: 'level', value: 'robot', operator: '!='}
],
operator: '&'
},
2012-01-13 09:47:18 +00:00
keys: ['notes', 'groups'],
2011-12-18 09:27:15 +00:00
max: -1,
2011-10-03 09:31:20 +00:00
scrollbarVisible: true,
sort: [{key: 'lastseen', operator: '-'}],
unique: 'id'
2011-10-03 09:31:20 +00:00
})
.bindEvent({
init: function(data) {
numberOfUsers = data.users;
$status.html(
2011-11-07 14:46:16 +00:00
Ox.formatNumber(data.items)
+ ' user' + (data.items == 1 ? '' : 's')
+ (
$guestsCheckbox.value()
? ' (' + Ox.formatNumber(data.users) + ' registered, '
+ Ox.formatNumber(data.guests) + ' guest'
2012-03-27 09:41:03 +00:00
+ (data.guests == 1 ? '' : 's') + ', '
+ Ox.formatNumber(data.robots) + ' robot'
+ (data.robots == 1 ? '' : 's')
+ ')'
: ''
)
);
2011-10-03 09:31:20 +00:00
},
load: function() {
$reloadButton.options({disabled: false});
},
2011-12-18 09:27:15 +00:00
select: selectUsers
2011-10-03 09:31:20 +00:00
}),
2011-12-18 09:27:15 +00:00
$formLabel = Ox.Label({
2011-10-03 09:31:20 +00:00
textAlign: 'center',
title: 'No user selected',
2011-12-18 09:27:15 +00:00
width: 212
2011-10-03 09:31:20 +00:00
})
2011-12-18 09:27:15 +00:00
.css({float: 'left', margin: '4px 2px 4px 4px'}),
2011-10-03 09:31:20 +00:00
2011-12-18 09:27:15 +00:00
$formButton = Ox.ButtonGroup({
buttons: [
2011-10-03 01:46:19 +00:00
{
2011-12-18 09:27:15 +00:00
id: 'edit',
selected: true,
title: 'edit',
tooltip: 'Edit'
},
2011-10-03 01:46:19 +00:00
{
2011-12-18 09:27:15 +00:00
id: 'mail',
title: 'mail',
tooltip: 'Mail'
}
],
selectable: true,
type: 'image'
})
.css({float: 'left', margin: '4px 4px 4px 2px'})
.bindEvent({
change: selectForm
}),
$form = Ox.Element({}),
$editForm,
$sendButton = Ox.Button({
disabled: true,
id: 'send',
title: 'Send',
width: 64
})
.bindEvent({
click: sendMail
}),
2011-12-18 09:27:15 +00:00
$mailForm = renderMailForm(),
$content = Ox.SplitPanel({
elements: [
{
element: Ox.SplitPanel({
elements: [
{
element: Ox.Bar({size: 24})
.append($reloadButton)
2011-12-18 09:27:15 +00:00
.append($guestsCheckbox)
.append($robotsCheckbox)
2011-12-18 09:27:15 +00:00
.append($findElement),
size: 24
},
{
element: $list
}
],
orientation: 'vertical'
})
},
{
element: Ox.SplitPanel({
2011-10-03 01:46:19 +00:00
elements: [
{
element: Ox.Bar({size: 24})
2011-12-18 09:27:15 +00:00
.append($formLabel)
.append($formButton),
2011-10-03 01:46:19 +00:00
size: 24
},
{
2011-12-18 09:27:15 +00:00
element: $form
2011-10-03 01:46:19 +00:00
}
],
orientation: 'vertical'
2011-12-18 09:27:15 +00:00
})
.bindEvent({
resize: setWidth
2011-10-03 01:46:19 +00:00
}),
2011-12-18 09:27:15 +00:00
resizable: true,
resize: [256, 384, 512],
size: 256
}
],
orientation: 'horizontal'
}),
that = Ox.Dialog({
buttons: [
Ox.Button({
id: 'statistics',
title: 'Statistics...'
}).bindEvent({
click: function() {
that.close();
pandora.$ui.statisticsDialog = pandora.ui.statisticsDialog().open();
}
}),
{},
2011-12-18 09:27:15 +00:00
Ox.Button({
title: 'Export E-Mail Addresses'
})
.css({margin: '4px 4px 4px 0'})
.bindEvent({
click: function() {
pandora.api.findUsers({
query: {conditions: [], operator: '&'},
keys: ['email', 'username'],
range: [0, numberOfUsers],
sort: [{key: 'username', operator: '+'}]
}, function(result) {
var $dialog = Ox.Dialog({
buttons: [
Ox.Button({
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
content: Ox.Element()
.addClass('OxSelectable')
.css({margin: '16px'})
.html(
result.data.items.filter(function(item) {
return item.email;
}).map(function(item) {
return Ox.encodeHTMLEntities(item.username)
+ ' &lt;' + item.email + '&gt;';
2011-12-18 09:27:15 +00:00
}).join(', ')
),
removeOnClose: true,
title: 'E-Mail Addresses'
})
.open();
});
}
}),
Ox.Button({
id: 'done',
title: 'Done',
width: 48
}).bindEvent({
click: function() {
that.close();
}
})
2011-10-03 01:46:19 +00:00
],
2011-12-18 09:27:15 +00:00
closeButton: true,
content: $content,
height: dialogHeight,
maximizeButton: true,
minHeight: 256,
minWidth: 512,
padding: 0,
removeOnClose: true,
title: 'Manage Users',
width: dialogWidth
})
.bindEvent({
resize: setHeight
2011-10-03 01:46:19 +00:00
}),
$status = $('<div>')
.css({
position: 'absolute',
top: '4px',
left: '128px',
right: '384px',
bottom: '4px',
paddingTop: '2px',
fontSize: '9px',
textAlign: 'center'
})
2012-05-22 13:15:16 +00:00
.appendTo(that.find('.OxButtonsbar'));
that.superClose = that.close;
that.close = function() {
Ox.Request.clearCache('findUsers');
that.superClose();
};
2011-12-18 09:27:15 +00:00
function getFormItemById(id) {
var ret;
Ox.forEach((
$formButton.value() == 'edit' ? $editForm : $mailForm
).options('items'), function($item) {
if ($item.options('id') == id) {
ret = $item;
2012-07-04 11:40:37 +00:00
return false;
2011-12-18 09:27:15 +00:00
}
});
return ret;
};
function getTo() {
return $list.options('selected').map(function(id) {
return $list.value(id);
}).filter(function(user) {
return ['guest', 'robot'].indexOf(user.level) == -1 && (
2011-12-18 09:27:15 +00:00
$mailForm.values().include == 'users' || user.newsletter
);
}).map(function(user) {
return user.username;
});
}
2011-10-03 09:31:20 +00:00
2011-12-18 09:27:15 +00:00
function renderEditForm() {
var user = $list.value($list.options('selected')[0]);
2011-10-03 09:31:20 +00:00
return Ox.Form({
2011-12-18 09:27:15 +00:00
items: [
Ox.Checkbox({
id: 'status',
label: 'Status',
labelWidth: 80,
title: !user.disabled ? 'Enabled' : 'Disabled',
2011-12-22 15:48:48 +00:00
value: !user.disabled,
2011-12-18 09:27:15 +00:00
width: formWidth - 16
})
.bindEvent({
change: function(data) {
this.options({
title: this.options('title') == 'Enabled'
? 'Disabled' : 'Enabled'
});
}
}),
Ox.Input({
id: 'username',
label: 'Username',
labelWidth: 80,
value: user.username,
width: formWidth - 16
})
.bindEvent({
submit: function(data) {
}
}),
Ox.Input({
id: 'email',
label: 'E-Mail',
labelWidth: 80,
value: user.email,
width: formWidth - 16
})
.bindEvent({
submit: function(data) {
2011-10-03 15:01:47 +00:00
2011-12-18 09:27:15 +00:00
}
}),
Ox.Select({
id: 'level',
2012-05-22 15:07:34 +00:00
items: pandora.site.userLevels.slice(1).map(function(level) {
return {
2011-12-18 09:27:15 +00:00
id: level,
title: Ox.toTitleCase(level)
2012-05-22 15:07:34 +00:00
};
2011-12-18 09:27:15 +00:00
}),
label: 'Level',
labelWidth: 80,
2011-12-22 15:48:48 +00:00
value: user.level,
2011-12-18 09:27:15 +00:00
width: formWidth - 16
}),
Ox.Checkbox({
id: 'newsletter',
label: 'Newsletter',
2011-10-03 15:01:47 +00:00
labelWidth: 80,
2011-12-18 09:27:15 +00:00
title: user.newsletter ? 'Subscribed' : 'Unsubscribed',
2011-12-22 15:48:48 +00:00
value: user.newsletter,
2011-12-18 09:27:15 +00:00
width: formWidth - 16
})
.bindEvent({
change: function(data) {
this.options({
title: this.options('title') == 'Subscribed'
? 'Unsubscribed' : 'Subscribed'
});
}
}),
Ox.Input({
id: 'groups',
label: 'Groups',
labelWidth: 80,
value: user.groups ? user.groups.join(', ') : '',
width: formWidth - 16
})
.bindEvent({
submit: function(data) {
}
}),
2011-12-18 09:27:15 +00:00
Ox.Input({
height: dialogHeight - 184,
2011-12-18 09:27:15 +00:00
id: 'notes',
placeholder: 'Notes',
type: 'textarea',
value: user.notes,
width: formWidth - 16
})
],
width: 240
})
.css({margin: '8px'})
.bindEvent({
change: function(event) {
var data = {id: user.id}, key, value;
if (event.id == 'status') {
2011-12-21 15:34:28 +00:00
data.disabled = !event.data.value;
2011-12-18 09:27:15 +00:00
} else if (event.id == 'level') {
2011-12-21 15:34:28 +00:00
data.level = event.data.value;
2011-12-18 09:27:15 +00:00
} else if (event.id == 'newsletter') {
2011-12-21 15:34:28 +00:00
data.newsletter = event.data.value;
2012-01-13 09:47:18 +00:00
} else if (event.id == 'groups') {
data.groups = event.data.value.split(', ');
2011-12-18 09:27:15 +00:00
} else {
data[event.id] = event.data.value;
}
if (event.id == 'status') {
$list.value(user.id, 'disabled', data.disabled);
} else {
$list.value(user.id, event.id, data[event.id]);
}
2011-12-18 09:27:15 +00:00
pandora.api.editUser(data, function(result) {
Ox.Request.clearCache('findUsers');
});
}
});
}
function renderMailForm() {
return Ox.Form({
items: [
Ox.Input({
disabled: true,
id: 'from',
label: 'From',
labelWidth: 80,
value: pandora.site.site.name + ' <' + pandora.site.site.email.contact + '>',
width: formWidth - 16
}),
Ox.Input({
disabled: true,
id: 'to',
label: 'To',
labelWidth: 80,
value: '',
width: formWidth - 16
}),
Ox.Select({
id: 'include',
items: [
{id: 'users', title: 'All users'},
{id: 'subscribers', title: 'Subscribers only'},
],
label: 'Include',
labelWidth: 80,
width: formWidth - 16
})
.bindEvent({
change: function() {
setTo();
setSend();
}
2011-10-03 09:31:20 +00:00
}),
2011-12-18 09:27:15 +00:00
Ox.Input({
id: 'subject',
label: 'Subject',
labelWidth: 80,
value: pandora.site.site.email.prefix + ' ',
width: formWidth - 16
})
.bindEvent({
change: setSend
2011-10-03 09:31:20 +00:00
}),
2011-12-18 09:27:15 +00:00
Ox.Input({
height: dialogHeight - 208,
id: 'message',
placeholder: 'Message',
2011-10-03 09:31:20 +00:00
type: 'textarea',
2011-12-18 09:27:15 +00:00
value: '\n\n' + pandora.site.site.email.footer,
width: formWidth - 16
2011-10-03 09:31:20 +00:00
})
2011-12-18 09:27:15 +00:00
.bindEvent({
change: setSend
}),
2011-12-20 07:20:23 +00:00
Ox.MenuButton({
2011-12-18 09:27:15 +00:00
id: 'insert',
items: [
{id: 'username', title: 'Username'},
{id: 'email', title: 'E-Mail address'},
],
title: 'Insert...',
width: formWidth - 16
})
.bindEvent({
click: function(data) {
var $input = getFormItemById('message'),
textarea = $input.find('textarea')[0],
2011-12-21 15:34:28 +00:00
value = $input.value();
$input.value(
2012-05-24 09:56:52 +00:00
value.slice(0, textarea.selectionStart)
2011-12-21 15:34:28 +00:00
+ '{' + data.id + '}'
2012-05-24 09:56:52 +00:00
+ value.slice(textarea.selectionEnd)
2011-12-21 15:34:28 +00:00
)
2011-12-18 09:27:15 +00:00
.focusInput(textarea.selectionStart + data.id.length + 2);
}
}),
Ox.Checkbox({
id: 'receipt',
title: 'Send a receipt to ' + pandora.user.email,
2011-12-22 15:48:48 +00:00
value: false,
2011-12-18 09:27:15 +00:00
width: formWidth - 16
}),
$sendButton
2011-12-18 09:27:15 +00:00
],
width: formWidth - 16
})
.css({margin: '8px', textAlign: 'right'});
}
function selectForm(data) {
var selected;
2011-12-21 15:34:28 +00:00
if (data.value == 'edit') {
2011-12-18 09:27:15 +00:00
$mailForm.detach();
selected = $list.options('selected');
if (selected.length == 1 && ['guest', 'robot'].indexOf(selected[0].level) == -1) {
2011-12-18 09:27:15 +00:00
$form.append($editForm = renderEditForm());
}
} else {
setTo();
setSend();
setWidth();
$editForm && $editForm.remove();
$form.append($mailForm);
}
}
function selectUsers(data) {
var users = data.ids.map(function(id) {
2011-12-18 09:27:15 +00:00
return $list.value(id);
});
setLabel();
if ($formButton.value() == 'edit') {
$form.empty();
if (
data.ids.length == 1
&& ['guest', 'robot'].indexOf(users[0].level) == -1
) {
$form.append($editForm = renderEditForm());
2011-12-18 09:27:15 +00:00
}
} else {
setTo();
setSend();
}
}
function sendMail() {
$sendButton.options({title: 'Sending', disabled: true});
2011-12-18 09:27:15 +00:00
pandora.api.mail({
to: getTo(),
2011-12-21 15:34:28 +00:00
subject: getFormItemById('subject').value(),
message: getFormItemById('message').value(),
2011-12-18 09:27:15 +00:00
receipt: getFormItemById('receipt').value()
}, function(result) {
var title = result.status.code == 200
? 'Message Sent'
: 'Application Error',
message = result.status.code == 200
? 'Your message has been sent.'
: 'Your message could not be sent. Please try again.',
$dialog = Ox.Dialog({
buttons: [
Ox.Button({
id: 'close',
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
// FIXME: we need a template for this type of dialog
content: Ox.Element()
.append(
$('<img>')
.attr({src: '/static/png/icon.png'})
2011-12-18 09:27:15 +00:00
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
$('<div>')
.css({position: 'absolute', left: '96px', top: '16px', width: '192px'})
.html(message)
),
height: 128,
keys: {enter: 'close', escape: 'close'},
removeOnClose: true,
title: title,
width: 304
}).open();
$sendButton.options({title: 'Send', disabled: false});
2011-12-18 09:27:15 +00:00
});
}
function setHeight(data) {
2011-12-21 15:34:28 +00:00
var form = $formButton.value(),
2011-12-18 09:27:15 +00:00
$item = getFormItemById(form == 'edit' ? 'notes' : 'message');
dialogHeight = data.height;
$item && $item.options({
height: dialogHeight - (form == 'edit' ? 160 : 208)
});
}
function setLabel() {
var users = $list.options('selected').map(function(id) {
return $list.value(id);
}),
title = users.length == 0 ? 'No user selected'
: users.length == 1 ? (
['guest', 'robot'].indexOf(users[0].level) > -1
? Ox.toTitleCase(users[0].level)
: Ox.encodeHTMLEntities(users[0].username)
+ ' &lt;' + users[0].email + '&gt;'
2011-12-18 09:27:15 +00:00
)
: users.length + ' users selected';
$formLabel.options({title: title});
}
function setSend() {
getFormItemById('send').options({
2011-12-21 15:34:28 +00:00
disabled: getFormItemById('to').value() == 'No recipients'
|| getFormItemById('subject').value() === ''
|| getFormItemById('message').value() === ''
2011-12-18 09:27:15 +00:00
});
}
function setTo() {
var recipients = getTo().length;
$mailForm.values({
to: (recipients || 'No') + ' recipient' + (recipients == 1 ? '' : 's')
});
}
function setWidth() {
var $form = $formButton.value() == 'edit' ? $editForm : $mailForm;
2011-12-18 09:27:15 +00:00
formWidth = $content.size(1);
$formLabel.options({width: formWidth - 44});
$form && $form.options('items').forEach(function($item) {
2011-12-18 09:27:15 +00:00
if ($item.options('id') != 'send') {
$item.options({width: formWidth - 16});
}
});
$status.css({right: formWidth + 128 + 'px'});
2011-10-03 09:31:20 +00:00
}
function updateList() {
var guests = $guestsCheckbox.value(),
robots = $robotsCheckbox.value(),
key = $findSelect.value(),
value = $findInput.value(),
query = {
conditions: value
2012-05-24 08:22:56 +00:00
? [].concat(
key != 'email' ? [{key: 'username', value: value, operator: '='}] : [],
key != 'username' ? [{key: 'email', value: value, operator: '='}] : []
)
2012-05-24 08:22:56 +00:00
: [].concat(
!guests ? [{key: 'level', value: 'guest', operator: '!='}] : [],
!robots ? [{key: 'level', value: 'robot', operator: '!='}] : []
),
operator: key == 'all' && value ? '|' : '&'
2011-10-03 09:31:20 +00:00
};
$list.options({
2012-06-20 10:47:15 +00:00
query: query
2011-10-29 17:46:46 +00:00
});
2011-10-03 09:31:20 +00:00
}
return that;
2011-10-03 09:31:20 +00:00
};