scb是straycat_bag,流浪猫的背包,从5月份开始学习javascript开始慢慢积累的一些代码。很快这个库就要成为AegeanSiren的一部分了。这个最后的版本留作纪念。
这些代码受GPLv3协议保护
$.scb = {
web_root: $('#web_root.js_data').text(),
static_root: $('#static_root.js_data').text(),
page_owner_guid: $('#page_owner_guid.js_data').text(),
page_owner_type: $('#page_owner_type.js_data').text(),
msg_e_tpl: '抱歉, %s。您可以再试一次。如果仍出现问题并且您确定您的操作无误,也许我们遇到了技术困难或者正在与入侵我们网站的死星掠夺者苦战。烦请点击网站下方的反馈链接向我们报告错误,谢谢
',
msg_empty_tpl: '请填写 %s',
adjustTextareaSize: function(node){
var $area = $(node);
var font_size = $area.css('font-size');
var line_height = $area.css('line-height');
var width = $area.css('width');
var font_family = $area.css('font-family');
var val = $area.val();
var min_height = parseInt($area.css('min-height'));
if(!min_height ){
min_height = parseInt(line_height) * 5;
}
var $twc = $('#twc'); /*text width calculator*/
if(0 == $twc.length){
$twc = $('
');
$twc.hide();
$('body').append($twc);
}
$twc.css({'font-size':font_size,'line-height':line_height,'font-family':font_family,'width':width});
$twc.text(val);
var height = parseInt($twc.height());
if( height < min_height )
{
height = min_height;
}
var limit = 500;
if(limit scrollHeight) && (scrollHeight > height)){
$area.height(scrollHeight);
}
},
cleanSp: function(node){
var $node = $(node);
var $next = $node.next();
var $parent = $node.parent();
if(0 == $next.length){
$prev = $node.prev();
if($prev.hasClass('sp')){
$prev.remove();
}
}else if($next.hasClass('sp')){
$next.remove();
}
},
/**
*
*/
log: function(msg,level){
/* check if log container exists */
var $log_container = $('#scb_log');
if(0 == $log_container.length){
$('#f_hd').prepend('
');
$log_container = $('#scb_log');
}
/* covert level to class */
var class = '';
switch(level){
case 'log':
default:
class = 'notice';
break;
case 'success':
class = 'success';
break;
case 'error':
class = 'error';
break;
}
$log_item = $('
'+msg+'
');
$log_container.prepend($log_item);
$log_item.click(function(event){
$this = $(this);
$this.fadeOut(function(){
$this.remove();
});
});
setTimeout(function(){
var $log_item = $('#scb_log .log:first');
$log_item.fadeOut(function(){
$log_item.remove();
});
},60000);
},
l: function(msg){
$.scb.log(msg,'log');
},
s: function(msg){
$.scb.log(msg,'success');
},
e: function(msg){
msg = $.scb.msg_e_tpl.replace('%s',msg);
$.scb.log(msg,'error');
},
/**
*
*/
validateEmail: function($node){
var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return reg.test($node.val());
},
/**
* activate next panel in the accordion the given node in
*/
activateNextAccordion: function(node){
try{
var $node = $(node);
var $content;
if($node.hasClass('ui-accordion-content')){
$content = $node;
}else{
$content = $node.closest('.ui-accordion-content');
}
$next = $content.next('.ui-accordion-header');
if(!$next.length){
var $accordion = $content.closest('.ui-accordion');
$next = $accordion.find('.ui-accordion-header:first');
}
$next.click();
}catch(e){
$.scb.e(e)
}
},/*activateNextAccordion()*/
/**
*
*/
collapseAllAccordion: function(node){
try{
var $accordion = $(node).closest('.ui-accordion');
var length = $accordion.find('.ui-accordion-header').length;
$accordion.accordion('activate',length);
}catch(e){
$.scb.e(e)
}
},
/**
*
*/
ajax: function(action,data,success_callback,fail_callback){
var a;
/*this temporary codes will be override*/
if(('//') == action.substr(0,2) || ('http' == action.substr(0,4)))
{
a = action;
}
else
{
a = $.scb.web_root+'a/'+$.scb.page_owner_type+'/'+action+'/'+$.scb.page_owner_guid;
}
data.via = 'json';
$.ajax({
url:a,
type:'POST',
cache:false,
data:data,
dataType:'json',
success:function(response)
{
try
{
if(response.success)
{
if($.isFunction(success_callback)){
success_callback(response.data);
}
if(response.message){
$.scb.s(response.message);
}
}
else
{
if($.isFunction(fail_callback)){
fail_callback();
}
if(response.message.length){
$.scb.e(response.message);
}
}
}
catch(e)
{
$.scb.e(e);
}
},
error:function(xhr,error)
{
if($.isFunction(fail_callback)){
fail_callback();
}
$.scb.e(error);
},
});
},/*ajax*/
isUsingTinyMCE: function(node){
var $node = $(node);
return (('undefined' != typeof(tinyMCE)) && $.isFunction(tinyMCE.get) && $node.attr('id') && tinyMCE.get($node.attr('id')));
}
};/*scb*/
$.fn.highlightOnFocus = function(hlSelector){
this.bind('focus',{selector:hlSelector},function(event){
$(this).closest(event.data.selector).addClass('hl');
});
this.bind('blur',{selector:hlSelector},function(event){
$(this).closest(event.data.selector).removeClass('hl');
});
return this;
};
$.fn.highlightOnHover = function(){
this.hover(
function(event){
$(this).addClass('hl');
},
function(event){
$(this).removeClass('hl');
}
);
};
$.fn.autoSizeTextarea = function(){
return this.each(function(){
$.scb.adjustTextareaSize(this);
$(this).keyup(function(){
$.scb.adjustTextareaSize(this);
});
});
};
$.fn.processDefVal = function(){
return this.each(function(){
var $this = $(this);
if($this.val() == $this.attr('defval')){
$this.addClass('quiet');
}
$this.focus(function(event){
var $this = $(this);
if($this.val() == $this.attr('defval')){
$this.val('');
}
$this.removeClass('quiet');
});
$this.blur(function(event){
var $this = $(this);
if('' == $this.val()){
$this.val($this.attr('defval'));
$this.addClass('quiet');
}else if($this.val() == $this.attr('defval')){
$this.addClass('quiet');
}else{
$this.removeClass('quiet');
}
});
});
};
$.fn.validate = function(params){
if(!$.scb.validate_count){
$.scb.validate_count = 0;
}
return this.each(function(){
var $this = $(this);
var validate_id = $.scb.validate_count++;
$this.attr('vldid',validate_id);
$this.bind('blur',{callback:params.callback,validator:params.validator},function(event){
try{
var callback = event.data.callback;
if(!$.isFunction(callback)){
throw 'given callback isn\'t a function';
}
var validator = event.data.validator;
if(!$.isFunction(validator)){
throw 'given validator isn\'t a function';
}
var $this = $(this);
result = validator($this);
callback(result);
}catch(e){
$.scb.e(e)
}
});
});
};
$.fn.limitHeight = function(opt){
return this.each(function(){
var $this = $(this);
var height = $this.height();
var line_height = parseInt($this.css('line-height'));
var limit;
if(opt && opt.limit){
limit = opt.limit;
}else{
limit = 10;
}
limit = limit * line_height;
if(height > limit){
$this.height(limit);
}
});
};
$.fn.unlimitHeight = function(){
return this.each(function(){
var $this = $(this);
$this.height('auto');
});
};
Posted via email from Meow and Grrr of a Stray Cat