96 lines
3.0 KiB
JavaScript
96 lines
3.0 KiB
JavaScript
/************************************************************************
|
|
*************************************************************************
|
|
@Name : QapTcha - jQuery Plugin
|
|
@Revison : 2.5
|
|
@Date : 26/01/2011
|
|
@Author: Surrel Mickael (www.myjqueryplugins.com - www.msconcept.fr)
|
|
@License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
|
|
|
|
**************************************************************************
|
|
*************************************************************************/
|
|
jQuery.QapTcha = {
|
|
build : function(options)
|
|
{
|
|
var defaults = {
|
|
txtLock : '发表评论前,请滑动滚动条解锁',
|
|
txtUnlock : '已解锁,可以发表评论了',
|
|
disabledSubmit : false,
|
|
autoRevert : true,
|
|
autoSubmit : false,
|
|
url : '/iQapTcha4tyepcho',
|
|
action : 'iQapTcha4tyepcho'
|
|
};
|
|
|
|
if(this.length>0)
|
|
return jQuery(this).each(function(i) {
|
|
/** Vars **/
|
|
var
|
|
opts = $.extend(defaults, options),
|
|
$this = $(this),
|
|
form = $('form').has($this),
|
|
Clr = jQuery('<div>',{'class':'clr'}),
|
|
bgSlider = jQuery('<div>',{id:'bgSlider'}),
|
|
Slider = jQuery('<div>',{id:'Slider'}),
|
|
Icons = jQuery('<div>',{id:'Icons'}),
|
|
TxtStatus = jQuery('<div>',{id:'TxtStatus','class':'dropError',text:opts.txtLock}),
|
|
inputQapTcha = jQuery('<input>',{name:'iQapTcha',value:'',type:'hidden'});
|
|
|
|
/** Disabled submit button **/
|
|
if(opts.disabledSubmit) form.find('input[type=\'submit\']').attr('disabled','disabled');
|
|
|
|
/** Construct DOM **/
|
|
bgSlider.appendTo($this);
|
|
Icons.insertAfter(bgSlider);
|
|
TxtStatus.insertAfter(Icons);
|
|
Clr.insertAfter(TxtStatus);
|
|
|
|
|
|
inputQapTcha.appendTo($this);
|
|
Slider.appendTo(bgSlider);
|
|
$this.show();
|
|
|
|
Slider.draggable({
|
|
revert: function(){
|
|
if(opts.autoRevert)
|
|
{
|
|
if(parseInt(Slider.css("left")) > 150) return false;
|
|
else return true;
|
|
}
|
|
},
|
|
containment: bgSlider,
|
|
axis:'x',
|
|
stop: function(event,ui){
|
|
if(ui.position.left > 150)
|
|
{
|
|
inputQapTcha.val(generatePass(32));
|
|
$.post(opts.url,{
|
|
action : opts.action,
|
|
iQaptcha : inputQapTcha.val()
|
|
},
|
|
function(data) {
|
|
if(data.code)
|
|
{
|
|
Slider.draggable('disable').css('cursor', 'default');
|
|
TxtStatus.text(opts.txtUnlock).addClass('dropSuccess').removeClass('dropError');
|
|
Icons.css('background-position', '-16px 0');
|
|
form.find('input[type=\'submit\']').removeAttr('disabled');
|
|
if(opts.autoSubmit) form.find('input[type=\'submit\']').trigger('click');
|
|
}
|
|
},'json');
|
|
}
|
|
}
|
|
});
|
|
|
|
function generatePass(nb) {
|
|
var chars = 'azertyupqsdfghjkmwxcvbn23456789AZERTYUPQSDFGHJKMWXCVBN_-#@';
|
|
var pass = '';
|
|
for(i=0;i<nb;i++){
|
|
var wpos = Math.round(Math.random()*chars.length);
|
|
pass += chars.substring(wpos,wpos+1);
|
|
}
|
|
return pass;
|
|
}
|
|
|
|
});
|
|
}
|
|
}; jQuery.fn.QapTcha = jQuery.QapTcha.build; |