/*
MARKUP
NOTE:
* MUST always receive a 'iMAX'
*/
$.fn.uaBtn_NP = function(options)
{
options = $.extend({
iLIMIT: 0
},options);
let pNP_Model = {
iMAX : 0,
iSKIP : 0,
iLIMIT : options.iLIMIT,
clear : function()
{
this.iMAX = 0;
this.iSKIP= 0;//-1*options.iLIMIT; //0;
return this;
},
setMax : function(iMAX)
{
this.iMAX = iMAX;
return this;
},
increment : function()
{
this.iSKIP += this.iLIMIT;
return this;
},
decrement : function()
{
this.iSKIP -= this.iLIMIT;
this.iSKIP = this.iSKIP<0 ? 0 : this.iSKIP;
return this;
},
hasNext : function()
{
return (this.iSKIP + this.iLIMIT < this.iMAX);
},
hasPrev : function()
{
return (this.iSKIP >0);
},
get: function()
{
return {
iSKIP : this.iSKIP,
iLIMIT: this.iLIMIT
};
}
};
this.
on(EV_CLEAR,function(ev)
{
ev.stopImmediatePropagation();
pNP_Model.clear();
$.each([pBtn_Prev,pBtn_Next],function()
{
$(this).trigger($.Event(EV_ENABLE,{uaData:{data:{bEnable:false}}}));
});
}).
on(EV_SET,function(ev)
{//new max
ev.stopImmediatePropagation();
pNP_Model.setMax(ev.uaData.data.iMAX);
$(this).trigger(EV_UPDATE);
}).
on(EV_UPDATE,function(ev)
{
ev.stopImmediatePropagation();
//enable/disable buttons
pBtn_Next.trigger($.Event(EV_ENABLE,{uaData:{data:{bEnable:pNP_Model.hasNext()}}}));
pBtn_Prev.trigger($.Event(EV_ENABLE,{uaData:{data:{bEnable:pNP_Model.hasPrev()}}}));
}).
on(EV_NOTIFY,function(ev)
{
ev.stopImmediatePropagation();
switch(ev.uaData.info.ev)
{
case EV_NEXT:
pNP_Model.increment();
break;
case EV_PREV:
pNP_Model.decrement();
break;
default:
break;
};
$(this).trigger($.Event(EV_EXTERN, {uaData:{info:{ev:ev.uaData.info.ev},data:pBtn_Next.get()}}));
});
$.fn.uaBtn_NP_Base = function()
{
this.
on(EV_ENABLE, function(ev)
{
ev.stopImmediatePropagation();
switch(ev.uaData.data.bEnable)
{
case false:
$(this).addClass('uaDisabled');
break;
default:
$(this).removeClass('uaDisabled');
break;
};//switch(ev.uaData.data.bEnable)
}).
on('click',function(ev)
{
ev.stopImmediatePropagation();
if( $(this).hasClass('uaDisabled')) return;
$(this).trigger($.Event(EV_EXTERN,{uaData:{info:{ev:EV_CLICKED}}}));
});
return this;
};//$.fn.uaBtn_NP_Base = function()
//revious
const pBtn_Prev = $('i:first',this).uaBtn_NP_Base().
on(EV_EXTERN,function(ev)
{
ev.stopImmediatePropagation();
ev.uaData.info.ev=EV_PREV;
$(this).parent().parent().trigger($.Event(EV_NOTIFY,{uaData:ev.uaData}));
});
const pBtn_Next = $('i:last',this).uaBtn_NP_Base().
on(EV_EXTERN,function(ev)
{
ev.stopImmediatePropagation();
ev.uaData.info.ev=EV_NEXT;
$(this).parent().parent().trigger($.Event(EV_NOTIFY,{uaData:ev.uaData}));
});
this.get = function()
{
return pNP_Model.get();
};
return this;
};