spng.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. 
  2. //定义全局
  3. var spng = window.NameSpace || {};
  4. //1.美化radio单选按钮 sp-radio-default
  5. app.directive("spRadioDefault", function () {
  6. return {
  7. restrict: "A",
  8. link: function (scope, elem, attrs) {
  9. //灰色radio
  10. spng.funRadio(elem, "rd-default", "rd-default-checked");
  11. }
  12. }
  13. });
  14. //sp-radio-orange
  15. app.directive("spRadioOrange", function () {
  16. return {
  17. restrict: "A",
  18. link: function (scope, elem, attrs) {
  19. //橙色radio
  20. spng.funRadio(elem, "rd-orange", "rd-orange-checked");
  21. }
  22. }
  23. });
  24. //sp-radio-blue
  25. app.directive("spRadioBlue", function () {
  26. return {
  27. restrict: "A",
  28. link: function (scope, elem, attrs) {
  29. //蓝色radio
  30. spng.funRadio(elem, "rd-blue", "rd-blue-checked");
  31. }
  32. }
  33. });
  34. //sp-radio-green
  35. app.directive("spRadioGreen", function () {
  36. return {
  37. restrict: "A",
  38. link: function (scope, elem, attrs) {
  39. //绿色radio
  40. spng.funRadio(elem, "rd-green", "rd-green-checked");
  41. }
  42. }
  43. });
  44. //操作radio
  45. spng.funRadio = function (radioDiv, radioClass, radioCheckClass) {
  46. //三个参数
  47. $(radioDiv).each(function () {
  48. $("input[type='radio']", this).hide();
  49. $("input[type='radio']", this).parent("label").addClass(radioClass);
  50. $("input[type='radio']", this).click(function () {
  51. $(this).attr("checked", true).parent().siblings().find("input[type='radio']").removeAttr("checked");
  52. $(this).parent("label").addClass(radioCheckClass).siblings("label").removeClass(radioCheckClass);
  53. });
  54. $("input[type='radio']", this).each(function () {
  55. if ($(this).attr("checked")) {
  56. $(this).attr("checked", true).parent("label").addClass(radioCheckClass).siblings().removeClass(radioCheckClass);
  57. }
  58. });
  59. });
  60. }
  61. //2.美化checkbox单选按钮 sp-checkbox-gray
  62. app.directive("spCheckboxGray", function () {
  63. return {
  64. restrict: "A",
  65. link: function (scope, elem, attrs) {
  66. spng.funcheckbox(elem, "chb-gray", "chb-gray-checked"); //灰色checkbox
  67. }
  68. }
  69. });
  70. //2.美化checkbox单选按钮 sp-checkbox-blue
  71. app.directive("spCheckboxBlue", function () {
  72. return {
  73. restrict: "A",
  74. link: function (scope, elem, attrs) {
  75. spng.funcheckbox(elem, "chb-blue", "chb-blue-checked"); //蓝色checkbox
  76. }
  77. }
  78. });
  79. //3.美化checkbox单选按钮 sp-checkbox-orange
  80. app.directive("spCheckboxOrange", function () {
  81. return {
  82. restrict: "A",
  83. link: function (scope, elem, attrs) {
  84. spng.funcheckbox(elem, "chb-orange", "chb-orange-checked");//橙色checkbox
  85. }
  86. }
  87. });
  88. //4.美化checkbox单选按钮 sp-checkbox-green
  89. app.directive("spCheckboxGreen", function () {
  90. return {
  91. restrict: "A",
  92. link: function (scope, elem, attrs) {
  93. spng.funcheckbox(elem, "chb-green", "chb-green-checked");//绿色checkbox
  94. }
  95. }
  96. });
  97. //操作checkbox
  98. spng.funcheckbox = function (chbDiv, chbClass, chbchecked) {
  99. $(chbDiv).each(function () {
  100. $("input[type='checkbox']", this).hide();
  101. $("input[type='checkbox']", this).parent("label").addClass(chbClass);
  102. $("input[type='checkbox']", this).click(function () {
  103. if ($(this).parent("label").hasClass(chbchecked)) {
  104. $(this).removeAttr("checked").parent("label").removeClass(chbchecked);
  105. } else {
  106. $(this).attr("checked", true).parent("label").addClass(chbchecked);
  107. }
  108. });
  109. $("input[type='checkbox']", this).each(function () {
  110. if ($(this).attr("checked")) {
  111. $(this).attr("checked", true).parent("label").addClass(chbchecked);
  112. }
  113. });
  114. if ($(this).children(".sp-checkbox-all").length = 1) {
  115. $(".sp-checkbox-all", this).click(function () {
  116. if ($(this).hasClass("hasClick")) {
  117. $(this).removeClass("hasClick");
  118. $(this).parent().parent().find("input[type='checkbox']").removeAttr("checked");
  119. $(this).parent().parent().find("label").removeClass(chbchecked);
  120. } else {
  121. $(this).addClass("hasClick");
  122. $(this).parent().parent().find("input[type='checkbox']").attr("checked", true);
  123. $(this).parent().parent().find("label").addClass(chbchecked);
  124. }
  125. });
  126. }
  127. });
  128. }
  129. //3.上传图片 sp-upload-img
  130. app.directive("spUploadImg", function () {
  131. return {
  132. restrict: 'AE',
  133. scope: false,
  134. link: function (scope, elem, attrs) {
  135. var getimgwidth = elem.prev(".sp-upload-img").outerWidth();
  136. elem.css({ width: getimgwidth });
  137. elem.bind('click', function () {
  138. $(this).val('');
  139. });
  140. elem.change(function () {
  141. var file = this.files[0];
  142. if (file.size > 5242880) {
  143. alert("图片大小不能大于5M");
  144. file = null;
  145. return false;
  146. }
  147. var fileName = file.name;
  148. var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  149. if (postfix != "jpg" && postfix != "png" && postfix != "jpeg" && postfix != "gif") {
  150. alert("图片仅支持png、jpg、jpeg、gif类型的文件");
  151. fileName = "";
  152. file = null;
  153. return false;
  154. }
  155. var fileUrl = $(this).val();
  156. $(this).parent().children(".sp-upload-img").attr("data-url", fileUrl);
  157. var getimg = $(this).parent().children(".sp-upload-img");
  158. var filereader = new FileReader();
  159. filereader.readAsDataURL(file);
  160. $(filereader).load(function () {
  161. getimg.attr("src", this.result);
  162. });
  163. });
  164. }
  165. }
  166. });
  167. ////3.上传图片 sp-upload-img
  168. //app.directive("spUploadImg", function () {
  169. // return {
  170. // restrict: 'AE',
  171. // scope: false,
  172. // link: function (scope, elem, attrs) {
  173. // elem.bind('click', function () {
  174. // $(this).val('');
  175. // });
  176. // elem.change(function () {
  177. // var file = this.files[0];
  178. // console.log(file);
  179. // if (file.size > 5242880) {
  180. // alert("图片大小不能大于5M");
  181. // file = null;
  182. // return false;
  183. // }
  184. // var fileName = file.name;
  185. // var postfix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  186. // if (postfix != "jpg" && postfix != "png" && postfix != "jpeg" && postfix != "gif") {
  187. // alert("图片仅支持png、jpg、jpeg、gif类型的文件");
  188. // fileName = "";
  189. // file = null;
  190. // return false;
  191. // }
  192. // var fileUrl = $(this).val();
  193. // $(this).parent().children(".sp-upload-img").attr("data-url", fileUrl);
  194. // var getimg = $(this).parent().children(".sp-upload-img");
  195. // var filereader = new FileReader();
  196. // filereader.readAsDataURL(file);
  197. // $(filereader).load(function () {
  198. // //getimg.attr("src", this.result);
  199. // //uploadPic(file.name, this.result);
  200. // /////post给另外一个服务器生成一个文件地址,再返回
  201. // var picBase = this.result;
  202. // var picName = file.name;
  203. // var str = "base64,"
  204. // var begin = picBase.indexOf(str) + str.length;
  205. // picBase = picBase.substring(begin, picBase.length);
  206. // var host = 'http://218.69.24.10:8069';
  207. // var data = {
  208. // "filename": picName,
  209. // "filedata": picBase
  210. // }
  211. // //console.log(data);
  212. // $.ajax({
  213. // type: 'POST',
  214. // url: host + '/GoldFile/image/upload',
  215. // contentType: "application/json",
  216. // data: JSON.stringify(data),
  217. // dataType: "json",
  218. // success: function (res) {
  219. // if (res.success) {
  220. // //sp.dialog("图片上传成功");
  221. // //$("#eventIMG").attr("src", res.url);
  222. // getimg.attr("src", res.url);
  223. // }
  224. // }
  225. // });
  226. // });
  227. // });
  228. // }
  229. // }
  230. //});
  231. //4.上传文件 sp-upload-file
  232. app.directive("spUploadFile", function () {
  233. return {
  234. restrict: 'AE',
  235. scope: false,
  236. link: function (scope, elem, attrs) {
  237. elem.change(function () {
  238. if ($(this).parent().children(".sp-upload-url").length > 0) {
  239. var file = this.files[0];
  240. if (file.size > 52428800) {
  241. alert("文件大小不能大于50M");
  242. file = null;
  243. return false;
  244. } else {
  245. var fileUrl = $(this).val();
  246. $(this).parent().children(".sp-upload-url").val(fileUrl);
  247. }
  248. } else if ($(this).parent().children(".sp-upload-btn-single").length > 0) {
  249. var file = this.files[0];
  250. if (file.size > 52428800) {
  251. alert("文件大小不能大于50M");
  252. file = null;
  253. return false;
  254. } else {
  255. var fileUrl = $(this).val();
  256. var urlArr = fileUrl.split("\\");
  257. var getName = urlArr[urlArr.length - 1];//截取路径并获取文件的名字
  258. var setdiv = "<div class='sp-lh-20'>" + getName + "</div>";
  259. $(this).parent().children(".sp-upload-tip").show(200).append(setdiv);
  260. }
  261. }
  262. });
  263. }
  264. }
  265. });
  266. /**
  267. * 使用示例 解决laydate与angularjs一起使用时的冲突
  268. * <input class="laydate-logo" theme="#009f95" sp-laydate type="text" id="id1" ng-model="startTime"/>
  269. */
  270. /**
  271. * laydate指令
  272. * 具体参数可以查看API:http://www.layui.com/doc/modules/laydate.html
  273. * class="laydate-bg" 为input 添加背景
  274. * <input class="laydate-logo" sp-laydate type="text" laytype="month" lang="en" btns="now-confirm" theme="#009f95" id="id1" ng-model="startTime" />
  275. * 墨绿色:#009f95
  276. * lang 默认: cn; en-英文 ,cn-中文
  277. * btns 底部按钮 默认:['clear', 'now', 'confirm'], (清除, 现在, 确认); 需要设置时 中间用'-'隔开
  278. * min 可选时间最小值 , 默认'1900-1-1'
  279. * max 可选时间最大值, 默认'2099-12-31'
  280. * laytype 选项中可选择的日期类型
  281. type可选值 名称 用途
  282. year 年选择器 只提供年列表选择
  283. month 年月选择器 只提供年、月选择
  284. date 日期选择器 可选择:年、月、日。type默认值,一般可不填
  285. time 时间选择器 只提供时、分、秒选择
  286. datetime 日期时间选择器 可选择:年、月、日、时、分、秒
  287. *
  288. * format 自定义格式 默认 'yyyy-MM-dd'
  289. *
  290. yyyy 年份,至少四位数。如果不足四位,则前面补零
  291. y 年份,不限制位数,即不管年份多少位,前面均不补零
  292. MM 月份,至少两位数。如果不足两位,则前面补零。
  293. M 月份,允许一位数。
  294. dd 日期,至少两位数。如果不足两位,则前面补零。
  295. d 日期,允许一位数。
  296. HH 小时,至少两位数。如果不足两位,则前面补零。
  297. H 小时,允许一位数。
  298. mm 分钟,至少两位数。如果不足两位,则前面补零。
  299. m 分钟,允许一位数。
  300. ss 秒数,至少两位数。如果不足两位,则前面补零。
  301. s 秒数,允许一位数。
  302. * 例如:
  303. * <input type="text" class="laydate-logo" theme="#009f95" sp-laydate laytype="datetime" format="yyyy年MM月dd日 HH时mm分ss秒" id="id1" ng-model="startTime" />
  304. *
  305. * range 开启左右面板范围选择
  306. * range: true 或 range: '~' 来自定义分割字符
  307. *<input sp-laydate type="text" range="~" id="id1" ng-model="startTime" />
  308. *<input type="text" class="sp-input laydate-logo" id="laydate_BEGINDATE" theme="#009f95" sp-laydate laytype="month" format="yyyy-MM" />
  309. *<input type="text" class="sp-input laydate-logo" theme="#009f95" sp-laydate id="id1" ng-model="startTime"/>
  310. */
  311. app.directive('spLaydate', function () {
  312. return {
  313. require: '?ngModel',
  314. restrict: 'A',
  315. scope: {
  316. ngModel: '=',
  317. },
  318. link: function (scope, element, attr, ngModel) {
  319. var _date = null, _config = {};
  320. _config = {
  321. lang: !!attr.lang ? attr.lang : 'cn',
  322. elem: element[0],
  323. btns: !!attr.btns ? attr.btns.split('-') : ['clear', 'now', 'confirm'],
  324. min: attr.min || '1900-1-1',
  325. max: attr.max || '2099-12-31',
  326. format: attr.format || 'yyyy-MM-dd',
  327. type: attr.laytype || 'date',
  328. trigger: 'click',
  329. range: attr.range,
  330. theme: attr.theme || 'default',
  331. position: 'fixed',
  332. done: function (value, date, endDate) {
  333. ngModel.$setViewValue(value);
  334. }
  335. };
  336. // 初始化
  337. _date = laydate.render(_config);
  338. // 模型值同步到视图上
  339. ngModel.$render = function () {
  340. element.val(ngModel.$viewValue || '');
  341. };
  342. }
  343. }
  344. });
  345. ////更多按钮的点击设置,zyg,201804241106
  346. //<span id="spBtnSolid" class="sp-btn-solid sp-btn-sm sp-radius" title="更多操作" sp-btn-solid><i class="icon icon-dot-3">&#xe982;</i></span>
  347. // <div class="sp-btn-solid-part" style="margin-left:50px; display:none;">
  348. // <button ng-click="ctl.btnCancel(item.ID)" class="sp-btn-transparent" ng-disabled="'{{item.TASKSTATUS}}'!='已下发'" ng-class="{'sp-cursor-allowed':'{{item.TASKSTATUS}}'!='已下发'}"><i class="fa fa-trash sp-color-red"></i>&nbsp;取消下发</button>
  349. // </div>
  350. //////sp-btn-solid
  351. app.directive("spBtnSolid", function () {
  352. return {
  353. restrict: "A",
  354. link: function (scope, elem, attrs) {
  355. $(elem).click(function (e) {
  356. $(".sp-btn-solid-part").hide();
  357. $(elem).next(".sp-btn-solid-part").show();
  358. sp.stopPropagation(e);
  359. });
  360. $(document).bind('click', function () {
  361. $(".sp-btn-solid-part").hide();
  362. });
  363. $(".sp-btn-solid-part").click(function (e) {
  364. sp.stopPropagation(e);
  365. });
  366. }
  367. }
  368. });
  369. //20170810 sp-grid-sort 表格点击表头排序
  370. app.directive("spGridSort", function () {
  371. return {
  372. restrict: "A",
  373. link: function (scope, elem, attrs) {
  374. $("thead tr th.sp-sort-th", elem).each(function () {
  375. if ($(this).children(".grid-th-span").length < 1) {
  376. $(this).append("<span class=\"grid-th-span\"><i class=\"fa fa-sort\"></i></span>");
  377. }
  378. });
  379. }
  380. }
  381. });