Files
IPB/Development/javascript_uncompressed/jscripts/ips_attach.js
T
2025-12-19 00:26:08 -08:00

667 lines
17 KiB
JavaScript

//------------------------------------------------------------------------------
// IPS JS: Attach Functions
// (c) 2005 Invision Power Services, Inc.
// http://www.invisionpower.com
// Matt Mecham (16/12/05)
//------------------------------------------------------------------------------
var ipsattach = new ips_attach();
function ips_attach()
{
/**
* iFrame id
*/
this.iframe_id = 'ips--iframe-obj-attach';
/**
* iFrame Object
*/
this.iframe_obj = null;
/**
* Init URL
* URL to load to display current attachments, etc
*/
this.iframe_init_url = '';
/**
* URL for removing attachment
*/
this.iframe_remove_url = '';
/**
* The name of the DIV that this iframe is attached to
*/
this.iframe_add_to_div = 'ips-attach-div-iframe';
this.iframe_add_to_div_obj = null;
/**
* The name of the DIV that this iframe is attached to
*/
this.iframe_parent_div = 'ips-attach-div-parent';
this.iframe_parent_div_obj = null;
/**
* The name of the DIV that the status bar is in
*/
this.status_div = 'ips-attach-div-status';
this.status_div_obj = null;
/**
* The name of the DIV that the dropdown menu text is in
*/
this.dropdown_text = 'ips-attach-menu-text';
this.dropdown_text_obj = null;
/**
* Default iframe height
*/
this.iframe_height = 300;
/**
* css class names
*/
this.iframe_classname = 'attach-iframe';
/**
* Message cover
*/
this.message_div_id = 'ips-attach-message';
this.message_div_obj = null;
this.message_span_id = 'ips-attach-message-span';
this.message_span_obj = null;
/**
* Default Status message
*/
this.status_msg = '';
this.status_is_error = '';
/**
* Current items array
*/
this.current_items = new Array();
/**
* Current items' menu
*/
this.current_item_menu = 'ips-attach-menu';
this.current_item_menu_obj = null;
/**
* Images URL
*/
this.images_url = null;
/**
* Language array
*/
this.lang = new Array();
/**
* CSS classes
*/
this.css_menu_row = 'attach-menu-row';
this.css_menu_row_over = 'attach-menu-row-over';
/**
* Show attachments menu
*/
this.show_attach_menu = 1;
/**
* Onload command
*/
this.onload_function = '';
/**
* Has an upload...
*/
this.has_upload_pending = 0;
/**
* INIT
*/
this.init = function()
{
//-----------------------------------------
// INIT objects
//-----------------------------------------
this.iframe_add_to_div_obj = document.getElementById( this.iframe_add_to_div );
this.iframe_parent_div_obj = document.getElementById( this.iframe_parent_div );
this.message_div_obj = document.getElementById( this.message_div_id );
this.message_span_obj = document.getElementById( this.message_span_id );
this.status_div_obj = document.getElementById( this.status_div );
this.dropdown_text_obj = document.getElementById( this.dropdown_text );
//-----------------------------------------
// INIT Current button
//-----------------------------------------
if ( this.show_attach_menu )
{
// INIT div
this.current_item_menu_obj = document.getElementById( this.current_item_menu );
ipsmenu.register( this.current_item_menu );
this.current_item_menu_obj._onclick = this.current_item_menu_obj.onclick;
this.current_item_menu_obj.onmouseover = this.current_item_menu_obj.onmouseout = this.current_item_menu_obj.onclick = this.current_menu_mouseover;
ipsmenu.menu_registered[ this.current_item_menu ]._open = ipsmenu.menu_registered[ this.current_item_menu ].open;
ipsmenu.menu_registered[ this.current_item_menu ].open = this.current_menu_button_show;
}
};
/**
* Dynamically builds the HTML for the IPS Menu system
*
* @var object Javascript object clicked
*/
this.init_current_menu = function( obj )
{
//-----------------------------------------
// Old menu?
//-----------------------------------------
var menu = '';
try
{
menu = document.getElementById( ipsattach.current_item_menu + '_menu' );
menu.innerHTML = '';
}
catch(error)
{
menu = document.createElement('div');
}
//-----------------------------------------
// Create menu wrapper
//-----------------------------------------
var _items = 0;
menu.id = ipsattach.current_item_menu + '_menu';
menu.className = 'attach-popupmenu';
menu.style.display = 'none';
menu.style.cursor = 'default';
menu.style.padding = '3px';
menu.style.width = 'auto';
menu.style.overflow = 'auto';
//-----------------------------------------
// What are we doing?
//-----------------------------------------
for( var i in ipsattach.current_items )
{
var option = document.createElement('div');
option.innerHTML = ipsattach.current_items[i];
option.style.width = ipsattach.current_item_menu_obj.style.width;
option.className = 'attach-menu-row';
option.onmouseover = option.onmouseout = ipsattach.menu_onmouse_event;
menu.appendChild(option);
_items++;
}
if ( ! _items )
{
var option = document.createElement('div');
option.innerHTML = ipsattach.lang['no_items'];
option.style.width = ipsattach.current_item_menu_obj.style.width;
option.className = 'attach-menu-row';
option.onmouseover = option.onmouseout = ipsattach.menu_onmouse_event;
menu.appendChild(option);
}
// Set status counter correct
ipsattach.set_attach_count_status( _items );
ipsattach.iframe_parent_div_obj.appendChild(menu);
ipsclass.set_unselectable( menu );
};
/**
* Recounts number of current attachments and sets counter
*
* @param integer Cached count
*/
this.set_attach_count_status = function( _items )
{
if ( ! ipsattach.show_attach_menu )
{
return;
}
if ( typeof( _items ) == 'undefined' )
{
_items = 0;
for( var i in ipsattach.current_items )
{
_items++;
}
}
try
{
ipsattach.dropdown_text_obj.innerHTML = " (" + _items + ")";
}
catch(error)
{
}
};
/**
* Add item into current_attachments menu
*/
this.add_current_item = function( attach_id, attach_name, attach_size, attach_image )
{
//-----------------------------------------
// Fix up attach_name
//-----------------------------------------
if ( attach_name.length > 22 )
{
var _ext = attach_name.replace( /^.*\.(\S+?)$/ , "$1" );
var _main = attach_name.replace( /^(.*)\.\S+?$/ , "$1" );
var _a = _main.substr( 0, 8 );
var _b = _main.substr( _main.length - 8 , _main.length );
attach_name = _a + '...' + _b + '.' + _ext;
}
var html = '';
html += '<img onclick="ipsattach.add_attachment_into_editor(' + attach_id + ', \'' + attach_name + '\')" src="' + this.images_url + '/folder_attach_images/attach_add.png" title="' + this.lang['attach_insert'] + '" style="vertical-align:middle" border="0" alt="" /> &nbsp;';
html += '<img onclick="ipsattach.remove_attachment(' + attach_id + ');" src="' + this.images_url + '/folder_attach_images/attach_remove.png" title="' + this.lang['attach_remove'] + ': [' + attach_id + ']' + '" style="vertical-align:middle" border="0" alt="" />&nbsp;';
html += '<img src="' + this.images_url + '/' + attach_image + '" style="vertical-align:middle" border="0" alt="" /> &nbsp;';
html += '<span style="font-weight:bold" onclick="ipsattach.add_attachment_into_editor(' + attach_id + ', \'' + attach_name + '\')">' + attach_name + '</span>';
html += ' <span class="desctext">&nbsp;('+attach_size+')</span>';
this.current_items[ this.current_items.length ] = html;
};
/**
* Adds the IPD attach tag into the current editor
*
* @param integer Attachment ID
* @param string Attachment name
*/
this.add_attachment_into_editor = function( id, name )
{
// Form tag
var tag = "[attachment=" + id + ":" + name + "]";
// Add
ipsclass.add_editor_contents( tag );
};
/**
* Removes an attachment
*
* @param integer Attachment ID
*/
this.remove_attachment = function( id )
{
menu_action_close();
if ( confirm( ipsattach.lang['remove_warn'] + ' ['+ id + ']' ) )
{
ipsattach.iframe_obj.src = ipsattach.iframe_remove_url + "&attach_id=" + id;
ipsattach.iframe_on_un_load( 'attach_removal' );
}
};
/**
* Show upload field and attachments
* Draws the iFrame and sets the source
*/
this.show_attach_box = function()
{
//-----------------------------------------
// INIT
//-----------------------------------------
var iheight = parseInt( this.iframe_add_to_div_obj.style.height );
var iwidth = this.iframe_add_to_div_obj.style.width;
//-----------------------------------------
// Generate iFrame box
//-----------------------------------------
if ( ! this.iframe_obj )
{
this.iframe_obj = document.createElement( 'IFRAME' );
this.iframe_obj.src = this.iframe_init_url;
this.iframe_obj.id = this.iframe_id;
this.iframe_obj.name = this.iframe_id;
this.iframe_obj.scrolling = 'no';
this.iframe_obj.frameBorder = 'no';
this.iframe_obj.border = '0';
this.iframe_obj.className = this.iframe_classname;
this.iframe_obj.style.width = iwidth ? iwidth : '100%';
this.iframe_obj.style.height = iheight ? iheight - 5 + 'px' : this.iframe_height + 'px';
this.iframe_obj.style.overflow = 'hidden';
this.iframe_obj.style.display = '';
// Add environmentals..
this.iframe_obj.msg_open = 1;
this.iframe_obj.iframe_loaded = 0;
this.iframe_obj.iframe_init = 0;
// Attach iFrame inside our DIV
this.iframe_add_to_div_obj.appendChild( this.iframe_obj );
// Add handler
if ( is_ie )
{
this.iframe_obj.style.backgroundColor = 'transparent';
this.iframe_obj.allowTransparency = true;
this.iframe_obj.onreadystatechange = this.iframe_on_load_ie;
}
else
{
this.iframe_obj.onload = this.iframe_onload;
}
//-----------------------------------------
// Safari bug: Onload events don't fire
// when the parent DIV is hidden.
//-----------------------------------------
if ( is_safari )
{
this.iframe_parent_div_obj.style.display = '';
}
else
{
this.iframe_parent_div_obj.style.display = 'none';
}
}
//-----------------------------------------
// INIT cover div
//-----------------------------------------
if ( typeof this.message_div_obj != 'undefined' )
{
this.message_div_obj.style.width = typeof this.iframe_parent_div_obj.style.width != 'undefined' ? this.iframe_parent_div_obj.style.width : '100%';
this.message_div_obj.style.height = this.iframe_parent_div_obj.offsetHeight ? this.iframe_parent_div_obj.offsetHeight : '120px';
this.message_div_obj.style.position = 'relative';
this.message_div_obj.style.display = 'none';
}
//-----------------------------------------
// Show cover div during INIT
//-----------------------------------------
this.show_message( this.lang['init_progress'] );
};
/**
* Draws status bar
* Using "ipsattach" as is called in an event function
*/
this.init_status_bar = function()
{
//-----------------------------------------
// INIT
//-----------------------------------------
var _html = '';
if ( typeof ipsattach.status_div_obj != 'undefined' )
{
try
{
ipsattach.status_msg = ipsattach.lang[ ipsattach.status_msg ];
}
catch(error) { }
if ( ipsattach.status_msg )
{
if ( ipsattach.status_is_error )
{
_html += "<img src='" + this.images_url + "/folder_attach_images/attach_error.png' border='0' style='vertical-align:middle' alt='" + ipb_global_lang['general_error'] + "' />&nbsp;" + ipsattach.status_msg;
}
else
{
_html += "<img src='" + this.images_url + "/folder_attach_images/attach_ok.png' border='0' style='vertical-align:middle' alt='" + ipb_global_lang['general_OK'] + "' />&nbsp;" + ipsattach.status_msg;
}
}
ipsattach.status_div_obj.innerHTML = _html;
}
//-----------------------------------------
// Reset pending...
//-----------------------------------------
ipsattach.has_upload_pending = 0;
};
/**
* Manually show the iframe message
*/
this.iframe_on_un_load = function( msg )
{
//-----------------------------------------
// Message...
//-----------------------------------------
msg = typeof(msg) != 'undefined' ? ipsattach.lang[ msg ] : ipsattach.lang['uploading_file'];
//-----------------------------------------
// Show...
//-----------------------------------------
if ( this.iframe_obj.iframe_loaded )
{
this.iframe_obj.iframe_loaded = 0;
this.iframe_obj.msg_open = 1;
ipsattach.show_message( msg );
}
};
/**
* Each time the window is loaded, close
* any open messages
*/
this.iframe_onload = function( e )
{
//-----------------------------------------
// First load?
//-----------------------------------------
if ( ! this.iframe_init )
{
this.iframe_init = 1;
this.iframe_loaded = 1;
if ( this.msg_open )
{
this.msg_open = 0;
ipsattach.hide_message();
}
window.frames[ this.id ].document.onmousedown = menu_action_close;
}
else
{
this.iframe_loaded = 1;
window.frames[ this.id ].document.onmousedown = menu_action_close;
if ( this.msg_open )
{
this.msg_open = 0;
ipsattach.hide_message();
}
//-----------------------------------------
// Run command?
//-----------------------------------------
if ( ipsattach.onload_function )
{
ipsattach.onload_function();
}
}
// INIT stuff
ipsattach.init_status_bar();
// Set status counter correct
ipsattach.set_attach_count_status();
};
/**
* IE only calls onload once... So subsequent loads
* don't do anything.. hence the readystate change
*/
this.iframe_on_load_ie = function( e )
{
//-----------------------------------------
// First load?
//-----------------------------------------
if ( this.readyState == 'complete' )
{
if ( ! this.iframe_init )
{
this.iframe_init = 1;
this.iframe_loaded = 1;
window.frames[ this.id ].document.onmousedown = menu_action_close;
if ( this.msg_open )
{
this.msg_open = 0;
ipsattach.hide_message();
}
}
else
{
this.iframe_loaded = 1;
window.frames[ this.id ].document.onmousedown = menu_action_close;
if ( this.msg_open )
{
this.msg_open = 0;
ipsattach.hide_message();
}
}
//window.frames[ this.id ].document.getElementsByTagName('html')[0].allowTransparency = true;
window.frames[ this.id ].document.getElementsByTagName('html')[0].style.background = 'transparent';
//alert( window.frames[ this.id ].document.getElementsByTagName('html')[0].outerHTML );
//-----------------------------------------
// Run command?
//-----------------------------------------
if ( ipsattach.onload_function )
{
ipsattach.onload_function();
}
// INIT stuff
ipsattach.init_status_bar();
// Set status counter correct
ipsattach.set_attach_count_status();
}
};
/**
* Show message DIV
*/
this.show_message = function( msg )
{
//-----------------------------------------
// Safari bug: Onload events don't fire
// when the parent DIV is hidden.
//-----------------------------------------
if ( is_safari )
{
return;
}
if ( typeof ipsattach.message_div_obj != 'undefined' )
{
ipsattach.message_div_obj.style.display = '';
ipsattach.message_span_obj.innerHTML = msg;
// Hide other div
ipsattach.iframe_parent_div_obj.style.display = 'none';
}
};
/**
* Hide message DIV
*/
this.hide_message = function()
{
//-----------------------------------------
// Safari bug: Onload events don't fire
// when the parent DIV is hidden.
//-----------------------------------------
if ( is_safari )
{
return;
}
if ( typeof ipsattach.message_div_obj != 'undefined' )
{
ipsattach.message_div_obj.style.display = 'none';
ipsattach.message_span_obj.innerHTML = '';
// Show other div
ipsattach.iframe_parent_div_obj.style.display = '';
}
};
/**
* Mouseover events
*
* @param Event Object
*/
this.current_menu_mouseover = function(e)
{
e = ipsclass.cancel_bubble(e, true);
if ( e.type == 'click' )
{
this._onclick(e);
}
};
/**
* Shows the pop-up menu
* First, dynamically writes menu - then passes to ipsmenu class to show
*
* @param Object Object
*/
this.current_menu_button_show = function(obj)
{
ipsattach.init_current_menu(obj);
this._open(obj);
};
/**
* Roll over effect for the menu
* ^^
*
* @param Event Object
*/
this.menu_onmouse_event = function( e )
{
e = ipsclass.cancel_bubble(e, true);
if ( e.type == 'mouseover' )
{
this.className = ipsattach.css_menu_row_over;
}
else if ( e.type == 'mouseout' )
{
this.className = ipsattach.css_menu_row;
}
};
}