"use strict"; /* Form editing code (general, may be used on many different kinds of form) */ // =========== // HTML EDITOR // =========== if (typeof window.CKEDITOR=='undefined') { window.CKEDITOR=null; } function wysiwyg_cookie_says_on() { var cookie=read_cookie('use_wysiwyg'); return ((cookie=='') || (cookie!='0')) && (browser_matches('wysiwyg') && ('{$MOBILE}'!='1')); } function wysiwyg_on() { return wysiwyg_cookie_says_on(); } function toggle_wysiwyg(name) { if (!browser_matches('wysiwyg')) { window.fauxmodal_alert('{!comcode:TOGGLE_WYSIWYG_ERROR;^}'); return false; } if (typeof window.do_ajax_request=='undefined') return false; if (typeof window.merge_text_nodes=='undefined') return false; if (typeof window.get_elements_by_class_name=='undefined') return false; var is_wysiwyg_on=wysiwyg_on(); if (is_wysiwyg_on) { if (read_cookie('use_wysiwyg')=='-1') { _toggle_wysiwyg(name); } else { generate_question_ui( '{!comcode:WHETHER_SAVE_WYSIWYG_SELECTION;}', { buttons__cancel: '{!INPUTSYSTEM_CANCEL;^}', buttons__clear: '{!javascript:WYSIWYG_DISABLE_ONCE;^}', //buttons__no: '{!javascript:WYSIWYG_DISABLE_ONCE_AND_DONT_ASK;^}', Too confusing, re-enable if you want it buttons__yes: '{!javascript:WYSIWYG_DISABLE_ALWAYS;^}' }, '{!comcode:DISABLE_WYSIWYG;^}', '{!comcode:DISCARD_WYSIWYG_CHANGES;^}', function(saving_cookies) { if (!saving_cookies) return; if (saving_cookies.toLowerCase()=='{!javascript:WYSIWYG_DISABLE_ONCE;^}'.toLowerCase()) { _toggle_wysiwyg(name); } if (saving_cookies.toLowerCase()=='{!javascript:WYSIWYG_DISABLE_ONCE_AND_DONT_ASK;^}'.toLowerCase()) { _toggle_wysiwyg(name); set_cookie('use_wysiwyg','-1',3000); } if (saving_cookies.toLowerCase()=='{!javascript:WYSIWYG_DISABLE_ALWAYS;^}'.toLowerCase()) { _toggle_wysiwyg(name); set_cookie('use_wysiwyg','0',3000); } }, 600, 140 ); } return false; } var ret=_toggle_wysiwyg(name); if (read_cookie('use_wysiwyg')!='-1') set_cookie('use_wysiwyg','1',3000); return ret; } function _toggle_wysiwyg(name) { var is_wysiwyg_on=wysiwyg_on(); var forms=document.getElementsByTagName('form'); var so=document.getElementById('post_special_options'); var so2=document.getElementById('post_special_options2'); if (is_wysiwyg_on) { // Find if the WYSIWYG has anything in it - if not, discard var all_empty=true,myregexp=new RegExp(/((\s)|(
)|(<\/p>)|()|( )|(
]*>))*/);
for (var fid=0;fid
/g,'
');
event.data.html=event.data.html.replace(/
<\/span><\/font><\/div>$/,'
');
}
});
// Allow drag and drop uploading
editor.on('contentDom',function() {
editor.document.on('dragover',function(e) {
html5_upload_event_drag_over(e.data.$,element,element.id);
});
editor.document.on('drop',function(e) {
html5_upload_event_drop(e.data.$,element,element.id);
});
});
return editor;
}
function find_tags_in_editor(editor,element)
{
if (!editor.document) return;
if (typeof editor.document.$=='undefined') return;
if (!editor.document.$) return;
var comcodes=get_elements_by_class_name(editor.document.$.getElementsByTagName('body')[0],'cms_keep_ui_controlled');
for (var i=0;i
');
window.wysiwyg_editors[element.id].setData(html);
window.setTimeout(function() {
find_tags_in_editor(window.wysiwyg_editors[element.id],element);
}, 100);
} else
{
element.value=text;
}
}
/*
Insert some text, with WYSIWYG support...
element: non-WYSIWYG element
text: text to insert (non-HTML)
sel: Selection DOM object so we know what to *overwrite* with the inserted text (or NULL)
plain_insert: Set to true if we are doing a simple insert, not inserting complex Comcode that needs to have editing representation
html: HTML to insert (if not passed then 'text' will be escaped)
(Use insert_textbox_wrapping to wrap Comcode tags around a selection)
*/
function insert_textbox(element,text,sel,plain_insert,html)
{
if (typeof plain_insert=='undefined') plain_insert=false;
if (typeof html=='undefined') html=null;
if (is_wysiwyg_field(element))
{
var editor=window.wysiwyg_editors[element.id];
var insert='';
if (plain_insert)
{
insert=get_selected_html(editor)+(html?html:escape_html(text).replace(new RegExp('\\\\n','gi'),'
'));
} else
{
var url=maintain_theme_in_link('{$FIND_SCRIPT_NOHTTP;,comcode_convert}?semihtml=1'+keep_stub());
if (window.location.href.indexOf('topics')!=-1) url+='&forum_db=1';
var request=do_ajax_request(url,null,'data='+window.encodeURIComponent(text.replace(new RegExp(String.fromCharCode(8203),'g'),'')));
if ((request.responseXML) && (request.responseXML.documentElement.getElementsByTagName('result')[0]))
{
var result_tags=request.responseXML.documentElement.getElementsByTagName('result');
var result=result_tags[0];
insert=merge_text_nodes(result.childNodes).replace(/\s*$/,'');
}
}
var before=editor.getData();
try
{
editor.focus(); // Needed on some browsers
var selected_html=get_selected_html(editor);
if ((editor.getSelection()) && (editor.getSelection().getStartElement().getName()=='kbd')) // Danger Danger - don't want to insert into another Comcode tag. Put it after. They can cut+paste back if they need.
{
editor.document.getBody().appendHtml(insert);
} else
{
//editor.insertHtml(insert); Actually may break up the parent tag, we want it to nest nicely
var element_for_inserting=CKEDITOR.dom.element.createFromHtml(insert);
editor.insertElement(element_for_inserting);
}
var after=editor.getData();
if (after==before) throw 'Failed to insert';
find_tags_in_editor(editor,element);
}
catch (e) // Sometimes happens on Firefox in Windows, appending is a bit tamer (e.g. you cannot insert if you have the start of a h1 at cursor)
{
var after=editor.getData();
if (after==before) // Could have just been a window.scrollBy popup-blocker exception, so only do this if the op definitely failed
editor.document.getBody().appendHtml(insert);
}
return;
}
var from=element.value.length,to;
element.focus();
if (typeof sel=='undefined') sel=null;
if (sel===null) sel=document.selection?document.selection:null;
if (typeof element.selectionEnd!='undefined') // Mozilla style
{
from=element.selectionStart;
to=element.selectionEnd;
var start=element.value.substring(0,from);
var end=element.value.substring(to,element.value.length);
element.value=start+element.value.substring(from,to)+text+end;
set_selection_range(element,from+text.length,from+text.length);
} else
if (sel) // IE style
{
var ourRange=sel.createRange();
if ((ourRange.moveToElementText) || (ourRange.parentElement()==element))
{
if (ourRange.parentElement()!=element) ourRange.moveToElementText(element);
ourRange.text=ourRange.text+text;
} else
{
element.value+=text;
from+=2;
set_selection_range(element,from+text.length,from+text.length);
}
}
else
{
// :(
from+=2;
element.value+=text;
set_selection_range(element,from+text.length,from+text.length);
}
}
function insert_textbox_opener(element,text,sel,plain_insert,html)
{
if ((typeof sel=='undefined') || (!sel)) var sel=get_main_cms_window().document.selection?get_main_cms_window().document.selection:null;
get_main_cms_window().insert_textbox(element,text,sel,plain_insert,html);
}
// Get selected HTML from CKEditor
function get_selected_html(editor)
{
var my_selection=editor.getSelection();
if (!my_selection || my_selection.getType()==window.CKEDITOR.SELECTION_NONE) return '';
var selected_text='';
if (my_selection.getNative())
{
if ((window.CKEDITOR.env.ie) && (typeof my_selection.getNative().getRangeAt=='undefined')) // IE8 and under (selection object)
{
my_selection.unlock(true);
selected_text=my_selection.getNative().createRange().htmlText;
} else // IE9 / standards (HTMLSelection object)
{
try
{
selected_text=get_inner_html(my_selection.getNative().getRangeAt(0).cloneContents());
}
catch (e) {}
}
}
return selected_text;
}
// Insert into the editor such as to *wrap* the current selection with something new (typically a new Comcode tag)
function insert_textbox_wrapping(element,before_wrap_tag,after_wrap_tag)
{
var from,to;
if (after_wrap_tag=='')
{
after_wrap_tag='[/'+before_wrap_tag+']';
before_wrap_tag='['+before_wrap_tag+']';
}
if (is_wysiwyg_field(element))
{
var editor=window.wysiwyg_editors[element.id];
editor.focus(); // Needed on some browsers, but on Opera will defocus our selection
var selected_html=get_selected_html(editor);
if (selected_html=='') selected_html='{!comcode:TEXT_OR_COMCODE_GOES_HERE;}'.toUpperCase();
var new_html='';
var url=maintain_theme_in_link('{$FIND_SCRIPT_NOHTTP;,comcode_convert}?semihtml=1'+keep_stub());
if (window.location.href.indexOf('topics')!=-1) url+='&forum_db=1';
var request=do_ajax_request(url,null,'data='+window.encodeURIComponent((before_wrap_tag+selected_html+after_wrap_tag).replace(new RegExp(String.fromCharCode(8203),'g'),'')));
if ((request.responseXML) && (request.responseXML.documentElement.getElementsByTagName('result')[0]))
{
var result_tags=request.responseXML.documentElement.getElementsByTagName('result');
var result=result_tags[0];
new_html=merge_text_nodes(result.childNodes).replace(/\s*$/,''); /* result is an XML-escaped string of HTML, so we get via looking at the node text */
} else
{
new_html = selected_html;
}
if ((editor.getSelection()) && (editor.getSelection().getStartElement().getName()=='kbd')) // Danger Danger - don't want to insert into another Comcode tag. Put it after. They can cut+paste back if they need.
{
editor.document.getBody().appendHtml(new_html);
} else
{
editor.insertHtml(new_html);
}
find_tags_in_editor(editor,element);
return;
}
if (typeof element.selectionEnd!='undefined') // Mozilla style
{
from=element.selectionStart;
to=element.selectionEnd;
var start=element.value.substring(0,from);
var end=element.value.substring(to,element.value.length);
if (to>from)
{
element.value=start+before_wrap_tag+element.value.substring(from,to)+after_wrap_tag+end;
} else
{
element.value=start+before_wrap_tag+after_wrap_tag+end;
}
set_selection_range(element,from,to+before_wrap_tag.length+after_wrap_tag.length);
} else
if (typeof document.selection!='undefined') // IE style
{
element.focus();
var sel=document.selection;
var ourRange=sel.createRange();
if ((ourRange.moveToElementText) || (ourRange.parentElement()==element))
{
if (ourRange.parentElement()!=element) ourRange.moveToElementText(element);
ourRange.text=before_wrap_tag+ourRange.text+after_wrap_tag;
} else element.value+=before_wrap_tag+after_wrap_tag;
}
else
{
// :(
element.value+=before_wrap_tag+after_wrap_tag;
set_selection_range(element,from,to+before_wrap_tag.length+after_wrap_tag.length);
}
}
// From http://www.faqts.com/knowledge_base/view.phtml/aid/13562
function set_selection_range(input,selection_start,selection_end)
{
if (typeof input.set_selection_range!='undefined') /* Mozilla style */
{
input.focus();
input.set_selection_range(selection_start,selection_end);
}
else if (typeof input.createTextRange!='undefined') /* IE style */
{
var range=input.createTextRange();
range.collapse(true);
range.moveEnd('character',selection_end);
range.moveStart('character',selection_start);
range.select();
} else input.focus();
}
function show_upload_syndication_options(name,syndication_json,no_quota)
{
if (typeof no_quota=='undefined') no_quota=false;
var html_spot=document.getElementById(name+'_syndication_options');
var html='';
var num_checked=0;
var file_ob=document.getElementById(name);
var pre_disabled=file_ob.disabled;
var syndication=JSON.parse(syndication_json),id,authorised,label,checked;
var num=0;
for (var hook in syndication)
{
num++;
}
for (var hook in syndication)
{
id='upload_syndicate__'+hook+'__'+name;
authorised=syndication[hook].authorised;
label=syndication[hook].label;
if (authorised)
{
checked=true;
num_checked++;
} else
{
checked=false;
}
window.setTimeout(function(id,authorised) {
return function() {
document.getElementById(id).onclick=function() {
var e=document.getElementById(id);
if (e.checked)
{
if (!authorised)
{
//e.checked=false; Better to assume success, not all oAuth support callback
var url='{$FIND_SCRIPT;,upload_syndication_auth}?hook='+window.encodeURIComponent(hook)+'&name='+window.encodeURIComponent(name)+keep_stub();
/*{+START,IF,{$MOBILE}}*/
window.open(url);
/*{+END}*/
/*{+START,IF,{$NOT,{$MOBILE}}}*/
faux_open(url,null,'width=960;height=500','_top');
/*{+END}*/
if (!pre_disabled)
{
file_ob.disabled=false;
}
}
}
};
};
}(id,authorised),0);
html+='';
}
if ((no_quota) && (num_checked==0))
{
file_ob.disabled=true;
}
if ((html!='') && (!no_quota))
{
html+='';
}
html='