javaeye3/public/javascripts/ask.js

421 lines
16 KiB
JavaScript
Executable File

document.observe("dom:loaded", function() {(function() {
var spinner_elem = new Element('img', {
src: '/images/spinner.gif',
'class': 'spinner'
});
var AskPageAction = Class.create({
initialize: function() {
this.data = new Hash();
},
toggle_action: function(event, event_element) {
this._event_element = event_element;
if (this._check()) {
this._clean();
if (this._is_show) this._factory().show();
}
event.stop();
},
get_event_element_parent_id: function() {
return this._event_element.readAttribute('data_parent_id');
},
set_status: function(status_str) {
if (status_str == 'showing') this.data.set('status', 'showing');
else this.data.set('status', undefined);
},
get_status: function() {
return this.data.get('status');
},
set_dropdown_status: function(status_str) {
var parent_id = this.get_event_element_parent_id();
this.data.set(parent_id + '_status', status_str);
this.set_status(status_str)
},
set_dropdown_current: function(action_obj) {
var parent_id = this.get_event_element_parent_id();
this.data.set(parent_id + '_current', action_obj);
},
get_dropdown_status: function() {
var parent_id = this.get_event_element_parent_id();
return this.data.get(parent_id + '_status');
},
get_dropdown_current: function() {
var parent_id = this.get_event_element_parent_id();
return this.data.get(parent_id + '_current');
},
set_popup_status: function(status_str) {
this.data.set('popup_status', status_str);
this.set_status(status_str)
},
set_popup_current: function(action_obj) {
this.data.set('popup_current', action_obj);
},
get_popup_status: function() {
return this.data.get('popup_status');
},
get_popup_current: function() {
return this.data.get('popup_current');
},
hide_current_popup: function(event_element) {
this._event_element = event_element;
var popup_current;
if (popup_current = this.get_popup_current()) popup_current.hide();
},
hide_current_dropdown: function(event_element) {
this._event_element = event_element;
var dropdown_current;
if (dropdown_current = this.get_dropdown_current()) dropdown_current.hide();
},
submit_comment: function(event_element) {
this._event_element = event_element;
if (this._event_element.previous('.spinner')) return;
var parent_id = this.get_event_element_parent_id;
var form_elem = event_element.up('form');
var comment_validate;
if (!this.data.get(parent_id + '_comment_validate')) {
comment_validate = new Validation(form_elem);
this.data.set(parent_id + '_comment_validate');
}
if (!comment_validate.validate()) return;
var url = form_elem['action'];
var right_spinner_elem = spinner_elem.clone();
right_spinner_elem.style.float = 'right';
this._event_element.insert({
before: right_spinner_elem
});
new Ajax.Request(url, {
method: 'post',
parameters: form_elem.serialize(),
onFailure:function(response){
form_elem.insert({top:response.responseText})
this._event_element.previous('.spinner').remove();
Element.scrollTo(form_elem);
}.bind(this),
onSuccess: function(transport) {
this._event_element.up(0).previous().value = '';
this._event_element.up(1).insert({
before: transport.responseText
});
this.get_dropdown_current().event_element().writeAttribute('data_comments_count', Number(this.get_dropdown_current().event_element().readAttribute('data_comments_count')) + 1);
this._event_element.previous('.spinner').remove();
}.bind(this)
});
},
destroy_comment: function(event_element) {
this._event_element = event_element;
if (this._event_element.previous('.spinner')) return;
var url = this._event_element.readAttribute('data_url');
var right_spinner_elem = spinner_elem.clone();
right_spinner_elem.style.float = 'right';
this._event_element.insert({
before: right_spinner_elem
});
new Ajax.Request(url, {
method: 'delete',
onSuccess: function(transport) {
this._event_element.up(1).remove();
this.get_dropdown_current().event_element().writeAttribute('data_comments_count', Number(this.get_dropdown_current().event_element().readAttribute('data_comments_count')) - 1);
this._event_element.previous('.spinner').remove();
}.bind(this)
});
},
show_problem_popup_notice: function(params) {
this._action_type = 'popup';
this._clean();
var notice_action = new NoticeAction(this);
notice_action.show(params);
},
//检测当前是否有层在显示中,以确定是否运行clean和show任务
//返回false表示有任务进行中,不清理和显示
//this._is_show表示是否显示新的层
_check: function() {
this._is_show = false;
var action_type = this._event_element.readAttribute('data_action_type');
this._action_type = action_type;
if (this.get_status() == 'showing') return false;
switch (action_type) {
case 'dropdown':
var dropdown_status = this.get_dropdown_status();
var dropdown_current = this.get_dropdown_current();
if (dropdown_status == undefined) { //当前没有下拉层
this._is_show = true;
} else if (dropdown_status == 'showed' && dropdown_current.event_element() != this._event_element) { //当前有下拉层,但需要显示新的下拉层
this._is_show = true;
} else if (dropdown_status == 'showing') { //有任务进行中
return false;
}
break;
case 'popup':
var popup_status = this.get_popup_status();
var popup_current = this.get_popup_current();
if (popup_status == undefined) {
this._is_show = true;
} else if (popup_status == 'showed' && popup_current.event_element() != this._event_element) {
this._is_show = true;
} else if (popup_status == 'showing') {
return false;
}
break;
default:
return false;
}
return true;
},
_clean: function() {
switch (this._action_type) {
case 'dropdown':
var dropdown_current = this.get_dropdown_current();
if (dropdown_current) {
dropdown_current.hide();
}
break;
case 'popup':
var popup_current = this.get_popup_current();
if (popup_current) {
popup_current.hide();
}
break;
}
},
_factory: function() {
var action_sub_type = this._event_element.readAttribute('data_action_sub_type');
var action_obj;
switch (action_sub_type) {
case 'comment':
action_obj = new CommentAction(this, this._event_element);
break;
case 'increment_score':
case 'edit_tags':
case 'replenish':
action_obj = new CommonDropdownAction(this, this._event_element);
break;
case 'close':
case 'destroy':
case 'postpone':
case 'solution_accept':
case 'solution_destroy':
action_obj = new ConfirmAction(this, this._event_element);
break;
}
return action_obj;
}
});
var PageAction = Class.create({
initialize: function(ask_page_action, event_element) {
this._ask_page_action = ask_page_action;
this._event_element = event_element;
},
event_element: function() {
return this._event_element;
}
});
var CommentAction = Class.create(PageAction, {
initialize: function($super, ask_page_action, event_element) {
$super(ask_page_action, event_element);
},
show: function() {
this._ask_page_action.set_dropdown_status('showing');
this._ask_page_action.set_dropdown_current(this);
this._event_element.appendChild(spinner_elem);
var url = this._event_element.readAttribute('data_url');
new Ajax.Request(url, {
method: 'get',
onSuccess: function(transport) {
this._event_element.up().addClassName('action_selected');
this._event_element.up(2).addClassName('action_bg');
var img_elem = this._event_element.down();
this._event_element.update(img_elem);
this._event_element.appendChild(document.createTextNode('收起评论'));
this._event_element.up(2).insert({
after: transport.responseText
});
this._ask_page_action.set_dropdown_status('showed');
}.bind(this)
});
},
hide: function() {
var img_elem = this._event_element.down();
this._event_element.up().removeClassName('action_selected');
this._event_element.up(2).removeClassName('action_bg');
this._event_element.update(img_elem);
var comments_count = this._event_element.readAttribute('data_comments_count');
comments_count = Number(comments_count);
var comments_count_text;
if (comments_count > 0) comments_count_text = document.createTextNode(comments_count + '条评论');
else comments_count_text = document.createTextNode('添加评论');
this._event_element.appendChild(comments_count_text);
this._event_element.up(2).next().remove();
this._ask_page_action.set_dropdown_status(undefined);
this._ask_page_action.set_dropdown_current(undefined);
}
});
var CommonDropdownAction = Class.create(PageAction, {
initialize: function($super, ask_page_action, event_element) {
$super(ask_page_action, event_element);
},
show: function() {
this._ask_page_action.set_dropdown_status('showing');
this._ask_page_action.set_dropdown_current(this);
this._event_element.up().addClassName('action_selected');
this._event_element.up(2).addClassName('action_bg');
var action_sub_type = this._event_element.readAttribute('data_action_sub_type');
this._event_element.up(2).insert({
after: window.html_codes.get(action_sub_type)
});
this._ask_page_action.set_dropdown_status('showed');
},
hide: function() {
var img_elem = this._event_element.down();
this._event_element.up().removeClassName('action_selected');
this._event_element.up(2).removeClassName('action_bg');
this._event_element.up(2).next().remove();
this._ask_page_action.set_dropdown_status(undefined);
this._ask_page_action.set_dropdown_current(undefined);
}
});
var PopupAction = Class.create(PageAction, {
initialize: function($super, ask_page_action, event_element) {
$super(ask_page_action, event_element);
},
show: function() {
this._ask_page_action.set_popup_status('showing');
this._ask_page_action.set_popup_current(this);
var action_sub_type = this._event_element.readAttribute('data_action_sub_type');
$(document.body).insert({
bottom: window.html_codes.get(action_sub_type)
});
this._set_problem_popupbox_style();
this._ask_page_action.set_popup_status('showed');
},
hide: function() {
$('problem_popupbox').remove();
this._ask_page_action.set_popup_status(undefined);
this._ask_page_action.set_popup_current(undefined);
},
_set_problem_popupbox_style: function() {
if (!$('problem_popupbox')) return;
var viewport = document.viewport.getDimensions();
$('problem_popupbox').setStyle({
'position': 'fixed',
'left': (viewport.width - $('problem_popupbox').getDimensions().width) / 2 + 'px',
'top': (viewport.height - $('problem_popupbox').getDimensions().height) / 2 + 'px'
});
if (navigator.userAgent.indexOf("MSIE 6") > 0) { // IE6
$('problem_popupbox').setStyle({
'position': 'absolute',
'top': document.documentElement.scrollTop + (viewport.height - $('problem_popupbox').getDimensions().height) / 2 + "px"
});
}
}
});
var ConfirmAction = Class.create(PopupAction, {
initialize: function($super, ask_page_action, event_element) {
$super(ask_page_action, event_element);
},
show: function() {
this._ask_page_action.set_popup_status('showing');
this._ask_page_action.set_popup_current(this);
var action_sub_type = this._event_element.readAttribute('data_action_sub_type');
var html_code = window.html_codes.get(action_sub_type);
if (this._event_element.readAttribute('replace_url')) {
html_code = html_code.replace('action_url', this._event_element.readAttribute('data_url'));
}
$(document.body).insert({
bottom: html_code
});
this._set_problem_popupbox_style();
this._ask_page_action.set_popup_status('showed');
}
});
var NoticeAction = Class.create(PopupAction, {
initialize: function($super, ask_page_action) {
$super(ask_page_action, null);
},
show: function(params) {
this._ask_page_action.set_popup_status('showing');
this._ask_page_action.set_popup_current(this);
var html_code = window.html_codes.get('problem_popup_notice');
var notice_template = new Template(html_code);
var delay_number = params['delay_number'] || 0
var delay_func = params['delay_func']
if (delay_number > 0) {
if(delay_func){
delay_func.delay(delay_number);
}else{
this.delay_hide.bind(this).delay(delay_number);
params['class_name'] = params['class_name'] + ' delay';
}
}
$(document.body).insert({
bottom: notice_template.evaluate(params)
});
this._set_problem_popupbox_style();
this._ask_page_action.set_popup_status('showed');
},
delay_hide: function() {
var problem_popupbox = $('problem_popupbox');
if (problem_popupbox && problem_popupbox.className.include('delay')) {
this.hide();
}
}
});
var ask_page_action = new AskPageAction();
window.ask_page_action = ask_page_action;
if ($$('div.ask_action_handler').length > 0) {
$(document.body).observe('click', function(event) {
var event_elem;
if (event_elem = event.findElement('a.ask_click_action')) {
ask_page_action.toggle_action(event, event_elem);
} else if (event_elem = event.findElement('input#close_popup_action')) {
ask_page_action.hide_current_popup(event_elem);
} else if (event_elem = event.findElement('input.close_dropdown_action')) {
ask_page_action.hide_current_dropdown(event_elem);
} else if (event_elem = event.findElement('input.submit_comment')) {
ask_page_action.submit_comment(event_elem);
} else if (event_elem = event.findElement('a.destroy_comment')) {
ask_page_action.destroy_comment(event_elem);
} else if (event_elem = event.findElement('input.after_show_spinner')) {
event_elem.disabled = true;
event_elem.insert({
after: spinner_elem
});
event_elem.form.submit();
} else if (event_elem = event.findElement('input.before_show_spinner')) {
event_elem.disabled = true;
var before_spinner_elem = spinner_elem.clone();
before_spinner_elem.className = 'spinner_img';
event_elem.insert({
before: before_spinner_elem
});
event_elem.form.submit();
}
});
$(document).observe('keyup', function(event){
var keyCode = event ? (event.which ? event.which : event.keyCode) : window.event.keyCode;
var event_elem;
if( (keyCode == 27) && (event_elem = $('close_popup_action')) ){ // escape key only, no enter key
ask_page_action.hide_current_popup(event_elem);
}
});
}
})();
});