Files
IPB/Development/javascript_uncompressed/skin_acp/clientscripts/ipd_titleedit.js
T
2025-12-19 00:26:08 -08:00

559 lines
13 KiB
JavaScript

//------------------------------------------------------------------------------
// IPS JS: Content Pages Functions
// (c) 2005 Invision Power Services, Inc.
// http://www.invisionpower.com
// Matt Mecham (27/07/05)
//------------------------------------------------------------------------------
var _this;
/*-------------------------------------------------------------------------*/
// Initiate object
/*-------------------------------------------------------------------------*/
function titleedit()
{
this.var_ajax_url = null;
// Click delay
this.var_click_delay = 1200;
this.var_link_id_text = 'pages-link';
this.var_span_id_text = 'pages-page';
this.var_div_main = 'pages-main';
// Language
this.lang_click_and_hold = null;
// Internal stuff
this._tid_timeout = null;
this._new_text = null;
this._tid_links_init = 0;
this._tid_date = new Array();
this._span_contents = new Array();
this._link_contents = new Array();
/**
* Array of uneditable IDs
*
*/
this.UNEDITABLES = new Array();
/**
* ID part of uneditable alert box
*
*/
this.uneditable_id_text = 'titleedit-uneditable';
// Set up divs
this.div_main_wrap = document.getElementById( this.var_div_main );
/**
* Type of edit box required
* Allowed: text, textarea
*/
this.edit_box = 'text';
this.edit_box_ta_rows = 5;
this.edit_box_ta_cols = 45;
/**
* Allows the use of a secret DIV
* to keep the content updated
* Leave blank to not use
*/
this.var_real_id_text = '';
}
/*-------------------------------------------------------------------------*/
// Initiate object
/*-------------------------------------------------------------------------*/
titleedit.prototype.init = function()
{
//----------------------------------
// Get page links...
// Gah.. another safari bug
//----------------------------------
var pagelinks = document.getElementsByTagName('*');
_this = this;
//----------------------------------
// Sort through and grab topic links
//----------------------------------
for ( var i = 0 ; i <= pagelinks.length ; i++ )
{
try
{
if ( ! pagelinks[i].id )
{
continue;
}
}
catch(e)
{
continue;
}
var linkid = pagelinks[i].id;
var linkname = linkid.replace( /^(.*)-(\d+)$/, "$1" );
var linknum = linkid.replace( /^(.*)-(\d+)$/, "$2" );
if ( linkname == this.var_link_id_text )
{
if ( this.UNEDITABLES[ linknum ] )
{
pagelinks[i].onmouseover = this.title_mouseover;
pagelinks[i].onmouseout = this.title_mouseout;
}
else
{
pagelinks[i].onmousedown = this.title_mousedown;
pagelinks[i].onmouseup = this.title_mouseup;
if ( ! this._tid_links_init )
{
pagelinks[i].title = pagelinks[i].title ? pagelinks[i].title + ". " + this.lang_click_and_hold : this.lang_click_and_hold;
}
}
}
}
this._tid_links_init = 1;
};
/*--------------------------------------------*/
// Mouseover: Show alert
/*--------------------------------------------*/
titleedit.prototype.title_mouseover = function( event )
{
//----------------------------------
// Prevent link loading
//----------------------------------
event = ipsclass.cancel_bubble( event, true );
//----------------------------------
// Get TID
//----------------------------------
var tid = _this.get_tid_from_id( this.id );
//-------------------------------
// Unmoveable?
//-------------------------------
var check_for_id = _this.UNEDITABLES[ tid ];
if ( check_for_id )
{
var alert_object = null;
//-------------------------------
// Attempt to get DIV
//-------------------------------
try
{
alert_object = document.getElementById( _this.uneditable_id_text );
}
catch(error)
{
}
//-------------------------------
// Yeah, so draw alert and return
//-------------------------------
if ( alert_object )
{
var root_obj = document.getElementById( _this.var_link_id_text + '-' + check_for_id );
//-------------------------------
// For some reason, Safari wants
// to be relative..
//-------------------------------
if ( is_safari )
{
root_obj.style.position = 'relative';
}
alert_object.style.top = ipsclass.get_obj_toppos( root_obj ) - 4 + 'px';
alert_object.style.left = ipsclass.get_obj_leftpos( root_obj ) + 5 + root_obj.offsetWidth + 'px';
alert_object.style.position = 'absolute';
alert_object.style.display = '';
}
return false;
}
};
/*--------------------------------------------*/
// Mouseout: Hide alert
/*--------------------------------------------*/
titleedit.prototype.title_mouseout = function( event )
{
//----------------------------------
// Prevent link loading
//----------------------------------
event = ipsclass.cancel_bubble( event, true );
//----------------------------------
// Get TID
//----------------------------------
var tid = _this.get_tid_from_id( this.id );
//-------------------------------
// Unmoveable?
//-------------------------------
var check_for_id = _this.UNEDITABLES[ tid ];
if ( check_for_id )
{
var alert_object = null;
//-------------------------------
// Attempt to get DIV
//-------------------------------
try
{
alert_object = document.getElementById( _this.uneditable_id_text );
}
catch(error)
{
}
//-------------------------------
// Yeah, so draw alert and return
//-------------------------------
if ( alert_object )
{
alert_object.style.display = 'none';
}
return false;
}
};
/*--------------------------------------------*/
// Mousedown, start timer
/*--------------------------------------------*/
titleedit.prototype.title_mousedown = function( event )
{
//----------------------------------
// Prevent link loading
//----------------------------------
event = ipsclass.cancel_bubble( event, true );
//----------------------------------
// Get TID
//----------------------------------
var tid = _this.get_tid_from_id( this.id );
//----------------------------------
// Start...
//----------------------------------
_this._tid_date[ tid ] = _this.get_time_now();
//----------------------------------
// Start loop
//----------------------------------
_this._tid_timeout = setTimeout( "_this.link_event_timer("+tid+")", 5 );
};
/*--------------------------------------------*/
// Event, loop
/*--------------------------------------------*/
titleedit.prototype.link_event_timer = function( tid )
{
//----------------------------------
// Get time now
//----------------------------------
var timenow = this.get_time_now();
//----------------------------------
// Click held for 1.5 s+?
//----------------------------------
if ( timenow > 0 && this._tid_date[tid] > 0 && ( (timenow - this._tid_date[tid] ) > this.var_click_delay ) )
{
//----------------------------------
// Yeah.. wipe tid time and convert
//----------------------------------
this._tid_date[tid] = 0;
this.span_to_input( tid );
return false;
}
//----------------------------------
// Not wiped? loop on!
//----------------------------------
if ( this._tid_date[tid] > 0 )
{
this._tid_timeout = setTimeout( "_this.link_event_timer("+tid+")", 5 );
}
else
{
return false;
}
};
/*--------------------------------------------*/
// Turn topic title into text input
/*--------------------------------------------*/
titleedit.prototype.span_to_input = function( tid )
{
//----------------------------------
// Using fancy js?
//----------------------------------
if ( ! use_enhanced_js )
{
return false;
}
this._span_contents[ tid ] = document.getElementById( this.var_span_id_text + '-' + tid ).innerHTML;
this._link_contents[ tid ] = document.getElementById( this.var_link_id_text + '-' + tid ).innerHTML;
var value = '';
//----------------------------------
// Using hidden div or not?
//----------------------------------
if ( this.var_real_id_text )
{
value = document.getElementById( this.var_real_id_text + '-' + tid ).innerHTML;
}
else
{
value = this._link_contents[ tid ];
}
//----------------------------------
// Show edit box
//----------------------------------
if ( this.edit_box == 'text' )
{
value = value.replace( /"/g, '&quot;');
document.getElementById(this.var_span_id_text + '-' + tid ).innerHTML = '<input id="edit-'+tid+'" class="dny-edit-title" type="text" size="30" onblur="_this.edit_blur(\''+tid+'\')" onkeypress="_this.edit_keypress(event, \''+tid+'\')" value="'+value+'" />';
}
else
{
value = value.replace( /<\/textarea>/g, '&lt;/textarea&gt;');
document.getElementById(this.var_span_id_text + '-' + tid ).innerHTML = '<textarea id="edit-'+tid+'" class="dny-edit-title" cols="'+this.edit_box_ta_cols+'" rows="'+this.edit_box_ta_rows+'" onblur="_this.edit_blur(\''+tid+'\')" onkeypress="_this.edit_keypress(event, \''+tid+'\')">'+value+'</textarea>';
}
document.getElementById( 'edit-' + tid ).focus();
return false;
};
/*--------------------------------------------*/
// Mouseup? Cancel timer to kill the loop
/*--------------------------------------------*/
titleedit.prototype.title_mouseup = function( event )
{
//----------------------------------
// Prevent link loading
//----------------------------------
event = ipsclass.cancel_bubble( event, true );
//----------------------------------
// Get TID
//----------------------------------
var tid = _this.get_tid_from_id( this.id );
//----------------------------------
// Reset timer
//----------------------------------
_this._tid_date[tid] = 0;
};
/*--------------------------------------------*/
// BLUR HANDLER
/*--------------------------------------------*/
titleedit.prototype.edit_blur = function( tid, type )
{
_this._new_text = document.getElementById( 'edit-' + tid ).value;
if ( _this._new_text != "" )
{
_this.title_save( tid, _this._new_text, type );
}
};
/*--------------------------------------------*/
// KEY PRESS HANDLER
/*--------------------------------------------*/
titleedit.prototype.edit_keypress = function( evt, tid, type )
{
evt = (evt) ? evt : window.event;
_this._new_text = document.getElementById( 'edit-' + tid ).value;
// Make sure we're not using a text area
if ( _this.edit_box == 'text' )
{
if ( ( evt.keyCode == 13 || evt.keyCode == 3 ) && _this._new_text != "" )
{
_this.title_save( tid, _this._new_text, type );
}
}
};
/*--------------------------------------------*/
// UDPATE DB
/*--------------------------------------------*/
titleedit.prototype.title_save = function( tid, new_text, type )
{
var donotedit = 0;
if ( new_text == _this._span_contents[ tid ] )
{
donotedit = 1;
}
document.getElementById( _this.var_span_id_text + '-' + tid ).innerHTML = _this._span_contents[ tid ];
document.getElementById( _this.var_link_id_text + '-' + tid ).innerHTML = new_text;
//----------------------------------
// Using hidden div or not?
//----------------------------------
if ( _this.var_real_id_text )
{
document.getElementById( _this.var_real_id_text + '-' + tid ).innerHTML = new_text;
}
//----------------------------------
// Re-check topic links
//----------------------------------
_this.init();
if ( donotedit )
{
return false;
}
var fields = new Array();
//----------------------------------
// Populate fields
//----------------------------------
fields['tid'] = tid;
fields['name'] = new_text;
fields['md5check'] = ipb_md5_check;
/*--------------------------------------------*/
// Main function to do on request
// Must be defined first!!
/*--------------------------------------------*/
do_request_function = function()
{
//----------------------------------
// Ignore unless we're ready to go
//----------------------------------
if ( ! xmlobj.readystate_ready_and_ok() )
{
xmlobj.show_loading("Saving...");
return;
}
xmlobj.hide_loading();
//----------------------------------
// INIT
//----------------------------------
var returned = xmlobj.xmlhandler.responseText;
if ( returned.match( /<null>(0|-|\=)<\/null>/ ) )
{
alert( "Error occured, not saved" );
}
};
//----------------------------------
// LOAD XML
//----------------------------------
xmlobj = new ajax_request();
xmlobj.onreadystatechange( do_request_function );
xmlobj.process( _this.var_ajax_url, 'POST', xmlobj.format_for_post(fields) );
return false;
};
/*--------------------------------------------*/
// Get time now
/*--------------------------------------------*/
titleedit.prototype.get_time_now = function()
{
var mydate = new Date();
return mydate.getTime();
};
/*--------------------------------------------*/
// Get TID from ID
/*--------------------------------------------*/
titleedit.prototype.get_tid_from_id = function( id )
{
return id.replace( /.*\-(\d+)/, "$1" );
};
/*-------------------------------------------------------------------------*/
// Add ID to UNMOVEABLES array
/*-------------------------------------------------------------------------*/
titleedit.prototype.add_id_to_uneditables = function( tid )
{
this.UNEDITABLES[ tid ] = tid;
};