sg-action
<a class="sg-action" href="" data-rel="this" data-ret="" data-confirm="Message" data-rel="close" data-removeparent="tr">Link</a>
Attribute
class = sg-action
data-rel = none | notify | this | box | parent[:#ID,Class] replace [:#ID,Class] | after | Element ID
data-done = notify | javascript | callback | back | close | moveto | remove:parent .class | reload:url | load[->replace,before,after,append,prepend,prev,next,clear]:id,class: url
data-ret = url
data-title = Title
data-confirm = Message
data-do = closebox
data-removeparent = Tag | Class | Element ID
data-closebox = yes
data-moveto = x,y
data-callback = Function Name | url
data-box-done = done command
Source
$(document).on('click','.sg-action', function(e) {
var $this=$(this)
var url=$this.attr('href')
var rel=$this.data('rel')
var ret=$this.data('ret')
var para={}
if (url=='javascript:void(0)') url=$this.data('url');
if ($this.data('confirm')!=undefined) {
if (confirm($this.data('confirm'))) {
para.confirm='yes'
} else {
e.stopPropagation()
return false
}
}
if ($this.data('do')=='closebox') {
if ($(e.target).closest('.sg-dropbox.box').length!=0) {
//alert('Close box '+$(e.target).closest('.sg-dropbox.box').attr('class'))
$('.sg-dropbox.box').children('div').hide()
$('.sg-dropbox.box.active').removeClass('active')
} else $.colorbox.close()
//e.stopPropagation()
return false
}
notify('กำลังโหลด.........');
console.log("Load from url "+url)
if (rel==undefined && ret==undefined) return true;
$.get(url,para,function(html) {
console.log("Load from url "+url+" completed.")
if (ret) {
$.get(ret,function(html) {
if (rel=='box') $.colorbox({html:html,width:$('#colorbox').width()});
else if (rel=='this') $this.html(html);
else if (rel=='replace') $this.replaceWith(html);
else if (rel=='after') $this.after(html);
else if (rel=='notify') notify(html,20000);
else $('#'+rel).html(html);
notify()
})
} else {
notify()
if (rel=="none") ;
else if (rel=='box') $.colorbox({html:html,width:$('#colorbox').width()});
else if (rel=='this') $this.html(html);
else if (rel.substr(0,6)=='parent') {
//$this.parent().html(html);
var $ele;
if (rel=='parent') $ele=$this.parent();
else {
var target=rel.substr(7);
console.log(target)
$ele=$this.closest(target);
}
$ele.html(html);
} else if (rel.substr(0,7)=='replace') {
var $ele;
if (rel=='replace') $ele=$this;
else {
var target=rel.substr(8);
$ele=$(target);
}
$ele.replaceWith(html);
} else if (rel=='after') {
$this.after(html);
} else if (rel=='notify') {
notify(html,20000);
} else $('#'+rel).html(html);
if ($this.data('moveto')) {
var moveto=$this.data('moveto').split(',');
window.scrollTo(parseInt(moveto[0]), parseInt(moveto[1]));
}
}
if ($this.data('removeparent')) {
var removeTag=$this.data('removeparent');
$this.closest(removeTag).remove();
}
if ($this.data('closebox')) {
if ($(e.target).closest('.sg-dropbox.box').length===0) {
$('.sg-dropbox.box').children('div').hide()
$('.sg-dropbox.box.active').removeClass('active')
} else $.colorbox.close()
}
// Process callback function
var callback=$this.data("callback");
if (callback && typeof window[callback] === 'function') {
window[callback]($this,html);
} else if (callback) {
window.location=callback;
}
})
return false;
});