var jcrop_api = null;
var ue = null;
var savePath = '/Uploads/ShimaoNews/';
var OldPictureUrl = "";//原图路径
var CheckTitleTab = 0;//新闻标题是否重复判别字段
//图片宽高、临时数据记录图片宽高、图片、左边距、上边距
$(function () {
//初始载入新闻分类
ChangeNewsCategoryName();
//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
ue = UE.getEditor('editor');
$("#LyncSmallPicture").uploadify(uploadsetting);
//$("#btnSave").click(saveNews);
$("#typeId").change(function () {
var NewsType = $("#typeId").val();
if (NewsType == "2") {
$("#zuozhe").find(".span_red").remove();
$("#areaquyu").find(".span_red").remove();
$("#fenlei").find(".span_red").remove();
$("#lyncxiao").find(".span_red").remove();
$("#lyncda").find(".span_red").remove();
$("#newsxiao").find(".span_red").remove();
$("#newsda").find(".span_red").remove();
$("#jianshu").find(".span_red").remove();
$("#AreaNameOrId").val("29");
$("#AreaNameOrId").attr("disabled", "disabled");
}
else {
var spanhtml = '<span class="span_red">*</span>';
if ($("#zuozhe").find(".span_red").length == 0) {
$("#zuozhe").append(spanhtml);
$("#areaquyu").append(spanhtml);
$("#fenlei").append(spanhtml);
$("#lyncxiao").append(spanhtml);
$("#lyncda").append(spanhtml);
$("#newsxiao").append(spanhtml);
$("#newsda").append(spanhtml);
$("#jianshu").append(spanhtml);
$("#AreaNameOrId").attr("disabled", "disabled");
$("#AreaNameOrId").removeAttr("disabled");
}
}
ChangeNewsCategoryName();
})//新闻类别更改
$("#btn_aggin").click(function () {
location.reload();//重写新闻
});
$("#btn_back").click(BackHostry);//返回历史页面
$("#edui1_bottombar").remove();
})
//上传设置
var uploadsetting = {
//指定swf文件
'swf': "/Applications/News/Scripts/uploadify/uploadify.swf",
//后台处理的页面
'uploader': '/News/UploadHandler',
//按钮显示的文字
'buttonText': '选择文件',
显示的高度和宽度,默认 height 30;width 120
'height': 28,
'width': 97,
//上传文件的类型 默认为所有文件 'All Files' ; '*.*'
//在浏览窗口底部的文件类型下拉菜单中显示的文本
'fileTypeDesc': '图片',
'fileSizeLimit': 3 * 1024,///限制文件上传大小小于3M
///每次选择,传入Cookie和Session
'onSelectOnce': function (e, data) {
$('#LyncSmallPicture').uploadifySettings('scriptData', { 'ASPSESSID': ASPSESSID, 'AUTHID': auth });
},
//允许上传的文件后缀
'fileTypeExts': '*.jpg; *.png;',
//发送给后台的其他参数通过formData指定
'formData': { 'savepath': '/Uploads/ShimaoNews/SourceImg/' },
//上传文件页面中,你想要用来作为文件队列的元素的id, 默认为false 自动生成, 不带#
'queueID': 'fileQueue',
//选择文件后自动上传
'auto': true,
//设置为true将允许多文件上传
'multi': false,
'overrideEvents': ['onSelectError', 'onDialogClose'],
'onSelectError': function (file, errorCode, errorMsg) {
if (errorCode == -110) {
//alert("文件" + file.name + "超过" + $('#upfile').uploadify("settings", 'fileSizeLimit') + "大小限制,请重新上传");
alert("上传图片必须小于3M");
return false;
}
},
//上传后执行的代码
'onUploadSuccess': function (file, data, response) {
var result = eval("(" + data + ")");
if (result["isok"]) {
if (jcrop_api) {
jcrop_api.destroy();
$("#popInnern_Content").html(appendHtmlStr)
}
//设置需要剪切的图片名称
$("#cutSourceImgName").val(result["newname"]);
$("#imgSource").attr("src", "");
$("#imgSource").attr("src", "/Uploads/ShimaoNews/SourceImg/" + result["newname"]);
$("#lyncSmallimg").attr("src", "/Uploads/ShimaoNews/SourceImg/" + result["newname"]);
$("#lyncBigimg").attr("src", "/Uploads/ShimaoNews/SourceImg/" + result["newname"]);
$("#newsSmallimg").attr("src", "/Uploads/ShimaoNews/SourceImg/" + result["newname"]);
$("#newsBigimg").attr("src", "/Uploads/ShimaoNews/SourceImg/" + result["newname"]);
$("#LyncBigPictureNewNameAfterJcrop").val("");
$("#LYNCSmallImgNewNameAfterJcrop").val("");
$("#newsSmallImgNewNameAfterJcrop").val("");
$("#NewsBigPictureImgNewNameAfterJcrop").val("");
$("#LYNCSmallSetState").show();
$("#LYNCSmallOverSetState").hide();
$("#LYNCBigSetState").show();
$("#LYNCBigOverSetState").hide();
$("#NewsSmallSetState").show();
$("#NewsSmallOverSetState").hide();
$("#NewsBigSetState").show();
$("#NewsBigOverSetState").hide();
//获取图片对象
$("#imgSource").load(function () {
img = null;
if (this.complete || this.readyState == "complete") {
img = $("#imgSource");
var ratioImg = new Image();
ratioImg.src = img;
AutoResizeImage(643, 396, img)
var contentlength = result["contentlength"];
//更改:上传图片不能大于3M edit by sam
if (contentlength > 3145728) {
alert("上传图片必须小于3M");
hidPictureCover();
} else {
appendHtmlStr = $("#popInnern_Content").html();
openPictureCover();
}
}
})
}
},
'onUploadError': function (file, errorCode) {
if (file.type === "File Size") {
alert('超过文件上传大小限制(3M)!');
return;
}
}
}
//初始化裁剪.裁剪区域宽高,裁剪框位置,要裁减的div的宽高
function initJcrop(width, height, positionX, positionY, cutDivWidth, cutDivHeight, type) {
$('#jcropDiv').Jcrop({
onRelease: releaseCheck,
onSelect: updateCoords,
allowResize: true,
aspectRatio: cutDivWidth / cutDivHeight
}, function () {
jcrop_api = this;
//设置选择框的大小范围
jcrop_api.setOptions({
minSize: [parseInt(width), parseInt(height)],
maxSize: [sourceWidth, sourceHeight],
keySupport: true,
fixedSupport: true,
allowSelect: !!this.checked
});
//初始化一下选择框的大小,防止没有移动的时候没有值
$('#x').val(positionX);
$('#y').val(positionY);
$('#w').val(parseInt(width));
$('#h').val(parseInt(height));
});
//自动把选择框跳转到图片中间位置
if (type == "1") {
jcrop_api.setSelect([positionX, positionY, 300, 200]);
console.log(jcrop_api.tellSelect());
var t = jcrop_api.tellSelect();
} else if (type == "2") {
jcrop_api.animateTo([positionX, positionY, 570, 150]);
} else if (type == "3") {
jcrop_api.animateTo([positionX, positionY, 440, 270]);
} else if (type == "4") {
jcrop_api.animateTo([positionX, positionY, 1100, 390]);
}
//jcrop_api.animateTo([positionX, positionY, 200, 300]);
};
function releaseCheck() {
jcrop_api.setOptions({ allowSelect: !!this.checked });
};
///检查新闻标题是否存在
function CheckNewsTitle() {
var Title = $("#title").val();
$.ajax({
url: "/News/CheckNewsTitle",
data: { "NewsTitle": Title },
type: "POST",
success: function (res) {
if (res["success"] == 1) {
CheckTitleTab = 0;
}
else {
CheckTitleTab = 1;
alert("新闻标题已存在!");
$("#title").focus();
}
}
})
}
//设置图片需要裁减的位置坐标
function updateCoords(c) {
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
};
//裁剪图片
function cutImg() {
var postData = {
"imgName": $("#cutSourceImgName").val(),
"width": parseInt($("#w").val()),
"height": parseInt($("#h").val()),
"top": parseInt($("#y").val()),
"left": parseInt($("#x").val()),
"type": $("#cutSourceType").val(),
"sourceWidth": parseInt(sourceWidth),
"sourceHeight": parseInt(sourceHeight),
"ratio": ratio
}
$.ajax({
url: "/News/CutImage",
data: postData,
type: "POST",
success: function (res) {
if (res["isok"]) {
closePopUp();
switch ($("#cutSourceType").val()) {
case "LyncSmallPicture":
$("#LYNCSmallImgNewNameAfterJcrop").val(res["afterCutImgName"]);
$("#LYNCSmallSetState").hide();
$("#LYNCSmallOverSetState").show();
break
case "LyncBigPicture":
$("#LyncBigPictureNewNameAfterJcrop").val(res["afterCutImgName"]);
$("#LYNCBigSetState").hide();
$("#LYNCBigOverSetState").show();
break
case "NewsSmallPicture":
$("#newsSmallImgNewNameAfterJcrop").val(res["afterCutImgName"]);
$("#NewsSmallSetState").hide();
$("#NewsSmallOverSetState").show();
break
case "NewsBigPicture":
$("#NewsBigPictureImgNewNameAfterJcrop").val(res["afterCutImgName"]);
$("#NewsBigSetState").hide();
$("#NewsBigOverSetState").show();
break
default:
break;
}
} else {
alert(result["msg"]);
}
}
});
}
//保存新闻
function saveNews() {
var Context = ue.getContent();
//var postData = {
// "ueditor": escape(ueditor)
//}
//定义字段并赋值
var Title = $("#title").val();
//判断文章标题不得为空
if (Title == '') {
alert("文章标题不得为空!");
return;
}
if (CheckTitleTab == 1) {
alert("文章标题已存在!");
return;
}
//判断文章内容不得为空
if (Context == '') {
alert("文章内容不得为空!");
return;
}
var AuthorId = $("input[name=userId]").val().replace(",", "");
AuthorId = AuthorId.replace(",", "");
if ($("#cutSourceImgName").val() == '') {
OldPictureUrl = $("#cutSourceImgName").val();
}
else {
OldPictureUrl = $("#cutSourceImgName").val().replace("Uploads/ShimaoNews/SourceImg/", "");
}
var TypeId = $("#typeId").val();
var AreaId = $("#AreaNameOrId").val();
var NewsCategoryId = $("#NewsCategoryName").val();
var NewsSmallPicture = $("#newsSmallImgNewNameAfterJcrop").val();
var NewsBigPicture = $("#NewsBigPictureImgNewNameAfterJcrop").val();
var LyncSmallPicture = $("#LYNCSmallImgNewNameAfterJcrop").val();
var LyncBigPicture = $("#LyncBigPictureNewNameAfterJcrop").val();
var AbstractContent = $("#description").val();
var CreatorID = 0;
if (TypeId == '1') {
if (AuthorId == '') {
alert("请输入作者!");
return;
}
if (LyncSmallPicture == '' || LyncBigPicture == '') {
alert("请设置Lync图片!");
return;
}
if (NewsSmallPicture == '' || NewsBigPicture == '') {
alert("请设置新闻图片!");
return;
}
if (AbstractContent == '') {
alert("请输入新闻简介!");
return;
}
if (AbstractContent.length < 30) {
alert("简介最少输入30字!");
return;
}
}
else {
if (AuthorId == '') {
AuthorId = 0
}
if (LyncSmallPicture == '' || LyncBigPicture == '') {
alert("请选择Lync图片!");
return;
}
if (AbstractContent != '' && AbstractContent.length < 30) {
alert("简介最少输入30字!");
return;
}
}
Context = escape(Context);//给内容加密
//发布新闻字段
var postData = {
"Title": Title,
"AuthorId": AuthorId,
"TypeId": TypeId,
"AreaId": AreaId,
"NewsCategoryId": NewsCategoryId,
"NewsSmallPicture": NewsSmallPicture,
"NewsBigPicture": NewsBigPicture,
"LyncBigPicture": LyncBigPicture,
"LyncSmallPicture": LyncSmallPicture,
"AbstractContent": AbstractContent,
"CreatorID": CreatorID,
"Context": Context,
"OldPictureUrl": OldPictureUrl
}
$.ajax({
url: "/News/SubmitNews",
data: postData,
type: "POST",
success: function (res) {
//if (success == 1)
//{
// alert("发布成功!");
//}
$(".Wrapper").hide();
$(".Wrapper_Success").show();
timeBegin();
}
});
}
//改变select新闻分类名
function ChangeNewsCategoryName() {
var NewsType = $("#typeId").val();
var data = {
"NewsType": NewsType
}
$.ajax({
url: "/News/GetNewsCategoryListByNewsType",
data: data,
type: "POST",
success: function (res) {
var NewsCategoryList = res;
$("#NewsCategoryName").find("option").remove();
for (var i = 0; i < NewsCategoryList.length; i++) {
var NewsCategoryNameOption = "<option value='" + NewsCategoryList[i]["NewsCategoryId"] + "'>" + NewsCategoryList[i]["NewsCategoryName"] + "</option>";
$("#NewsCategoryName").append(NewsCategoryNameOption); //为Select追加一个Option(下拉项)
}
}
});
}
//返回历史页面
function BackHostry() {
//window.history.back(-1);
location.href = "/News";
history.go(-1);
//window.history.go(-1);
}
//倒计时
function timeBegin() {
var time = parseInt($("#time_num").html()) - 1;
//如果时间为零,返回上一页
if (time == 0) {
BackHostry();
}
//否则时间减一赋给标签
else {
setTimeout("timeBegin()", 1000);//设置1000毫秒以后执行一次本函数
$("#time_num").html(time);
}
}
//预览
function preview() {
$(".previewCover .popContn .popInnern .popInnern_Content *").remove();
$(".popInnern span").text("");
$(".popInnern span").text($("#title").val());
$(".previewCover .popContn .popInnern .popInnern_Content").append(ue.getContent());
$(".previewCover").show();
}
function closePopUpPreview() {
$(".previewCover").hide();
}
//限制字符长度//检查长度
function checklength(obj, maxlength, toID) {
//var length = maxlength - lentgh(obj.value);
var length = maxlength - $(obj).val().length;
if (length < 0) {
length = 0;
var strLength = $(obj).val().substring(0, maxlength);
$(obj).val(strLength);
alert("请输入长度不超过" + maxlength + "的字符");
}
document.getElementById(toID).innerHTML = $(obj).val().length + "/" + maxlength + "字符";
//if ($(obj).val().length > maxlength) {
// alert("请输入长度不超过" + maxlength + "的字符");
//}
}
function hidPictureCover() {
$(".setPictureCover").hide();
}
function openPictureCover() {
$(".setPictureCover").show();
}
function cutLYNCSmallImg() {
$("#cutSourceType").val("LyncSmallPicture");
$("#popTitlen").text("LYNC小图");
$(".blackCover").show();
if ($("#LYNCSmallImgNewNameAfterJcrop").val() == '') {
$("#btnCancelCut").hide();
}
else {
$("#btnCancelCut").show();
}
$(".blackCover").css("height", $(".mainRight").outerHeight());
if (jcrop_api) {
jcrop_api.destroy();
$("#popInnern_Content").html(appendHtmlStr)
}
var hRatio = 100 / height;
var wRatio = 150 / width;
var cwidth = $("#jcropDiv").width() * wRatio;
var cheight = $("#jcropDiv").height() * hRatio;
var positionX = ($("#jcropDiv").width() - $("#jcropDiv").width() * wRatio) / 2;
var positionY = ($("#jcropDiv").height() - $("#jcropDiv").height() * hRatio) / 2;
//初始化Jcrop
initJcrop(cwidth, cheight, positionX, positionY, 150, 100, 1);
}
function cutLYNCBigImg() {
$("#cutSourceType").val("LyncBigPicture");
$("#popTitlen").text("LYNC大图");
$(".blackCover").show();
if ($("#LyncBigPictureNewNameAfterJcrop").val() == '') {
$("#btnCancelCut").hide();
}
else {
$("#btnCancelCut").show();
}
$(".blackCover").css("height", $(".mainRight").outerHeight());
if (jcrop_api) {
jcrop_api.destroy();
$("#popInnern_Content").html(appendHtmlStr)
}
var hRatio = 150 / height;
var wRatio = 570 / width;
var cwidth = $("#jcropDiv").width() * wRatio;
var cheight = $("#jcropDiv").height() * hRatio;
var positionX = ($("#jcropDiv").width() - $("#jcropDiv").width() * wRatio) / 2;
var positionY = ($("#jcropDiv").height() - $("#jcropDiv").height() * hRatio) / 2;
//初始化Jcrop
initJcrop(cwidth, cheight, positionX, positionY, 570, 150, 2);
}
function cutNewsSmallImg() {
$("#cutSourceType").val("NewsSmallPicture");
$("#popTitlen").text("新闻小图");
$(".blackCover").show();
$(".blackCover").css("height", $(".mainRight").outerHeight());
if ($("#newsSmallImgNewNameAfterJcrop").val() == '') {
$("#btnCancelCut").hide();
}
else {
$("#btnCancelCut").show();
}
if (jcrop_api) {
jcrop_api.destroy();
$("#popInnern_Content").html(appendHtmlStr)
}
var hRatio = 270 / height;
var wRatio = 440 / width;
var cwidth = $("#jcropDiv").width() * wRatio;
var cheight = $("#jcropDiv").height() * hRatio;
var positionX = ($("#jcropDiv").width() - $("#jcropDiv").width() * wRatio) / 2;
var positionY = ($("#jcropDiv").height() - $("#jcropDiv").height() * hRatio) / 2;
//初始化Jcrop
initJcrop(cwidth, cheight, positionX, positionY, 440, 270, 3);
}
function cutNewsBigImg() {
$("#cutSourceType").val("NewsBigPicture");
$("#popTitlen").text("新闻大图");
$(".blackCover").show();
$(".blackCover").css("height", $(".mainRight").outerHeight());
if ($("#NewsBigPictureImgNewNameAfterJcrop").val() == '') {
$("#btnCancelCut").hide();
}
else {
$("#btnCancelCut").show();
}
if (jcrop_api) {
jcrop_api.destroy();
$("#popInnern_Content").html(appendHtmlStr)
}
var hRatio = 390 / height;
var wRatio = 1100 / width;
var cwidth = $("#jcropDiv").width() * wRatio;
var cheight = $("#jcropDiv").height() * hRatio;
var positionX = ($("#jcropDiv").width() - $("#jcropDiv").width() * wRatio) / 2;
var positionY = ($("#jcropDiv").height() - $("#jcropDiv").height() * hRatio) / 2;
//初始化Jcrop
initJcrop(cwidth, cheight, positionX, positionY, 1100, 390, 4);
}
//查询LYNC小图
function SearchLYNCSmallImg() {
if ($("#LYNCSmallImgNewNameAfterJcrop").val() == '') {
alert("请先设置LYNC小图");
}
else {
var src_image = $("#LYNCSmallImgNewNameAfterJcrop").val();
$("#imgNews").attr("src", "../Uploads/ShimaoNews/LyncSmallPicture/" + src_image + "");
openPopUps();
}
}
//查询LYNC大图
function SearchLYNCBigImg() {
if ($("#LyncBigPictureNewNameAfterJcrop").val() == '') {
alert("请先设置LYNC大图");
}
else {
var src_image = $("#LyncBigPictureNewNameAfterJcrop").val();
$("#imgNews").attr("src", "../Uploads/ShimaoNews/LyncBigPicture/" + src_image + "");
openPopUps();
}
}
//查询新闻小图
function SearchNewsSmallImg() {
if ($("#newsSmallImgNewNameAfterJcrop").val() == '') {
alert("请先设置新闻小图");
}
else {
//绑定点击查看已设置的图片
var src_image = $("#newsSmallImgNewNameAfterJcrop").val();
$("#imgNews").attr("src", "../Uploads/ShimaoNews/NewsSmallPicture/" + src_image + "");
openPopUps();
}
}
//查询新闻大图
function SearchNewsBigImg() {
if ($("#NewsBigPictureImgNewNameAfterJcrop").val() == '') {
alert("请先设置新闻大图");
}
else {
var src_image = $("#NewsBigPictureImgNewNameAfterJcrop").val();
$("#imgNews").attr("src", "../Uploads/ShimaoNews/NewsBigPicture/" + src_image + "");
openPopUps();
}
}
function AutoResizeImage(maxWidth, maxHeight, objImg) {
var img = new Image();
img.src = objImg.attr("src");
var hRatio;
var wRatio;
var Ratio = 1;
width = img.width;
height = img.height;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth == 0 && maxHeight == 0) {
Ratio = 1;
} else if (maxWidth == 0) {//
if (hRatio < 1) Ratio = hRatio;
} else if (maxHeight == 0) {
if (wRatio < 1) Ratio = wRatio;
} else if (wRatio < 1 || hRatio < 1) {
Ratio = (wRatio <= hRatio ? wRatio : hRatio);
}
if (Ratio < 1) {
w = w * Ratio;
h = h * Ratio;
}
ratio = Ratio;
objImg.width(w);
objImg.height(h);
sourceWidth = w, sourceHeight = h;
$("#jcropDiv").width(w - 4);
$("#jcropDiv").height(h - 4);
$("#popInnern").width(w + 4);
$("#popInnern").height(h + 4);
$("#jcropDiv").css("margin-left", (maxWidth - w) / 2)
}
//取消按钮事件
function btn_Cancel() {
var Context = ue.getContent();
var AbstractContent = $("#description").val();
if (Context == "" && AbstractContent == "") {
window.close();
}
else {
$("#Cancel001").html("您确认放弃当前的输入内容吗?");
openPopUp001();
}
}
//确定放弃当前输入内容
function Shure001() {
window.close();
}