(
function
( window){
$(document).ready(
function
(){
generateSystemMenu();
selectSystemMenu(
"system_challenge_menu"
);
getBreifUserInfo();
initInputComponent();
initDepartData();
bindEvent2Switcher();
});
var
CURRENT_SELECTED_ITEM = -1;
var
CURRENT_HTML_EDITOR =
null
;
function
initInputComponent(){
var
textareaArray =
new
Array(
"challenge_title_id"
,
"challenge_prescript_id"
,
"challenge_challenger_id"
);
$(
"#"
+ textareaArray[0]).focus();
$.each(textareaArray,
function
(i, item){
var
dynamicItem = $(
"#"
+ item);
bindPlaceHolder(dynamicItem);
dynamicItem.bind(
"keyup"
,
function
(event){
autoAdaptHeight(
this
);
setLengthHint(
this
);
});
});
}
function
autoAdaptHeight(component){
var
paddingTop = parseInt($(component).css(
"padding-top"
));
var
paddingBtm = parseInt($(component).css(
"padding-bottom"
));
var
scrollHeight = component.scrollHeight;
var
height = $(component).height();
if
(window.navigator.userAgent.indexOf(
"Chrome"
) > 0){
if
(scrollHeight - paddingTop - paddingBtm > height){
$(component).css(
"height"
, scrollHeight);
}
return
;
}
$(component).css(
"height"
, scrollHeight);
}
function
setLengthHint(component){
if
(component.id ==
"challenge_title_id"
){
if
(!component.value){
return
;
}
var
titleId = $(
"#challenge_title_hint_id"
);
if
(component.value && component.value.length > 96){
titleId.parent().show();
}
else
{
titleId.parent().hide();
}
titleId.text(component.value.length - 96);
}
}
function
initDepartData(){
asyncRequest(
"gainDepart.data"
,
null
,
function
(result){
var
resultJson = eval(result);
if
(!resultJson){
return
;
}
$(
"#challenge_depart_id"
).empty();
$.each(resultJson,
function
(i, item){
var
departItem = $(
"<div />"
).attr(
"class"
,
"challenge_depart_item"
).attr(
"id"
,
"challenge_depart_id_"
+ item.depId).text(item.depName);
departItem.click(
function
(){
if
(CURRENT_SELECTED_ITEM != -1 && CURRENT_SELECTED_ITEM != item.depId){
$(
"#challenge_depart_id_"
+ CURRENT_SELECTED_ITEM).attr(
"class"
,
"challenge_depart_item"
);
}
CURRENT_SELECTED_ITEM = item.depId;
$(
"#challenge_depart_id_"
+ item.depId).attr(
"class"
,
"challenge_depart_item_selected"
);
});
$(
"#challenge_depart_id"
).append(departItem);
});
});
}
function
bindEvent2Switcher(){
var
switcher = $(
"#challenge_editor_switcher"
);
var
display = switcher.attr(
"display"
);
switcher.click(
function
(){
var
parent = $(
"#challenge_prescript_id"
).parent();
if
(display ===
"default"
){
parent.removeClass(
"challenge_textarea_wrapper"
).addClass(
"challenge_richtext_wrapper"
);
display =
"custom"
;
switcher.attr(
"display"
,
"custom"
);
var
options = {resizeType: 0, items: [
"bold"
,
"italic"
,
"underline"
,
"strikethrough"
,
"|"
,
"insertorderedlist"
,
"insertunorderedlist"
,
"|"
,
"image"
]};
CURRENT_HTML_EDITOR = KindEditor.create(
"#challenge_prescript_id"
, options);
}
else
{
parent.removeClass(
"challenge_richtext_wrapper"
).addClass(
"challenge_textarea_wrapper"
);
display =
"default"
;
switcher.attr(
"display"
,
"default"
);
CURRENT_HTML_EDITOR =
null
;
KindEditor.remove(
"#challenge_prescript_id"
);
}
});
}
function
publishChallenge(){
var
challengeTitle = $.trim($(
"#challenge_title_id"
).val());
if
(!challengeTitle){
showSystemGlobalInfo(
"亲,您还没有写下问题呢~~~"
);
return
;
}
if
(CURRENT_SELECTED_ITEM === -1){
showSystemGlobalInfo(
"亲,您还没有选择科室呢~~~"
);
return
;
}
var
challengePrescript = $.trim($(
"#challenge_prescript_id"
).val());
if
(CURRENT_HTML_EDITOR){
challengePrescript = CURRENT_HTML_EDITOR.html();
}
var
challengers = $.trim($(
"#challenge_challenger_id"
).val());
var
data = {
"title"
: challengeTitle,
"departId"
: CURRENT_SELECTED_ITEM,
"prescript"
: challengePrescript,
"challengers"
: challengers};
asyncRequest(
"publishChallenge.data"
, data,
function
(result)
{
var
resultJson = eval(result);
top.location = resultJson.forwardPath;
});
}
window.publishChallenge = publishChallenge;
})( window );