function H(isOutBox){ this.document = document; this.isOutBox = isOutBox; this.iframe = this.isOutBox ? $('#mainbox') : $('body'); // DOM-элемент, который перехватывает клик по логину //this.grabLogin = null; // DOM-элемент, который перехватывает клик по названию шмотки //this.grabItem = null; this.grabLogin = new Grabber({inputClass: 'grabLogin'}); this.grabItem = new Grabber({inputClass: 'grabLogin'}); // storage this.data = {}; } H.prototype.toString = function(){ return 'This is H-object'; } H.prototype.getMainBox = function(){ return this.isOutBox ? this.iframe.contents().find('body') : $('body'); } H.prototype.loadMainBox = function(location){ location = location || '/main.php'; this.iframe.attr('src',location); } H.prototype.loadDocument = function(location){ this.document.location = location; } // ======== storage H.prototype.set = function(key, value){ this.data[key] = value; } H.prototype.get = function(key, defaultValue){ return undefined == this.data[key] ? defaultValue : this.data[key]; } H.prototype.setHP = function(id, curHP, maxHP){ curHP = curHP || 0; maxHP = maxHP || 0; const hp = this.getMainBox().find('#hpKey_' + id); if(hp.length < 1){ return false; } const hpA = $('img:eq(1)', hp); const hpB = $('img:eq(2)', hp); const redHP = 0.33; // меньше 30% красный цвет const yellowHP = 0.66; // меньше 60% желтый цвет, иначе зеленый if(curHP > maxHP){ curHP = maxHP; } const text = curHP + '/' + maxHP; const lengthHP = 170 - (text.length - 1) * 8; const sizeFirst = Math.round((lengthHP / maxHP) * curHP); const sizeSecond = lengthHP - sizeFirst; hpA.attr('width', sizeFirst); hpB.attr('width', sizeSecond); if(curHP / maxHP < redHP){ hpA.attr('src', '/i/1red.gif'); }else{ if(curHP / maxHP < yellowHP){ hpA.attr('src', '/i/1yellow.gif'); }else{ hpA.attr('src', '/i/1green.gif'); } } hp.html(hp.html().substring(0, hp.html().lastIndexOf(':') + 1) + Math.round(curHP) + "/" + maxHP); } /* ------------------ перехват клика по логину юзера -------------------------------------------- * / H.prototype.setGrabLogin = function(input){ var obj = this; this.clearGrabLogin(); var tmp = $(input); if(tmp.length > 0){ this.grabLogin = tmp.get(0); $(this.grabLogin) .addClass('grabLogin') .dblclick(function(){obj.toggleGrabLogin(this)}) .select(); return true; } return false; } H.prototype.clearGrabLogin = function(){ $(this.grabLogin).removeClass('grabLogin'); this.grabLogin = null; }, H.prototype.toggleGrabLogin = function(input){ if($(input).hasClass('grabLogin')){ this.clearGrabLogin(); }else{ this.setGrabLogin($(input)); } } /* -------------------------------- простые диалоги --------------------------------------------- */ H.prototype.sdOptionsDefault = { formMethod: 'POST', formAction: '', formClass: '', title: 'заголовок не указан', width: 250, data: {}, content: 'контент не указан', onSubmit: function(){return true;}, onClose: function(){this.remove()} } H.prototype._sd = function(options){ options = $.extend({}, this.sdOptionsDefault, options); const m = $('
').css('width', options.width); const t = $('
')
.click(function () {
return options.onClose.call($(this).closest('div.sd-container'))
});
const f = $('')
.attr('method', options.formMethod)
.attr('action', options.formAction)
.submit(function () {
return options.onSubmit.call($(this).closest('div.sd-container'))
});
for(let i in options.data){
$('').attr('name',i).val(options.data[i]).appendTo(f);
}
if(options.formClass){
f.addClass(options.formClass);
}
$('div.sd-container', this.getMainBox()).remove();
return m.append(t.prepend(c)).append(f.append(options.content));
}
H.prototype.sd = function(options){
const tmp = this._sd(options);
return this.getMainBox().append(tmp);
}
H.prototype.sdOneInput = function(options){
let onSubmit2 = options.onSubmit;
options = $.extend({},this.sdOptionsDefault, {inputName: 'target', inputValue: '', grabber: null}, options);
const i = $('')
.css({'width': options.width - 45})
.attr('name', options.inputName)
.val(options.inputValue);
if(options.grabber && this[options.grabber] instanceof Grabber){
this[options.grabber].set(i);
}else{
i.select();
}
options.content.append($('')
.append(i)
.append(''));
options.onSubmit = function(){
const v = i.val($.trim(i.val())).val();
if(v.length <= 0){
alert('Не заполнено обязательное поле');
return false
}
if(typeof onSubmit2 == 'function'){
return onSubmit2.call(this);
}
return true;
}
return this.sd(options);
}
H.prototype.sdLogin = function(options){
options.content = $('
' + conf[1],
glue: 'before',
life: conf[0],
sticky: conf[0] <= 0,
theme: 'msg_' + type
});
}
/* ================== перехват клика на логине/шмотке и т.п. ==================================== */
function Grabber(options){
this.options = $.extend({}, this.optionsDef, options);
this.input = null;
}
Grabber.prototype.toString = function(){
return 'This is Grabber-object';
}
Grabber.prototype.optionsDef = {
inputClass: 'grab'
}
Grabber.prototype.get = function(){
return this.input;
}
Grabber.prototype.isActive = function(){
return $(this.input).is(':visible');
}
Grabber.prototype.set = function(input){
const obj = this;
this.clear();
const tmp = $(input);
if(tmp.length > 0){
this.input = tmp.get(0);
$(this.input)
.addClass(this.options.inputClass)
.dblclick(function(){obj.toggle(this)})
.select();
return true;
}
return false;
}
Grabber.prototype.clear = function(){
$(this.input).removeClass(this.options.inputClass);
this.input = null;
},
Grabber.prototype.toggle = function(input){
if($(input).hasClass(this.options.inputClass)){
this.clear();
}else{
this.set($(input));
}
}