2015-08-23 22:38:18 +02:00
/ * *
* Red people autocomplete
*
* require jQuery , jquery . textcomplete
* /
function contact _search ( term , callback , backend _url , type , extra _channels , spinelement ) {
if ( spinelement ) {
$ ( spinelement ) . spin ( 'tiny' ) ;
}
// Check if there is a cached result that contains the same information we would get with a full server-side search
var bt = backend _url + type ;
if ( ! ( bt in contact _search . cache ) ) contact _search . cache [ bt ] = { } ;
var lterm = term . toLowerCase ( ) ; // Ignore case
for ( var t in contact _search . cache [ bt ] ) {
if ( lterm . indexOf ( t ) >= 0 ) { // A more broad search has been performed already, so use those results
$ ( spinelement ) . spin ( false ) ;
// Filter old results locally
var matching = contact _search . cache [ bt ] [ t ] . filter ( function ( x ) { return ( x . name . toLowerCase ( ) . indexOf ( lterm ) >= 0 || ( typeof x . nick !== 'undefined' && x . nick . toLowerCase ( ) . indexOf ( lterm ) >= 0 ) ) ; } ) ; // Need to check that nick exists because groups don't have one
matching . unshift ( { taggable : false , text : term , replace : term } ) ;
setTimeout ( function ( ) { callback ( matching ) ; } , 1 ) ; // Use "pseudo-thread" to avoid some problems
return ;
}
}
var postdata = {
start : 0 ,
count : 100 ,
search : term ,
type : type ,
} ;
if ( typeof extra _channels !== 'undefined' && extra _channels )
postdata [ 'extra_channels[]' ] = extra _channels ;
$ . ajax ( {
type : 'POST' ,
url : backend _url ,
data : postdata ,
dataType : 'json' ,
success : function ( data ) {
// Cache results if we got them all (more information would not improve results)
// data.count represents the maximum number of items
if ( data . items . length - 1 < data . count ) {
contact _search . cache [ bt ] [ lterm ] = data . items ;
}
var items = data . items . slice ( 0 ) ;
items . unshift ( { taggable : false , text : term , replace : term } ) ;
callback ( items ) ;
$ ( spinelement ) . spin ( false ) ;
} ,
} ) . fail ( function ( ) { callback ( [ ] ) ; } ) ; // Callback must be invoked even if something went wrong.
}
contact _search . cache = { } ;
function contact _format ( item ) {
// Show contact information if not explicitly told to show something else
if ( typeof item . text === 'undefined' ) {
var desc = ( ( item . label ) ? item . nick + ' ' + item . label : item . nick ) ;
if ( typeof desc === 'undefined' ) desc = '' ;
if ( desc ) desc = ' (' + desc + ')' ;
2015-12-06 02:08:25 +01:00
return "<div class='{0}' title='{4}'><img class='dropdown-menu-img-sm' src='{1}'><span class='contactname'>{2}</span><span class='dropdown-sub-text'>{3}</span><div class='clear'></div></div>" . format ( item . taggable , item . photo , item . name , desc , item . link ) ;
2015-08-23 22:38:18 +02:00
}
else
return "<div>" + item . text + "</div>" ;
}
function editor _replace ( item ) {
if ( typeof item . replace !== 'undefined' ) {
return '$1$2' + item . replace ;
}
// $2 ensures that prefix (@,@!) is preserved
var id = item . id ;
// 16 chars of hash should be enough. Full hash could be used if it can be done in a visually appealing way.
// 16 chars is also the minimum length in the backend (otherwise it's interpreted as a local id).
if ( id . length > 16 )
id = item . id . substring ( 0 , 16 ) ;
return '$1$2' + item . nick . replace ( ' ' , '' ) + '+' + id + ' ' ;
}
function basic _replace ( item ) {
if ( typeof item . replace !== 'undefined' )
return '$1' + item . replace ;
return '$1' + item . name + ' ' ;
}
2015-10-24 13:04:14 +02:00
function trim _replace ( item ) {
if ( typeof item . replace !== 'undefined' )
return '$1' + item . replace ;
return '$1' + item . name ;
}
2015-08-23 22:38:18 +02:00
function submit _form ( e ) {
$ ( e ) . parents ( 'form' ) . submit ( ) ;
}
2016-04-17 16:29:18 +02:00
function getWord ( text , caretPos ) {
var index = text . indexOf ( caretPos ) ;
var postText = text . substring ( caretPos , caretPos + 8 ) ;
if ( ( postText . indexOf ( "[/list]" ) > 0 ) || postText . indexOf ( "[/ul]" ) > 0 || postText . indexOf ( "[/ol]" ) > 0 ) {
return postText ;
}
}
function getCaretPosition ( ctrl ) {
var CaretPos = 0 ; // IE Support
if ( document . selection ) {
ctrl . focus ( ) ;
var Sel = document . selection . createRange ( ) ;
Sel . moveStart ( 'character' , - ctrl . value . length ) ;
CaretPos = Sel . text . length ;
}
// Firefox support
else if ( ctrl . selectionStart || ctrl . selectionStart == '0' )
CaretPos = ctrl . selectionStart ;
return ( CaretPos ) ;
}
function setCaretPosition ( ctrl , pos ) {
if ( ctrl . setSelectionRange ) {
ctrl . focus ( ) ;
ctrl . setSelectionRange ( pos , pos ) ;
}
else if ( ctrl . createTextRange ) {
var range = ctrl . createTextRange ( ) ;
range . collapse ( true ) ;
range . moveEnd ( 'character' , pos ) ;
range . moveStart ( 'character' , pos ) ;
range . select ( ) ;
}
}
function listNewLineAutocomplete ( id ) {
var text = document . getElementById ( id ) ;
var caretPos = getCaretPosition ( text )
var word = getWord ( text . value , caretPos ) ;
if ( word != null ) {
var textBefore = text . value . substring ( 0 , caretPos ) ;
var textAfter = text . value . substring ( caretPos , text . length ) ;
$ ( '#' + id ) . val ( textBefore + '\r\n[*] ' + textAfter ) ;
setCaretPosition ( text , caretPos + 5 ) ;
return true ;
}
}
function string2bb ( element ) {
if ( element == 'bold' ) return 'b' ;
else if ( element == 'italic' ) return 'i' ;
else if ( element == 'underline' ) return 'u' ;
else if ( element == 'overline' ) return 'o' ;
else if ( element == 'strike' ) return 's' ;
else if ( element == 'superscript' ) return 'sup' ;
else if ( element == 'subscript' ) return 'sub' ;
else return element ;
}
2015-08-23 22:38:18 +02:00
/ * *
* jQuery plugin 'editor_autocomplete'
* /
( function ( $ ) {
$ . fn . editor _autocomplete = function ( backend _url , extra _channels ) {
if ( typeof extra _channels === 'undefined' ) extra _channels = false ;
// Autocomplete contacts
contacts = {
match : /(^|\s)(@\!*)([^ \n]+)$/ ,
index : 3 ,
search : function ( term , callback ) { contact _search ( term , callback , backend _url , 'c' , extra _channels , spinelement = false ) ; } ,
replace : editor _replace ,
template : contact _format ,
} ;
smilies = {
match : /(^|\s)(:[a-z]{2,})$/ ,
index : 2 ,
search : function ( term , callback ) { $ . getJSON ( '/smilies/json' ) . done ( function ( data ) { callback ( $ . map ( data , function ( entry ) { return entry . text . indexOf ( term ) === 0 ? entry : null ; } ) ) ; } ) ; } ,
template : function ( item ) { return item . icon + item . text ; } ,
replace : function ( item ) { return "$1" + item . text + ' ' ; } ,
} ;
this . attr ( 'autocomplete' , 'off' ) ;
this . textcomplete ( [ contacts , smilies ] , { className : 'acpopup' , zIndex : 1020 } ) ;
} ;
} ) ( jQuery ) ;
/ * *
* jQuery plugin 'search_autocomplete'
* /
( function ( $ ) {
$ . fn . search _autocomplete = function ( backend _url ) {
// Autocomplete contacts
contacts = {
match : /(^@)([^\n]{2,})$/ ,
index : 2 ,
search : function ( term , callback ) { contact _search ( term , callback , backend _url , 'x' , [ ] , spinelement = '#nav-search-spinner' ) ; } ,
replace : basic _replace ,
template : contact _format ,
} ;
this . attr ( 'autocomplete' , 'off' ) ;
var a = this . textcomplete ( [ contacts ] , { className : 'acpopup' , maxCount : 100 , zIndex : 1020 , appendTo : 'nav' } ) ;
a . on ( 'textComplete:select' , function ( e , value , strategy ) { submit _form ( this ) ; } ) ;
} ;
} ) ( jQuery ) ;
( function ( $ ) {
$ . fn . contact _autocomplete = function ( backend _url , typ , autosubmit , onselect ) {
if ( typeof typ === 'undefined' ) typ = '' ;
if ( typeof autosubmit === 'undefined' ) autosubmit = false ;
// Autocomplete contacts
contacts = {
match : /(^)([^\n]+)$/ ,
index : 2 ,
search : function ( term , callback ) { contact _search ( term , callback , backend _url , typ , [ ] , spinelement = false ) ; } ,
replace : basic _replace ,
template : contact _format ,
} ;
this . attr ( 'autocomplete' , 'off' ) ;
var a = this . textcomplete ( [ contacts ] , { className : 'acpopup' , zIndex : 1020 } ) ;
2015-10-24 13:04:14 +02:00
if ( autosubmit )
a . on ( 'textComplete:select' , function ( e , value , strategy ) { submit _form ( this ) ; } ) ;
if ( typeof onselect !== 'undefined' )
a . on ( 'textComplete:select' , function ( e , value , strategy ) { onselect ( value ) ; } ) ;
} ;
} ) ( jQuery ) ;
( function ( $ ) {
$ . fn . name _autocomplete = function ( backend _url , typ , autosubmit , onselect ) {
if ( typeof typ === 'undefined' ) typ = '' ;
if ( typeof autosubmit === 'undefined' ) autosubmit = false ;
// Autocomplete contacts
names = {
match : /(^)([^\n]+)$/ ,
index : 2 ,
search : function ( term , callback ) { contact _search ( term , callback , backend _url , typ , [ ] , spinelement = false ) ; } ,
replace : trim _replace ,
template : contact _format ,
} ;
this . attr ( 'autocomplete' , 'off' ) ;
var a = this . textcomplete ( [ names ] , { className : 'acpopup' , zIndex : 1020 } ) ;
2015-08-23 22:38:18 +02:00
if ( autosubmit )
a . on ( 'textComplete:select' , function ( e , value , strategy ) { submit _form ( this ) ; } ) ;
if ( typeof onselect !== 'undefined' )
a . on ( 'textComplete:select' , function ( e , value , strategy ) { onselect ( value ) ; } ) ;
} ;
2016-02-28 12:11:12 +01:00
} ) ( jQuery ) ;
2016-04-17 16:29:18 +02:00
( function ( $ ) {
$ . fn . bbco _autocomplete = function ( type ) {
if ( type == 'bbcode' ) {
var open _close _elements = [ 'bold' , 'italic' , 'underline' , 'overline' , 'strike' , 'superscript' , 'subscript' , 'quote' , 'code' , 'spoiler' , 'map' , 'nobb' , 'list' , 'ul' , 'ol' , 'li' , 'table' , 'tr' , 'th' , 'td' , 'center' , 'color' , 'font' , 'size' , 'zrl' , 'zmg' , 'rpost' , 'qr' , 'observer' ] ;
var open _elements = [ 'observer.baseurl' , 'observer.address' , 'observer.photo' , 'observer.name' , 'observer.webname' , 'observer.url' , '*' , 'hr' , ] ;
var elements = open _close _elements . concat ( open _elements ) ;
}
if ( type == 'comanche' ) {
var open _close _elements = [ 'region' , 'layout' , 'template' , 'theme' , 'widget' , 'block' , 'menu' , 'var' , 'css' , 'js' , 'authored' , 'comment' , 'webpage' ] ;
var open _elements = [ ] ;
var elements = open _close _elements . concat ( open _elements ) ;
}
if ( type == 'comanche-block' ) {
var open _close _elements = [ 'menu' , 'var' ] ;
var open _elements = [ ] ;
var elements = open _close _elements . concat ( open _elements ) ;
}
bbco = {
match : /\[(\w*\**)$/ ,
search : function ( term , callback ) {
callback ( $ . map ( elements , function ( element ) {
return element . indexOf ( term ) === 0 ? element : null ;
} ) ) ;
} ,
index : 1 ,
replace : function ( element ) {
element = string2bb ( element ) ;
if ( open _elements . indexOf ( element ) < 0 ) {
if ( element === 'list' || element === 'ol' || element === 'ul' ) {
return [ '\[' + element + '\]' + '\n\[*\] ' , '\n\[/' + element + '\]' ] ;
}
else if ( element === 'table' ) {
return [ '\[' + element + '\]' + '\n\[tr\]' , '\[/tr\]\n\[/' + element + '\]' ] ;
}
else {
return [ '\[' + element + '\]' , '\[/' + element + '\]' ] ;
}
}
else {
return '\[' + element + '\] ' ;
}
}
} ;
this . attr ( 'autocomplete' , 'off' ) ;
var a = this . textcomplete ( [ bbco ] , { className : 'acpopup' , zIndex : 1020 } ) ;
a . on ( 'textComplete:select' , function ( e , value , strategy ) { value ; } ) ;
a . keypress ( function ( e ) {
e . stopImmediatePropagation ( ) ;
if ( e . keyCode == 13 ) {
var x = listNewLineAutocomplete ( this . id ) ;
if ( x )
e . preventDefault ( ) ;
}
} ) ;
} ;
} ) ( jQuery ) ;