<?php


/**
 *
 *
 * @category  vendors
 * @package   rtac
 *
 * @author    Yusein Yuseinov <yyuseinov@gmail.com>
 * @copyright 2006 - 2014 Experta OOD
 * @license   GPL 3
 *
 * @since     v 0.1
 */
class rtac_yuku_Textcomplete extends core_Manager
{
    public $interfaces = 'rtac_AutocompleteIntf';
    
    
    public $title = 'Yuku textcomplete';
    
    
    /**
     * Добавя необходимите неща за да работи плъгина
     *
     * @param core_Et $tpl
     *
     * @see rtac_AutocompleteIntf::loadPacks(&$tpl)
     */
    public static function loadPacks(&$tpl)
    {
        $conf = core_Packs::getConfig('rtac');
        $tpl->push('rtac/yuku/' . $conf->RTAC_YUKU_VERSION . '/jquery.textcomplete.js', 'JS');
        
        $tpl->push('rtac/yuku/autocomplete.css', 'CSS');
    }
    
    
    /**
     * Стартира autocomplete-а за добавяне на потребители
     *
     * @param core_Et $tpl
     * @param string  $rtId
     * @param integer  $maxShowCnt
     *
     * @see rtac_AutocompleteIntf::runAutocompleteUsers(&$tpl, $rtId, $maxShowCnt)
     */
    public static function runAutocompleteUsers(&$tpl, $rtId, $maxCount)
    {
        jquery_Jquery::run($tpl, "
        	$('#{$rtId}').textcomplete(
                {
                    match: /\B@((\w|\.|[А-Яа-я])*)$/i,
                    index: 1,
                    search: function (term, callback) {
                        getEfae().process({url: rtacObj.shareUsersURL.{$rtId}}, {term:term, roles:rtacObj.shareUserRoles.{$rtId}, users:rtacObj.shareUsersIds.{$rtId}, rtid: '{$rtId}'}, false);
                        callback(rtacObj.sharedUsers.{$rtId});
                    },
                    replace: function (userObj) {
                        
                        return '@' + userObj.nick + ' ';
                    },
                    maxCount: {$maxCount},
                    cache: true,
                    template: function(userObj) {
                    	return userObj.nick + ' ' + '<span class=\'autocomplete-name\'>' + userObj.names + '</span>';
    				}
                }
            );
        ", true);
    }
    
    
    /**
     * Стартира autocomplete-а за добавяне на текст
     *
     * @param core_Et $tpl
     * @param string  $textId
     * @param integer  $maxCount
     *
     * @see rtac_AutocompleteIntf::runAutocompleteUsers(&$tpl, $rtId, $maxShowCnt)
     */
    public static function runAutocompleteText(&$tpl, $textId, $maxCount)
    {
        jquery_Jquery::run($tpl, "
        	$('#{$textId}').textcomplete(
                {
                    match: /([^\s]*)$/,
                    index: 1,
                    search: function (term, callback) {
                    	
                        callback($.map(rtacObj.textCompleteObj.{$textId}, function (element) {
                        	if (typeof term == 'undefined') return ;
                        	if ((term == ' ') || (term == '')) return ;
                        	term = term.toLowerCase();
                        	
                            if (typeof element == 'string') {
                                var text = element.toLowerCase();

                                return text.indexOf(term) === 0 ? element : null;
                            } else {
                                var text = element.search.toLowerCase();
                                
                                // Ако започват с буквата/думата или преди интервала има такава буква/дума
                                if ((text.indexOf(term) === 0) || (text.indexOf(' ' + term) != -1)) {
                                    return element;
                                } else {
                                    return null;
                                }
                            }
                    	}));
                    },
                    replace: function (textComplete) {
                        if (typeof textComplete == 'string') {
                            var text = textComplete;
                        } else {
                            var text = textComplete.val;
                        }
                        
                        return text + rtacObj.textCompleteStrEnd.{$textId};
                    },
                    maxCount: {$maxCount},
                    cache: true,
                    template: function(textComplete) {
                        if (typeof textComplete == 'string') {
                            var text = textComplete;
                        } else {
                            var text = textComplete.template;
                        }
                        
                    	return text;
    				}
                }
            );
        ", true);
    }
}
