//$Id$
/**
* Global Variables
*/
var am = new Array();
var am_tinymce = false;
am['filter'] = new Array();
if (Drupal.jsEnabled) {
$(document).ready(function() {
$('#node-form').ajaxForm({
url: '/am/save',
dataType: 'json',
success: amReturn
});
$('#edit-upload').hide();
$('#am-tabs-add-node a').click(function(cur) {
$('#am-existing').addClass('hide');
$('#am-node-add').removeClass('hide');
$('#am-tabs li').removeClass('on');
$('#am-tabs-add-node').addClass('on');
$('body').removeClass('body-search');
return false;
});
$('#am-tabs-existing a').click(function(cur) {
$('#am-node-add').addClass('hide');
$('#am-existing').removeClass('hide');
$('#am-tabs li').removeClass('on');
$('#am-tabs-existing').addClass('on');
$('body').addClass('body-search');
return false;
});
updateTypeOptions();
am['filter']['type'] = $('#node-type-filter option:selected').val();
am['filter']['tid'] = $('#node-channel-filter option:selected').val();
/* IE doesn't pick up the .click so we have to use .change on the parent element
* and then drill down into the options and find [@selected]
*/
if ($.browser.msie) {
$('#node-channel-filter').change(function() {
updateNodeTid($(this).children("[@selected]"));
});
$('#node-results').change(function() {
showNodePreview($(this).children("[@selected]"));
});
// ATTIKS
$('#imagecachepresets').change(function() {
updateCachePreset($(this).children("[@selected]"));
});
}
else {
$('#node-channel-filter option').click(function() {
updateNodeTid(this);
});
$('#node-results').click(function() {
showNodePreview(this);
});
// ATTIKS
$('#imagecachepresets').click(function() {
updateCachePreset(this);
});
}
$('#node-name-filter').keyup(updateNodeFilter);
$('#am-select-form').submit(selectNode);
updateTypeOptions();
am['filter']['type'] = $('#node-type-filter option:selected').val();
am['filter']['tid'] = $('#node-channel-filter option:selected').val();
// ATTIKS
am['filter']['imagecache'] = $('#imagecachepresets option:selected').val();
});
}
// ATTIKS
function updateCachePreset(select) {
am['filter']['imagecache'] = $(select).val();
}
function updateNodeTid(select) {
am['filter']['tid'] = $(select).val();
refreshList();
}
function updateNodeType(select) {
am['filter']['type'] = $(select).val();
refreshList();
}
function updateNodeFilter() {
am['filter']['name'] = $(this).val();
refreshList();
}
function refreshList() {
am['url'] = '/am/list/' + am['filter']['type'] + '/' + am['filter']['tid'];
$.getJSON(am['url'],function(obj){
if (obj.length) {
am['nodes'] = filterList(new Array(obj.nodes));
}
else {
am['nodes'] = filterList(obj.nodes);
}
updateList(am['nodes']);
});
}
function updateList(nodes) {
var results = '';
if (!nodes || nodes.length == 0) {
results += 'No Results';
$("#am-results").html(results);
}
else {
for (var i=0;i < nodes.length;i++) {
results += '' + nodes[i].thumb;
results += '' + nodes[i].title + ', ' + nodes[i].type + '
';
}
$("#am-results").html(results);
showNodePreview(nodes[0].nid);
}
}
function filterList(nodes) {
var output = new Array()
if (!am['filter']['name']) {
return nodes;
}
for (var i=0;i < nodes.length;i++) {
var nodeTitle = nodes[i].title.toLowerCase();
if (am['filter']['name']) {
if (nodeTitle.substr(0,am['filter']['name'].length) == am['filter']['name'].toLowerCase()) {
output.push(nodes[i]);
}
}
else {
return nodes;
}
}
return output;
}
function showNodePreview(nid) {
if (am['nodes']) {
for (var i=0;i< am['nodes'].length;i++) {
if (am['nodes'][i].nid == nid) {
previewNode(am['nodes'][i]);
/* !! TODO !!
* This is sort of an odd place for the following 2 lines since it has nothing to do with the previewing
* of the node, will put it here for now, and refactor the onclick later.
*/
$('#am-results > a').removeClass('on');
$('#node-' + nid).addClass('on');
am['node'] = am['nodes'][i];
}
}
}
}
function previewNode(node) {
$('#am-preview p').html('' + node.content + 'Select this image');
}
function selectNode() {
if (!am['node']) {
alert('You must select an asset from the left hand controls in order to continue.');
return false;
}
if (!am_tinymce) {
amUpdateTarget(am['node']['nid'], am['node']['title'], am['filter']['imagecache']);
}
else {
amUpdateTinyMCE(am['node']['nid'], am['filter']['imagecache']);
}
return false;
}
function updateTypeOptions() {
var options = '';
var field_name;
var url = '/am/fieldinfo';
if (!am_tinymce) {
field_name = $('.am-target',window.opener.document).attr('name');
am['type'] = field_name.substr(0,field_name.indexOf('['));
url += '/' + am['type'];
}
$.getJSON(url,function(obj){
if (obj.types) {
options += '';
for (key in obj.types) {
options += '';
}
$("#node-type-filter").html(options);
$('#node-type-filter option:first').attr('selected', 'selected');
if ($.browser.msie) {
$('#node-type-filter').change(function() {
updateNodeType($(this).children("[@selected]"));
});
}
else {
$('#node-type-filter option').click(function() {
updateNodeType(this);
});
}
}
});
}
function amUpdateTarget(nid, node_title, cachepreset) {
var update = node_title + ' [nid:' + nid + ']'; // ATTIKS Not a good idea, displaying has to be handled by template file + ' [cachepreset:' + cachepreset + ']';
$('.am-target',window.opener.document).val(update);
$('.am-target',window.opener.document).removeClass('am-target');
window.close();
}
function amUpdateTinyMCE(nid, cachepreset) {
$('#asset-popup').html('please wait');
$.getJSON('/am/node_load/' + nid + '/' + cachepreset,function(obj){
alert (obj);
am['inline'] = obj;
insertToEditor(am['inline']);
window.close();
});
}
function amReturn(data, text) {
if (data) {
d=data['data'];
if (!am_tinymce) {
amUpdateTarget(d['nid'], d['title'], am['filter']['imagecache']);
}
else {
amUpdateTinyMCE(d['nid'], am['filter']['imagecache']);
}
}
return false;
}