//定义全局 var spng = window.NameSpace || {}; //1.美化radio单选按钮 sp-radio-default app.directive("spRadioDefault", function () { return { restrict: "A", link: function (scope, elem, attrs) { //灰色radio spng.funRadio(elem, "rd-default", "rd-default-checked"); } } }); //sp-radio-orange app.directive("spRadioOrange", function () { return { restrict: "A", link: function (scope, elem, attrs) { //橙色radio spng.funRadio(elem, "rd-orange", "rd-orange-checked"); } } }); //sp-radio-blue app.directive("spRadioBlue", function () { return { restrict: "A", link: function (scope, elem, attrs) { //蓝色radio spng.funRadio(elem, "rd-blue", "rd-blue-checked"); } } }); //sp-radio-green app.directive("spRadioGreen", function () { return { restrict: "A", link: function (scope, elem, attrs) { //绿色radio spng.funRadio(elem, "rd-green", "rd-green-checked"); } } }); //操作radio spng.funRadio = function (radioDiv, radioClass, radioCheckClass) { //三个参数 $(radioDiv).each(function () { $("input[type='radio']", this).hide(); $("input[type='radio']", this).parent("label").addClass(radioClass); $("input[type='radio']", this).click(function () { $(this).attr("checked", true).parent().siblings().find("input[type='radio']").removeAttr("checked"); $(this).parent("label").addClass(radioCheckClass).siblings("label").removeClass(radioCheckClass); }); $("input[type='radio']", this).each(function () { if ($(this).attr("checked")) { $(this).attr("checked", true).parent("label").addClass(radioCheckClass).siblings().removeClass(radioCheckClass); } }); }); } //2.美化checkbox单选按钮 sp-checkbox-gray app.directive("spCheckboxGray", function () { return { restrict: "A", link: function (scope, elem, attrs) { spng.funcheckbox(elem, "chb-gray", "chb-gray-checked"); //灰色checkbox } } }); //2.美化checkbox单选按钮 sp-checkbox-blue app.directive("spCheckboxBlue", function () { return { restrict: "A", link: function (scope, elem, attrs) { spng.funcheckbox(elem, "chb-blue", "chb-blue-checked"); //蓝色checkbox } } }); //3.美化checkbox单选按钮 sp-checkbox-orange app.directive("spCheckboxOrange", function () { return { restrict: "A", link: function (scope, elem, attrs) { spng.funcheckbox(elem, "chb-orange", "chb-orange-checked");//橙色checkbox } } }); //4.美化checkbox单选按钮 sp-checkbox-green app.directive("spCheckboxGreen", function () { return { restrict: "A", link: function (scope, elem, attrs) { spng.funcheckbox(elem, "chb-green", "chb-green-checked");//绿色checkbox } } }); //操作checkbox spng.funcheckbox = function (chbDiv, chbClass, chbchecked) { $(chbDiv).each(function () { $("input[type='checkbox']", this).hide(); $("input[type='checkbox']", this).parent("label").addClass(chbClass); $("input[type='checkbox']", this).click(function () { if ($(this).parent("label").hasClass(chbchecked)) { $(this).removeAttr("checked").parent("label").removeClass(chbchecked); } else { $(this).attr("checked", true).parent("label").addClass(chbchecked); } }); $("input[type='checkbox']", this).each(function () { if ($(this).attr("checked")) { $(this).attr("checked", true).parent("label").addClass(chbchecked); } }); if ($(this).children(".sp-checkbox-all").length = 1) { $(".sp-checkbox-all", this).click(function () { if ($(this).hasClass("hasClick")) { $(this).removeClass("hasClick"); $(this).parent().parent().find("input[type='checkbox']").removeAttr("checked"); $(this).parent().parent().find("label").removeClass(chbchecked); } else { $(this).addClass("hasClick"); $(this).parent().parent().find("input[type='checkbox']").attr("checked", true); $(this).parent().parent().find("label").addClass(chbchecked); } }); } }); } //3.上传图片 sp-upload-img app.directive("spUploadImg", function () { return { restrict: 'AE', scope: false, link: function (scope, elem, attrs) { var getimgwidth = elem.prev(".sp-upload-img").outerWidth(); elem.css({ width: getimgwidth }); elem.bind('click', function () { $(this).val(''); }); elem.change(function () { var file = this.files[0]; if (file.size > 5242880) { alert("图片大小不能大于5M"); file = null; return false; } var fileName = file.name; var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (postfix != "jpg" && postfix != "png" && postfix != "jpeg" && postfix != "gif") { alert("图片仅支持png、jpg、jpeg、gif类型的文件"); fileName = ""; file = null; return false; } var fileUrl = $(this).val(); $(this).parent().children(".sp-upload-img").attr("data-url", fileUrl); var getimg = $(this).parent().children(".sp-upload-img"); var filereader = new FileReader(); filereader.readAsDataURL(file); $(filereader).load(function () { getimg.attr("src", this.result); }); }); } } }); ////3.上传图片 sp-upload-img //app.directive("spUploadImg", function () { // return { // restrict: 'AE', // scope: false, // link: function (scope, elem, attrs) { // elem.bind('click', function () { // $(this).val(''); // }); // elem.change(function () { // var file = this.files[0]; // console.log(file); // if (file.size > 5242880) { // alert("图片大小不能大于5M"); // file = null; // return false; // } // var fileName = file.name; // var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); // if (postfix != "jpg" && postfix != "png" && postfix != "jpeg" && postfix != "gif") { // alert("图片仅支持png、jpg、jpeg、gif类型的文件"); // fileName = ""; // file = null; // return false; // } // var fileUrl = $(this).val(); // $(this).parent().children(".sp-upload-img").attr("data-url", fileUrl); // var getimg = $(this).parent().children(".sp-upload-img"); // var filereader = new FileReader(); // filereader.readAsDataURL(file); // $(filereader).load(function () { // //getimg.attr("src", this.result); // //uploadPic(file.name, this.result); // /////post给另外一个服务器生成一个文件地址,再返回 // var picBase = this.result; // var picName = file.name; // var str = "base64," // var begin = picBase.indexOf(str) + str.length; // picBase = picBase.substring(begin, picBase.length); // var host = 'http://218.69.24.10:8069'; // var data = { // "filename": picName, // "filedata": picBase // } // //console.log(data); // $.ajax({ // type: 'POST', // url: host + '/GoldFile/image/upload', // contentType: "application/json", // data: JSON.stringify(data), // dataType: "json", // success: function (res) { // if (res.success) { // //sp.dialog("图片上传成功"); // //$("#eventIMG").attr("src", res.url); // getimg.attr("src", res.url); // } // } // }); // }); // }); // } // } //}); //4.上传文件 sp-upload-file app.directive("spUploadFile", function () { return { restrict: 'AE', scope: false, link: function (scope, elem, attrs) { elem.change(function () { if ($(this).parent().children(".sp-upload-url").length > 0) { var file = this.files[0]; if (file.size > 52428800) { alert("文件大小不能大于50M"); file = null; return false; } else { var fileUrl = $(this).val(); $(this).parent().children(".sp-upload-url").val(fileUrl); } } else if ($(this).parent().children(".sp-upload-btn-single").length > 0) { var file = this.files[0]; if (file.size > 52428800) { alert("文件大小不能大于50M"); file = null; return false; } else { var fileUrl = $(this).val(); var urlArr = fileUrl.split("\\"); var getName = urlArr[urlArr.length - 1];//截取路径并获取文件的名字 var setdiv = "