Untitled diff

Created Diff never expires
58 removals
352 lines
56 additions
349 lines
/*##################################################
/**
* bbcode.js
* @copyright © 2005-2019 PHPBoost
* -------------------
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
* begin : August 01, 2005
* @author Regis VIARRE <crowkait@phpboost.com>
* copyright : (C) 2005 Viarre Régis
* @version PHPBoost 5.2 - last update: 2018 11 18
* email : crowkait@phpboost.com
* @since PHPBoost 1.2 - 2005 08 01
*
* @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*
* @contributor Arnaud GENET <elenwii@phpboost.com>
###################################################
* @contributor Sebastien LARTIGUE <babsolune@phpboost.com>
*
*/
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
###################################################*/
function textarea_resize(id, px, type)
function textarea_resize(id, px, type)
{
{
var textarea = document.getElementById(id);
var textarea = document.getElementById(id);
if( type == 'height' )
if( type == 'height' )
{
{
var current_height = parseInt(textarea.style.height) ? parseInt(textarea.style.height) : 300;
var current_height = parseInt(textarea.style.height) ? parseInt(textarea.style.height) : 300;
var new_height = current_height + px;
var new_height = current_height + px;
if( new_height > 40 )
if( new_height > 40 )
textarea.style.height = new_height + "px";
textarea.style.height = new_height + "px";
}
}
else
else
{
{
var current_width = parseInt(textarea.style.width) ? parseInt(textarea.style.width) : 150;
var current_width = parseInt(textarea.style.width) ? parseInt(textarea.style.width) : 150;
var new_width = current_width + px;
var new_width = current_width + px;
if( new_width > 40 )
if( new_width > 40 )
textarea.style.width = new_width + "px";
textarea.style.width = new_width + "px";
}
}
return false;
return false;
}
}
//Insertion dans le champs.
//Insertion dans le champs.
function simple_insert(open_balise, close_balise, field)
function simple_insert(open_balise, close_balise, field)
{
{
var textarea = document.getElementById(field);
var textarea = document.getElementById(field);
var scroll = textarea.scrollTop;
var scroll = textarea.scrollTop;
if( close_balise != "" && close_balise != "smile" )
if( close_balise != "" && close_balise != "smile" )
textarea.value += '[' + open_balise + '][/' + close_balise + ']';
textarea.value += '[' + open_balise + '][/' + close_balise + ']';
else if( close_balise == "smile" )
else if( close_balise == "smile" )
textarea.value += ' ' + open_balise + ' ';
textarea.value += ' ' + open_balise + ' ';
textarea.focus();
textarea.focus();
textarea.scrollTop = scroll;
textarea.scrollTop = scroll;
return;
return;
}
}
//Récupération de la sélection sur netscape, ajout des balises autour.
//Récupération de la sélection sur netscape, ajout des balises autour.
function netscape_sel(target, open_balise, close_balise)
function netscape_sel(target, open_balise, close_balise)
{
{
var sel_length = target.textLength;
var sel_length = target.textLength;
var sel_start = target.selectionStart;
var sel_start = target.selectionStart;
var sel_end = target.selectionEnd;
var sel_end = target.selectionEnd;
var scroll = target.scrollTop; //Position verticale.
var scroll = target.scrollTop; //Position verticale.
if( sel_end == 1 || sel_end == 2 )
if( sel_end == 1 || sel_end == 2 )
{
{
sel_end = sel_length;
sel_end = sel_length;
}
}
var string_start = (target.value).substring(0, sel_start);
var string_start = (target.value).substring(0, sel_start);
var selection = (target.value).substring(sel_start, sel_end);
var selection = (target.value).substring(sel_start, sel_end);
var string_end = (target.value).substring(sel_end, sel_length);
var string_end = (target.value).substring(sel_end, sel_length);
if( close_balise != "" && selection == "" && close_balise != "smile" )
if( close_balise != "" && selection == "" && close_balise != "smile" )
{
{
target.value = string_start + open_balise + close_balise + string_end;
target.value = string_start + open_balise + close_balise + string_end;
target.setSelectionRange(string_start.length + open_balise.length, target.value.length - string_end.length - close_balise.length);
target.setSelectionRange(string_start.length + open_balise.length, target.value.length - string_end.length - close_balise.length);
target.focus();
target.focus();
}
}
else if( close_balise == "smile" )
else if( close_balise == "smile" )
{
{
target.value = string_start + selection + ' ' + open_balise + ' ' + string_end;
target.value = string_start + selection + ' ' + open_balise + ' ' + string_end;
target.setSelectionRange(string_start.length + open_balise.length + 2, target.value.length - string_end.length);
target.setSelectionRange(string_start.length + open_balise.length + 2, target.value.length - string_end.length);
target.focus();
target.focus();
}
}
else
else
{
{
target.value = string_start + open_balise + selection + close_balise + string_end;
target.value = string_start + open_balise + selection + close_balise + string_end;
target.setSelectionRange(string_start.length + open_balise.length, target.value.length - string_end.length - close_balise.length);
target.setSelectionRange(string_start.length + open_balise.length, target.value.length - string_end.length - close_balise.length);
target.focus();
target.focus();
}
}
target.scrollTop = scroll; //Remet à la bonne position le textarea.
target.scrollTop = scroll; //Remet à la bonne position le textarea.
return;
return;
}
}
//Récupération de la sélection sur IE, ajout des balises autour.
//Récupération de la sélection sur IE, ajout des balises autour.
function ie_sel(target, open_balise, close_balise)
function ie_sel(target, open_balise, close_balise)
{
{
selText = false;
selText = false;
var scroll = target.scrollTop; //Position verticale.
var scroll = target.scrollTop; //Position verticale.
selection = document.selection.createRange().text; // Sélection
selection = document.selection.createRange().text; // Sélection
if( close_balise != "" && selection == "" && close_balise != "smile" )
if( close_balise != "" && selection == "" && close_balise != "smile" )
document.selection.createRange().text = open_balise + close_balise;
document.selection.createRange().text = open_balise + close_balise;
else if( close_balise == "smile" )
else if( close_balise == "smile" )
document.selection.createRange().text = selection + open_balise + ' ';
document.selection.createRange().text = selection + open_balise + ' ';
else
else
document.selection.createRange().text = open_balise + selection + close_balise;
document.selection.createRange().text = open_balise + selection + close_balise;
target.scrollTop = scroll; //Remet à la bonne position le textarea.
target.scrollTop = scroll; //Remet à la bonne position le textarea.
selText = '';
selText = '';
return;
return;
}
}
//Fonction de remplacement des caractères spéciaux
//Fonction de remplacement des caractères spéciaux
function url_encode_rewrite(link_name)
function url_encode_rewrite(link_name)
{
{
link_name = link_name.toLowerCase(link_name);
link_name = link_name.toLowerCase(link_name);
var chars_special = new Array(/ /g, /é/g, /è/g, /ê/g, /à/g, /â/g, /ù/g, /ü/g, /û/g, /ï/g, /î/g, /ô/g, /ç/g);
var chars_special = new Array(/ /g, /é/g, /è/g, /ê/g, /à/g, /â/g, /ù/g, /ü/g, /û/g, /ï/g, /î/g, /ô/g, /ç/g);
var chars_replace = new Array("-", "e", "e", "e", "a", "a", "u", "u", "u", "i", "i", "o", "c");
var chars_replace = new Array("-", "e", "e", "e", "a", "a", "u", "u", "u", "i", "i", "o", "c");
var nbr_chars = chars_special.length;
var nbr_chars = chars_special.length;
for( var i = 0; i < nbr_chars; i++)
for( var i = 0; i < nbr_chars; i++)
{
{
link_name = link_name.replace(chars_special[i], chars_replace[i]);
link_name = link_name.replace(chars_special[i], chars_replace[i]);
}
}
link_name = link_name.replace(/([^a-z0-9]|[\s])/g, '-');
link_name = link_name.replace(/([^a-z0-9]|[\s])/g, '-');
link_name = link_name.replace(/([-]{2,})/g, '-');
link_name = link_name.replace(/([-]{2,})/g, '-');
return link_name.replace(/(^\s*)|(\s*$)/g,'').replace(/(^-)|(-$)/g,'');
return link_name.replace(/(^\s*)|(\s*$)/g,'').replace(/(^-)|(-$)/g,'');
}
}
//Fonction d'insertion du BBcode dans le champs, tient compte du navigateur utilisé.
//Fonction d'insertion du BBcode dans le champs, tient compte du navigateur utilisé.
function insertbbcode(open_balise, close_balise, field)
function insertbbcode(open_balise, close_balise, field)
{
{
var area = document.getElementById(field);
var area = document.getElementById(field);
var nav = navigator.appName; //Recupère le nom du navigateur
var nav = navigator.appName; //Recupère le nom du navigateur
area.focus();
area.focus();
if( nav == 'Microsoft Internet Explorer' ) // Internet Explorer
if( nav == 'Microsoft Internet Explorer' ) // Internet Explorer
ie_sel(area, open_balise, close_balise);
ie_sel(area, open_balise, close_balise);
else if( nav == 'Netscape' || nav == 'Opera' ) //Netscape ou opera
else if( nav == 'Netscape' || nav == 'Opera' ) //Netscape ou opera
netscape_sel(area, open_balise, close_balise);
netscape_sel(area, open_balise, close_balise);
else //insertion normale (autres navigateurs)
else //insertion normale (autres navigateurs)
simple_insert(open_balise, close_balise, field);
simple_insert(open_balise, close_balise, field);
return;
return;
}
}
//Insertion dans le champs des codes de type select.
//Insertion dans le champs des codes de type select.
function insertbbcode_select(id_select, close_balise, field)
function insertbbcode_select(id_select, close_balise, field)
{
{
var select = document.getElementById(id_select + field);
var select = document.getElementById(id_select + field);
if( select.value != '' )
if( select.value != '' )
insertbbcode('[' + id_select + '=' + select.value + ']', close_balise, field);
insertbbcode('[' + id_select + '=' + select.value + ']', close_balise, field);
//On remet la valeur par défaut.
//On remet la valeur par défaut.
select.options[0].selected = true;
select.options[0].selected = true;
return;
return;
}
}
//Insertion dans le champs des codes de type select.
//Insertion dans le champs des codes de type select.
function insertbbcode_select2(id_select, field)
function insertbbcode_select2(id_select, field)
{
{
var select = document.getElementById(id_select + field);
var select = document.getElementById(id_select + field);
if( select.value != '' )
if( select.value != '' )
insertbbcode('[' + select.value + ']', '[/' + select.value + ']', field);
insertbbcode('[' + select.value + ']', '[/' + select.value + ']', field);
//On remet la valeur par défaut.
//On remet la valeur par défaut.
select.options[0].selected = true;
select.options[0].selected = true;
return;
return;
}
}
//Conserve la configuration de la barre bbcode.
//Conserve la configuration de la barre bbcode.
function set_bbcode_preference(divID)
function set_bbcode_preference(divID)
{
{
if( getCookie('hide-bbcode') == 0 )
if( getCookie('hide-bbcode') == 0 )
{
{
jQuery('#bbcode-expanded').removeClass('expand');
jQuery('#bbcode-expanded').removeClass('expand');
jQuery( "." + divID).each(function(){
jQuery( "." + divID).each(function(){
jQuery( "." + divID).hide();
jQuery( "." + divID).hide();
});
});
}
}
}
}
//Masquage du div.
//Masquage du div.
function show_bbcode_div(divID)
function show_bbcode_div(divID)
{
{
if( getCookie('hide-bbcode') == 0 )
if( getCookie('hide-bbcode') == 0 )
{
{
jQuery('#bbcode-expanded').addClass('expand');
jQuery('#bbcode-expanded').addClass('expand');
sendCookie('hide-bbcode', 1); //On envoi le cookie pour se souvenir du choix de l'utilisateur.
sendCookie('hide-bbcode', 1); //On envoi le cookie pour se souvenir du choix de l'utilisateur.
jQuery( "." + divID).each(function(){
jQuery( "." + divID).each(function(){
jQuery( "." + divID).fadeIn(300);
jQuery( "." + divID).fadeIn(300);
});
});
}
}
else
else
{
{
jQuery( "." + divID).each(function(){
jQuery( "." + divID).each(function(){
jQuery( "." + divID).fadeOut(300);
jQuery( "." + divID).fadeOut(300);
});
});
jQuery('#bbcode-expanded').removeClass('expand');
jQuery('#bbcode-expanded').removeClass('expand');
sendCookie('hide-bbcode', 0); //On envoi le cookie pour se souvenir du choix de l'utilisateur.
sendCookie('hide-bbcode', 0); //On envoi le cookie pour se souvenir du choix de l'utilisateur.
}
}
}
}
function bbcode_color(divID, field, type)
function bbcode_color(divID, field, type)
{
{
var i;
var i;
var br;
var br;
var contents;
var contents;
var color = new Array(
var color = new Array(
'#000000', '#433026', '#333300', '#003300', '#003366', '#000080', '#333399', '#333333',
'#000000', '#433026', '#333300', '#003300', '#003366', '#000080', '#333399', '#333333',
'#800000', '#FFA500', '#808000', '#008000', '#008080', '#0000FF', '#666699', '#808080',
'#800000', '#FFA500', '#808000', '#008000', '#008080', '#0000FF', '#666699', '#808080',
'#F04343', '#FF9900', '#99CC00', '#339966', '#33CCCC', '#3366FF', '#800080', '#ACA899',
'#F04343', '#FF9900', '#99CC00', '#339966', '#33CCCC', '#3366FF', '#800080', '#ACA899',
'#FFC0CB', '#FFCC00', '#FFFF00', '#00FF00', '#00FFFF', '#00CCFF', '#993366', '#C0C0C0',
'#FFC0CB', '#FFCC00', '#FFFF00', '#00FF00', '#00FFFF', '#00CCFF', '#993366', '#C0C0C0',
'#FF99CC', '#FFCC99', '#FFFF99', '#CCFFCC', '#CCFFFF', '#CC99FF', '#E3007B', '#FFFFFF');
'#FF99CC', '#FFCC99', '#FFFF99', '#CCFFCC', '#CCFFFF', '#CC99FF', '#E3007B', '#FFFFFF');
contents = '<table><tr>';
contents = '<table><tr>';
for(i = 0; i < 40; i++)
for(i = 0; i < 40; i++)
{
{
br = (i+1) % 8;
br = (i+1) % 8;
br = (br == 0 && i != 0 && i < 39) ? '</tr><tr>' : '';
br = (br == 0 && i != 0 && i < 39) ? '</tr><tr>' : '';
contents += '<td><a href="" style="background:' + color[i] + ';" onclick="insertbbcode(\'[' + type + '=' + color[i] + ']\', \'[/' + type + ']\', \'' + field + '\');bb_hide_block(\'' + divID + '\', \'' + field + '\', 0);return false;"></a></td>' + br;
contents += '<td><a href="" aria-label="' + color[i] + '" title="' + color[i] + '" style="background-color:' + color[i] + ';" onclick="insertbbcode(\'[' + type + '=' + color[i] + ']\', \'[/' + type + ']\', \'' + field + '\');bb_hide_block(\'' + divID + '\', \'' + field + '\', 0);return false;"></a></td>' + br;
}
}
document.getElementById("bb-"+ type + field).innerHTML = contents + '</tr></table>';
document.getElementById("bb-"+ type + field).innerHTML = contents + '</tr></table>';
}
}
function bbcode_table(field, head_name)
function bbcode_table(field, head_name)
{
{
var cols = document.getElementById('bb-cols' + field).value;
var cols = document.getElementById('bb-cols' + field).value;
var lines = document.getElementById('bb-lines' + field).value;
var lines = document.getElementById('bb-lines' + field).value;
var head = document.getElementById('bb-head' + field).checked;
var head = document.getElementById('bb-head' + field).checked;
var code = '';
var code = '';
if( cols >= 0 && lines >= 0 )
if( cols >= 0 && lines >= 0 )
{
{
var colspan = cols > 1 ? ' colspan="' + cols + '"' : '';
var colspan = cols > 1 ? ' colspan="' + cols + '"' : '';
var pointor = head ? (59 + colspan.length) : 22;
var pointor = head ? (59 + colspan.length) : 22;
code = head ? '[table]\n\t[row]\n\t\t[head' + colspan + ']'+ head_name +'[/head]\n\t[/row]\n' : '[table]\n';
code = head ? '[table]\n\t[row]\n\t\t[head' + colspan + ']'+ head_name +'[/head]\n\t[/row]\n' : '[table]\n';
for(var i = 0; i < lines; i++)
for(var i = 0; i < lines; i++)
{
{
code += '\t[row]\n';
code += '\t[row]\n';
for(var j = 0; j < cols; j++)
for(var j = 0; j < cols; j++)
code += '\t\t[col][/col]\n';
code += '\t\t[col][/col]\n';
code += '\t[/row]\n';
code += '\t[/row]\n';
}
}
code += '[/table]';
code += '[/table]';
insertbbcode(code.substring(0, pointor), code.substring(pointor, code.length), field);
insertbbcode(code.substring(0, pointor), code.substring(pointor, code.length), field);
}
}
}
}
function bbcode_list(field)
function bbcode_list(field)
{
{
var elements = document.getElementById('bb_list' + field).value;
var elements = document.getElementById('bb_list' + field).value;
var ordered_list = document.getElementById('bb_ordered_list' + field).checked;
var ordered_list = document.getElementById('bb_ordered_list' + field).checked;
if( elements <= 0 )
if( elements <= 0 )
elements = 1;
elements = 1;
var pointor = ordered_list ? 19 : 11;
var pointor = ordered_list ? 19 : 11;
code = '[list' + (ordered_list ? '=ordered' : '') + ']\n';
code = '[list' + (ordered_list ? '=ordered' : '') + ']\n';
for(var j = 0; j < elements; j++)
for(var j = 0; j < elements; j++)
code += '\t[*]\n';
code += '\t[*]\n';
code += '[/list]';
code += '[/list]';
insertbbcode(code.substring(0, pointor), code.substring(pointor, code.length), field);
insertbbcode(code.substring(0, pointor), code.substring(pointor, code.length), field);
}
}
function bbcode_url(field, prompt_text)
function bbcode_url(field, prompt_text)
{
{
var url = prompt(prompt_text, '');
var url = prompt(prompt_text, '');
if(url != '' && url != null)
if(url != '' && url != null)
insertbbcode('[url=' + url + ']', '[/url]', field);
insertbbcode('[url=' + url + ']', '[/url]', field);
}
}
function bbcode_quote(field, prompt_text)
{
var author = prompt(prompt_text, '');
if(author != null)
{
if(author != '')
insertbbcode('[quote=' + author + ']', '[/quote]', field);
else
insertbbcode('[quote]', '[/quote]', field);
}
}
function bbcode_lightbox(field, prompt_text)
function bbcode_lightbox(field, prompt_text)
{
{
var url = prompt(prompt_text, '');
var url = prompt(prompt_text, '');
if(url != '' && url != null)
if(url != '' && url != null)
insertbbcode('[lightbox=' + url + '][img style="max-width: 150px;"]' + url, '[/img][/lightbox]', field);
insertbbcode('[lightbox=' + url + '][img style="max-width: 150px;"]' + url, '[/img][/lightbox]', field);
}
}
function bbcode_anchor(field, prompt_text)
function bbcode_anchor(field, prompt_text)
{
{
var anchor = prompt(prompt_text, '');
var anchor = prompt(prompt_text, '');
if(anchor != '' && anchor != null)
if(anchor != '' && anchor != null)
insertbbcode('[anchor=' + url_encode_rewrite(anchor) + ']', '[/anchor]', field);
insertbbcode('[anchor=' + url_encode_rewrite(anchor) + ']', '[/anchor]', field);
else
else
insertbbcode('[anchor]', '[/anchor]', field);
insertbbcode('[anchor]', '[/anchor]', field);
}
}
function bbcode_abbr(field, prompt_text)
function bbcode_abbr(field, prompt_text)
{
{
var desc = prompt(prompt_text, '');
var desc = prompt(prompt_text, '');
if(desc != '' && desc != null)
if(desc != '' && desc != null)
insertbbcode('[abbr=' + desc + ']', '[/abbr]', field);
insertbbcode('[abbr=' + desc + ']', '[/abbr]', field);
else
else
insertbbcode('[abbr]', '[/abbr]', field);
insertbbcode('[abbr]', '[/abbr]', field);
}
}
function bbcode_acronym(field, prompt_text)
function bbcode_acronym(field, prompt_text)
{
{
var desc = prompt(prompt_text, '');
var desc = prompt(prompt_text, '');
if(desc != '' && desc != null)
if(desc != '' && desc != null)
insertbbcode('[acronym=' + desc + ']', '[/acronym]', field);
insertbbcode('[acronym=' + desc + ']', '[/acronym]', field);
else
else
insertbbcode('[acronym]', '[/acronym]', field);
insertbbcode('[acronym]', '[/acronym]', field);
}
}
function bbcode_fieldset(field, prompt_text)
function bbcode_fieldset(field, prompt_text)
{
{
var legend = prompt(prompt_text, '');
var legend = prompt(prompt_text, '');
if(legend != '' && legend != null)
if(legend != '' && legend != null)
insertbbcode('[fieldset legend="' + legend + '"]', '[/fieldset]', field);
insertbbcode('[fieldset legend="' + legend + '"]', '[/fieldset]', field);
else
else
insertbbcode('[fieldset]', '[/fieldset]', field);
insertbbcode('[fieldset]', '[/fieldset]', field);
}
}
function bbcode_mail(field, prompt_text)
function bbcode_mail(field, prompt_text)
{
{
var mail = prompt(prompt_text, '');
var mail = prompt(prompt_text, '');
if(mail != '' && mail != null)
if(mail != '' && mail != null)
insertbbcode('[mail=' + mail + ']', '[/mail]', field);
insertbbcode('[mail=' + mail + ']', '[/mail]', field);
}
}
function bbcode_feed(field, prompt_text)
function bbcode_feed(field, prompt_text)
{
{
var feed = prompt(prompt_text, '');
var feed = prompt(prompt_text, '');
if(feed != '' && feed != null)
if(feed != '' && feed != null)
insertbbcode('[feed cat="0" number="5"]' + feed.toLowerCase(), '[/feed]', field);
insertbbcode('[feed cat="0" number="5"]' + feed.toLowerCase(), '[/feed]', field);
}
}