1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-06-17 07:10:48 +03:00
ifhub.club/framework/frontend/framework/js/core/main.js

621 lines
15 KiB
JavaScript
Raw Normal View History

2011-08-09 05:43:22 +03:00
Function.prototype.bind = function(context) {
var fn = this;
if(jQuery.type(fn) != 'function'){
throw new TypeError('Function.prototype.bind: call on non-function');
};
if(jQuery.type(context) == 'null'){
throw new TypeError('Function.prototype.bind: cant be bound to null');
};
2011-08-09 05:43:22 +03:00
return function() {
return fn.apply(context, arguments);
};
};
String.prototype.tr = function(a,p) {
var k;
var p = typeof(p)=='string' ? p : '';
var s = this;
2012-02-20 22:32:23 +02:00
jQuery.each(a,function(k){
var tk = p?p.split('/'):[];
tk[tk.length] = k;
var tp = tk.join('/');
if(typeof(a[k])=='object'){
s = s.tr(a[k],tp);
}else{
s = s.replace((new RegExp('%%'+tp+'%%', 'g')), a[k]);
};
});
return s;
};
2011-08-09 05:43:22 +03:00
var ls = ls || {};
/**
* Управление всплывающими сообщениями
*/
2011-08-09 05:43:22 +03:00
ls.msg = (function ($) {
/**
* Опции
*/
this.options = {
class_notice: 'n-notice',
class_error: 'n-error'
};
/**
* Отображение информационного сообщения
*/
this.notice = function(title,msg){
$.notifier.broadcast(title, msg, this.options.class_notice);
};
2011-08-09 05:43:22 +03:00
/**
* Отображение сообщения об ошибке
2011-08-09 05:43:22 +03:00
*/
this.error = function(title,msg){
$.notifier.broadcast(title, msg, this.options.class_error);
};
2011-08-09 05:43:22 +03:00
return this;
}).call(ls.msg || {},jQuery);
/**
* Доступ к языковым текстовкам (предварительно должны быть прогружены в шаблон)
*/
ls.lang = (function ($) {
/**
* Набор текстовок
*/
this.msgs = {};
/**
* Загрузка текстовок
*/
this.load = function(msgs){
$.extend(true,this.msgs,msgs);
};
2011-08-09 05:43:22 +03:00
/**
* Отображение сообщения об ошибке
2011-08-09 05:43:22 +03:00
*/
this.get = function(name,replace){
if (this.msgs[name]) {
var value=this.msgs[name];
if (replace) {
value = value.tr(replace);
2011-08-09 05:43:22 +03:00
}
return value;
}
return '';
};
2011-08-09 05:43:22 +03:00
return this;
}).call(ls.lang || {},jQuery);
/**
* Методы таймера например, запуск функии через интервал
*/
ls.timer = (function ($) {
this.aTimers={};
/**
* Запуск метода через определенный период, поддерживает пролонгацию
*/
this.run = function(fMethod,sUniqKey,aParams,iTime){
iTime=iTime || 1500;
aParams=aParams || [];
sUniqKey=sUniqKey || Math.random();
if (this.aTimers[sUniqKey]) {
clearTimeout(this.aTimers[sUniqKey]);
this.aTimers[sUniqKey]=null;
}
var timeout = setTimeout(function(){
clearTimeout(this.aTimers[sUniqKey]);
this.aTimers[sUniqKey]=null;
fMethod.apply(this,aParams);
}.bind(this),iTime);
this.aTimers[sUniqKey]=timeout;
};
return this;
}).call(ls.timer || {},jQuery);
/**
* Функционал хранения js данных
*/
ls.registry = (function ($) {
this.aData={};
/**
* Сохранение
*/
this.set = function(sName,data){
this.aData[sName]=data;
};
/**
* Получение
*/
this.get = function(sName){
return this.aData[sName];
};
return this;
}).call(ls.registry || {},jQuery);
2011-08-09 05:43:22 +03:00
/**
* Flash загрузчик
*/
ls.swfupload = (function ($) {
2011-08-09 05:43:22 +03:00
this.swfu = null;
2012-01-28 17:52:44 +02:00
this.swfOptions = {};
2011-08-09 05:43:22 +03:00
this.initOptions = function() {
2011-08-09 05:43:22 +03:00
this.swfOptions = {
// Backend Settings
upload_url: aRouter['photoset']+"upload",
post_params: {'SSID':SESSION_ID, 'security_ls_key': LIVESTREET_SECURITY_KEY},
// File Upload Settings
file_types : "*.jpg;*.jpe;*.jpeg;*.png;*.gif;*.JPG;*.JPE;*.JPEG;*.PNG;*.GIF",
2011-08-09 05:43:22 +03:00
file_types_description : "Images",
file_upload_limit : "0",
// Event Handler Settings
file_queue_error_handler : this.handlerFileQueueError,
file_dialog_complete_handler : this.handlerFileDialogComplete,
upload_progress_handler : this.handlerUploadProgress,
upload_error_handler : this.handlerUploadError,
upload_success_handler : this.handlerUploadSuccess,
upload_complete_handler : this.handlerUploadComplete,
// Button Settings
button_placeholder_id : "start-upload",
button_width: 122,
button_height: 30,
button_text : '<span class="button">'+ls.lang.get('topic_photoset_upload_choose')+'</span>',
button_text_style : '.button { color: #1F8AB7; font-size: 14px; }',
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_text_left_padding: 6,
button_text_top_padding: 3,
button_cursor: SWFUpload.CURSOR.HAND,
// Flash Settings
2013-08-08 14:00:37 +03:00
flash_url : PATH_FRAMEWORK_FRONTEND+'/js/vendor/swfupload/swfupload.swf',
2011-08-09 05:43:22 +03:00
custom_settings : {
2011-08-09 05:43:22 +03:00
},
// Debug Settings
debug: false
};
2012-02-26 18:30:41 +02:00
ls.hook.run('ls_swfupload_init_options_after',arguments,this.swfOptions);
2012-02-26 18:30:41 +02:00
};
2011-08-10 15:39:17 +03:00
this.loadSwf = function() {
2012-01-18 08:41:51 +02:00
var f = {};
2012-01-18 08:41:51 +02:00
f.onSwfobject = function(){
if(window.swfobject && swfobject.swfupload){
f.onSwfobjectSwfupload();
}else{
ls.debug('window.swfobject && swfobject.swfupload is undefined, load swfobject/plugin/swfupload.js');
$.getScript(
2013-08-08 14:00:37 +03:00
PATH_FRAMEWORK_FRONTEND+'/js/vendor/swfobject/plugin/swfupload.js',
2012-01-18 08:41:51 +02:00
f.onSwfobjectSwfupload
);
}
}.bind(this);
2012-01-18 08:41:51 +02:00
f.onSwfobjectSwfupload = function(){
if(window.SWFUpload){
f.onSwfupload();
}else{
ls.debug('window.SWFUpload is undefined, load swfupload/swfupload.js');
$.getScript(
2013-08-08 14:00:37 +03:00
PATH_FRAMEWORK_FRONTEND+'/js/vendor/swfupload/swfupload.js',
2012-01-18 08:41:51 +02:00
f.onSwfupload
);
}
}.bind(this);
2012-01-18 08:41:51 +02:00
f.onSwfupload = function(){
2011-08-10 15:39:17 +03:00
this.initOptions();
$(this).trigger('load');
2012-01-18 08:41:51 +02:00
}.bind(this);
2012-01-18 08:41:51 +02:00
(function(){
if(window.swfobject){
f.onSwfobject();
}else{
ls.debug('window.swfobject is undefined, load swfobject/swfobject.js');
$.getScript(
2013-08-08 14:00:37 +03:00
PATH_FRAMEWORK_FRONTEND+'/js/vendor/swfobject/swfobject.js',
2012-01-18 08:41:51 +02:00
f.onSwfobject
);
}
}.bind(this))();
};
2011-08-09 05:43:22 +03:00
this.init = function(opt) {
if (opt) {
$.extend(true,this.swfOptions,opt);
}
this.swfu = new SWFUpload(this.swfOptions);
return this.swfu;
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerFileQueueError = function(file, errorCode, message) {
$(this).trigger('eFileQueueError',[file, errorCode, message]);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerFileDialogComplete = function(numFilesSelected, numFilesQueued) {
$(this).trigger('eFileDialogComplete',[numFilesSelected, numFilesQueued]);
if (numFilesQueued>0) {
this.startUpload();
}
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerUploadProgress = function(file, bytesLoaded) {
var percent = Math.ceil((bytesLoaded / file.size) * 100);
$(this).trigger('eUploadProgress',[file, bytesLoaded, percent]);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerUploadError = function(file, errorCode, message) {
$(this).trigger('eUploadError',[file, errorCode, message]);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerUploadSuccess = function(file, serverData) {
$(this).trigger('eUploadSuccess',[file, serverData]);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.handlerUploadComplete = function(file) {
var next = this.getStats().files_queued;
if (next > 0) {
this.startUpload();
}
$(this).trigger('eUploadComplete',[file, next]);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
return this;
}).call(ls.swfupload || {},jQuery);
/**
* Вспомогательные функции
*/
ls.tools = (function ($) {
/**
* Переводит первый символ в верхний регистр
*/
this.ucfirst = function(str) {
var f = str.charAt(0).toUpperCase();
return f + str.substr(1, str.length-1);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
/**
* Выделяет все chekbox с определенным css классом
*/
this.checkAll = function(cssclass, checkbox, invert) {
$('.'+cssclass).each(function(index, item){
if (invert) {
$(item).attr('checked', !$(item).attr("checked"));
} else {
$(item).attr('checked', $(checkbox).attr("checked"));
}
});
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
/**
* Предпросмотр
*/
this.textPreview = function(textId, save, divPreview) {
var text = WYSIWYG ? tinyMCE.activeEditor.getContent() : $('#' + textId).val();
var ajaxUrl = aRouter['ajax']+'preview/text/';
var ajaxOptions = {text: text, save: save};
2012-04-28 09:09:47 +03:00
ls.hook.marker('textPreviewAjaxBefore');
ls.ajax(ajaxUrl, ajaxOptions, function(result){
2011-08-09 05:43:22 +03:00
if (!result) {
ls.msg.error('Error','Please try again later');
}
if (result.bStateError) {
ls.msg.error(result.sMsgTitle||'Error',result.sMsg||'Please try again later');
2011-08-09 05:43:22 +03:00
} else {
if (!divPreview) {
divPreview = 'text_preview';
}
2012-01-28 17:52:44 +02:00
var elementPreview = $('#'+divPreview);
2012-04-28 09:09:47 +03:00
ls.hook.marker('textPreviewDisplayBefore');
2012-01-28 17:52:44 +02:00
if (elementPreview.length) {
elementPreview.html(result.sText);
2012-04-28 09:09:47 +03:00
ls.hook.marker('textPreviewDisplayAfter');
2011-08-09 05:43:22 +03:00
}
}
});
2012-02-26 18:30:41 +02:00
};
/**
* Возвращает выделенный текст на странице
*/
this.getSelectedText = function(){
2011-09-02 11:22:03 +03:00
var text = '';
if(window.getSelection){
text = window.getSelection().toString();
} else if(window.document.selection){
var sel = window.document.selection.createRange();
text = sel.text || sel;
if(text.toString) {
text = text.toString();
} else {
text = '';
}
}
return text;
2012-02-26 18:30:41 +02:00
};
/**
* Получает значение атрибута data
*/
this.getOption = function (element, data, defaultValue) {
var option = element.data(data);
switch (option) {
case 'true':
return true;
case 'false':
return false;
case undefined:
return defaultValue
default:
return option;
}
};
2013-04-03 00:08:55 +03:00
this.getDataOptions = function (element, prefix) {
var prefix = prefix || 'option',
resultOptions = {},
dataOptions = typeof element === 'string' ? $(element).data() : element.data();
for (option in dataOptions) {
// Remove 'option' prefix
if (option.substring(0, prefix.length) == prefix) {
var str = option.substring(prefix.length);
resultOptions[str.charAt(0).toLowerCase() + str.substring(1)] = dataOptions[option];
2013-04-03 00:08:55 +03:00
}
}
return resultOptions;
};
2012-01-28 17:52:44 +02:00
2011-08-09 05:43:22 +03:00
return this;
}).call(ls.tools || {},jQuery);
/**
* Дополнительные функции
*/
ls = (function ($) {
2011-08-09 05:43:22 +03:00
/**
* Глобальные опции
*/
2012-02-26 18:30:41 +02:00
this.options = this.options || {};
2011-08-09 05:43:22 +03:00
/**
* Выполнение AJAX запроса, автоматически передает security key
*/
2013-04-30 13:03:45 +03:00
this.ajax = function(url, params, callback, more) {
more = more || {};
params = params || {};
params.security_ls_key = LIVESTREET_SECURITY_KEY;
2013-04-30 13:03:45 +03:00
$.each(params, function(k, v){
2011-08-09 05:43:22 +03:00
if (typeof(v) == "boolean") {
2013-04-30 13:03:45 +03:00
params[k] = v ? 1 : 0;
2011-08-09 05:43:22 +03:00
}
2012-02-26 18:30:41 +02:00
});
2013-04-30 13:03:45 +03:00
if (url.indexOf('http://') != 0 && url.indexOf('https://') != 0 && url.indexOf('/') != 0) {
url=aRouter['ajax'] + url + '/';
2011-08-09 05:43:22 +03:00
}
2012-01-28 17:52:44 +02:00
2013-04-30 13:03:45 +03:00
var ajaxOptions = $.extend({}, {
type: "POST",
2011-08-09 05:43:22 +03:00
url: url,
data: params,
2013-04-30 13:03:45 +03:00
dataType: 'json',
2012-02-29 09:13:43 +02:00
success: callback || function(){
ls.debug("ajax success: ");
ls.debug.apply(this,arguments);
2011-08-09 05:43:22 +03:00
}.bind(this),
2013-04-30 13:03:45 +03:00
error: function(msg){
2012-02-29 09:13:43 +02:00
ls.debug("ajax error: ");
2013-04-30 13:03:45 +03:00
ls.debug.apply(this, arguments);
2011-08-09 05:43:22 +03:00
}.bind(this),
2013-04-30 13:03:45 +03:00
complete: function(msg){
2012-02-29 09:13:43 +02:00
ls.debug("ajax complete: ");
2013-04-30 13:03:45 +03:00
ls.debug.apply(this, arguments);
2011-08-09 05:43:22 +03:00
}.bind(this)
2013-04-30 13:03:45 +03:00
}, more);
2013-04-30 13:03:45 +03:00
ls.hook.run('ls_ajax_before', [ajaxOptions, callback, more], this);
2012-01-28 17:52:44 +02:00
return $.ajax(ajaxOptions);
2011-08-09 05:43:22 +03:00
};
2011-08-09 05:43:22 +03:00
/**
* Выполнение AJAX отправки формы, включая загрузку файлов
*/
this.ajaxSubmit = function(url, form, callback, more) {
var more = more || {}
form = typeof form == 'string' ? $(form) : form;
if (url.indexOf('http://') != 0 && url.indexOf('https://') != 0 && url.indexOf('/') != 0) {
url = aRouter['ajax'] + url + '/';
2011-08-09 05:43:22 +03:00
}
var options = {
2011-08-09 05:43:22 +03:00
type: 'POST',
url: url,
dataType: more.dataType || 'json',
data: {
security_ls_key: LIVESTREET_SECURITY_KEY
},
beforeSubmit: function (arr, form, options) {
form.find('[type=submit]').prop('disabled', true).addClass('loading');
},
beforeSerialize: function (form, options) {
return form.parsley('validate');
},
success: typeof callback == 'function' ? function (result, status, xhr, form) {
if (result.bStateError) {
form.find('[type=submit]').prop('disabled', false).removeClass('loading');
ls.msg.error(null, result.sMsg);
more.warning(result, status, xhr, form);
} else {
if (result.sMsg) {
form.find('[type=submit]').prop('disabled', false).removeClass('loading');
ls.msg.notice(null, result.sMsg);
}
callback(result, status, xhr, form);
}
} : function () {
2012-02-29 09:13:43 +02:00
ls.debug("ajax success: ");
ls.debug.apply(this, arguments);
2011-08-09 05:43:22 +03:00
}.bind(this),
2012-02-29 09:13:43 +02:00
error: more.error || function(){
ls.debug("ajax error: ");
ls.debug.apply(this, arguments);
2011-08-09 05:43:22 +03:00
}.bind(this)
2012-02-26 18:30:41 +02:00
};
2012-01-28 17:52:44 +02:00
2013-01-21 08:12:29 +02:00
ls.hook.run('ls_ajaxsubmit_before', [options,form,callback,more], this);
2011-08-09 05:43:22 +03:00
form.ajaxSubmit(options);
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
/**
* Создание ajax формы
*
* @param {string} url Ссылка
* @param {jquery, string} form Селектор формы либо объект jquery
* @param {Function} callback Success коллбэк
* @param {[type]} more Дополнительные параметры
*/
this.ajaxForm = function(url, form, callback, more) {
var form = typeof form == 'string' ? $(form) : form;
form.on('submit', function (e) {
ls.ajaxSubmit(url, form, callback, more);
e.preventDefault();
});
};
2011-08-09 05:43:22 +03:00
/**
* Дебаг сообщений
*/
this.debug = function() {
2011-08-09 05:43:22 +03:00
if (this.options.debug) {
this.log.apply(this,arguments);
2011-08-09 05:43:22 +03:00
}
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
/**
* Лог сообщений
*/
this.log = function() {
if (!$.browser.msie && window.console && window.console.log) {
Function.prototype.bind.call(console.log, console).apply(console, arguments);
2011-08-09 05:43:22 +03:00
} else {
//alert(msg);
}
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
return this;
}).call(ls || {},jQuery);
/**
* Автокомплитер
*/
ls.autocomplete = (function ($) {
/**
* Добавляет автокомплитер к полю ввода
*/
this.add = function(obj, sPath, multiple) {
if (multiple) {
obj.bind("keydown", function(event) {
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function(request, response) {
ls.ajax(sPath,{value: ls.autocomplete.extractLast(request.term)},function(data){
response(data.aItems);
});
},
search: function() {
var term = ls.autocomplete.extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
return false;
},
select: function(event, ui) {
var terms = ls.autocomplete.split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
});
} else {
obj.autocomplete({
source: function(request, response) {
ls.ajax(sPath,{value: ls.autocomplete.extractLast(request.term)},function(data){
response(data.aItems);
});
}
});
}
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.split = function(val) {
return val.split( /,\s*/ );
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
this.extractLast = function(term) {
return ls.autocomplete.split(term).pop();
2012-02-26 18:30:41 +02:00
};
2011-08-09 05:43:22 +03:00
return this;
}).call(ls.autocomplete || {},jQuery);
2012-06-05 19:42:51 +03:00
/**
2013-04-03 00:08:55 +03:00
* Костыли для ИЕ
*/
2012-06-05 19:42:51 +03:00
ls.ie = (function ($) {
return this;
2012-06-05 21:03:39 +03:00
}).call(ls.ie || {},jQuery);
2012-06-05 19:42:51 +03:00
2012-01-28 17:52:44 +02:00
(ls.options || {}).debug=1;