function.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //设置全局AJAX默认选项。
  2. $.ajaxSetup({cache : false});
  3. //jQuery常用函数封装
  4. (function ($) {
  5. //删除
  6. $.fn.isDel = function (){
  7. return this.each(function() {
  8. $(this).click(function(){
  9. var url = $(this).attr("url");
  10. var css = this;
  11. parent.layer.confirm('确认要删除本资源?',{icon: 3,title:'友情提示'}, function(index){
  12. $.getJSON(url,function(data) {
  13. parent.layer.close(index);
  14. if (data.code == 200) {
  15. $(css).parents("tr").remove();
  16. $(css).parent().remove();
  17. $.isEmptyObject(data.url) ? '' : window.location.replace(data.url);
  18. }else{
  19. parent.layer.msg(data.msg,{icon:5,shade:0.5})
  20. }
  21. });
  22. });
  23. })
  24. });
  25. };
  26. //点击URL请求
  27. $.fn.actUrl = function (options) {
  28. $(this).click(function () {
  29. $(this).requestUrl(options);
  30. })
  31. };
  32. //表单改变值请求
  33. $.fn.changeUrl = function (options) {
  34. var defaults = {
  35. ispost: true,
  36. confirm: false,
  37. }
  38. var options = $.extend(defaults, options);
  39. $(this).change(function () {
  40. if ($.isEmptyObject(options.param)) {
  41. options.param = {
  42. id : $(this).attr('id'),
  43. sort: $(this).val(),
  44. }
  45. }
  46. $(this).requestUrl(options);
  47. })
  48. };
  49. /**
  50. * 通过AJAX请求
  51. */
  52. $.fn.requestUrl = function (options) {
  53. var defaults = {
  54. confirm: true,
  55. parent:false,
  56. ispost: false,
  57. msg:'确认要操作当前资源?',
  58. }
  59. var options = $.extend(defaults, options);
  60. console.log('actUrl默认参数');
  61. console.log(options);
  62. return this.each(function () {
  63. if ((options.ispost && $.isEmptyObject(options.param)) || (options.ispost && $.isEmptyObject(options.url))) {
  64. console.log('%cJQuery的actUrl参数在{ispost:true}下必须设置param和url对象参数\n{\n ispost:true,\n param:{}\n url:"http://***"\n}',"color:red");
  65. return;
  66. }
  67. var param = $.isEmptyObject(options.param) ? {} : options.param
  68. var url = $.isEmptyObject(options.url) ? $(this).attr("url") : options.url;
  69. var load = 2
  70. var callfun = function (data) {
  71. parent.layer.close(load);
  72. if (data.code == 200) {
  73. if (options.parent) {
  74. $.isEmptyObject(data.url) ? window.parent.location.reload() : window.parent.location.replace(data.url);
  75. } else {
  76. $.isEmptyObject(data.url) ? window.location.reload() : window.location.replace(data.url);
  77. }
  78. }else{
  79. parent.layer.msg(data.msg,{icon:5,shade:0.5})
  80. }
  81. }
  82. if (options.confirm) {
  83. parent.layer.confirm(options.msg,{ icon: 3, title: '友情提示' },function (index) {
  84. parent.layer.close(index);
  85. load = parent.layer.load(0, { shade: [0.3, '#393D49'], time: 3000 });
  86. if (options.ispost) {
  87. $.post(url,param,callfun)
  88. } else {
  89. $.getJSON(url,callfun,"json");
  90. }
  91. });
  92. } else {
  93. if (options.ispost) {
  94. $.post(url,param,callfun)
  95. } else {
  96. $.getJSON(url,callfun,"json");
  97. }
  98. }
  99. });
  100. }
  101. //弹出窗口
  102. $.fn.win = function (options) {
  103. $(this).click(function () {
  104. $(this).popup(options);
  105. })
  106. };
  107. $.fn.popup = function (options){
  108. var defaults = {
  109. url: $(this).attr("url"),
  110. input: $(this).attr('data') ? $(this).attr('data') : $(this).attr('id'),
  111. area: ['60%', '70%'],
  112. title:'快捷窗口',
  113. reload: 0
  114. }, options = $.extend(defaults, options);
  115. console.log('popup默认参数');
  116. console.log(options);
  117. return this.each(function () {
  118. options.input != void 0 && (options.url = options.url + '?input=' + options.input);
  119. parent.layer.open({
  120. type: 2, title: options.title,area: options.area, content: options.url, success: function (layero,index) {
  121. parent.layer.getChildFrame('body', index).addClass(window.name);
  122. }, end: function () {
  123. 1 == options.reload && parent.$("#" + window.name)[0].contentWindow.location.reload();
  124. }
  125. });
  126. });
  127. };
  128. //表单
  129. $.fn.isForm = function (options){
  130. var defaults = {types: 0,iframe:0,upload:''}
  131. var options = $.extend(defaults, options);
  132. console.log('isForm默认参数');
  133. console.log(options);
  134. return this.each(function (){
  135. $(this).validatorForm(options); //表单处理
  136. $(".ui-editor").length > 0 && $(this).find(".ui-editor").editor(options.upload);
  137. $(".ui-editor").length > 0 && $(this).find('.ui-editor').editor(options.upload)//编辑器
  138. $(".ui-mieditor").length > 0 && $(this).find('.ui-mieditor').minieditor(options.upload)//编辑器
  139. $(".ui-date").length > 0 && $(this).find('.ui-date').layday() //时间选择器
  140. $(".ui-time").length > 0 && $(this).find('.ui-time').laytime() //时间选择器
  141. $(".ui-color").length > 0 && $(this).find('.ui-color').color() //颜色选择器
  142. $(".ui-upload").length > 0 && $(".ui-upload").click(function () {$(this).popup({url:options.upload})}) //上传附件
  143. });
  144. };
  145. //表单验证
  146. $.fn.validatorForm = function (options){
  147. return this.each(function () {
  148. var win = parent.layer.getFrameIndex(window.name);
  149. if (options.types == 0) {
  150. $(this).Validform({
  151. btnSubmit: ".submit",showAllError:false,tiptype:3,ajaxPost: true,postonce:true,ignoreHidden:true,beforeSubmit:function(){
  152. parent.layer.load(0,{shade:[0.2,'#000'],time:1500});
  153. }, callback: function (data) {
  154. if (data.code == "200") {
  155. parent.layer.alert(data.msg, {icon: 1, closeBtn: 0 },function(index){
  156. if(typeof data.parent != "undefined"){
  157. $.isEmptyObject(data.url) ? window.parent.location.reload(): window.parent.location.replace(data.url);
  158. }else{
  159. $.isEmptyObject(data.url) ? window.location.reload(): window.location.replace(data.url);
  160. }
  161. parent.layer.close(index);
  162. parent.layer.close(win);
  163. });
  164. }else if(data.code == "302"){
  165. if(typeof data.parent != "undefined"){
  166. $.isEmptyObject(data.url) ? window.parent.location.reload(): window.parent.location.replace(data.url);
  167. }else{
  168. $.isEmptyObject(data.url) ? window.location.reload(): window.location.replace(data.url);
  169. }
  170. parent.layer.close(win);
  171. } else {
  172. parent.layer.alert(data.msg,{icon:5,closeBtn:0});
  173. }
  174. }
  175. })
  176. }else{
  177. $(this).Validform({btnSubmit: ".submit",showAllError:false,postonce:true,tiptype:function(msg,o){if(o.type ==3){layer.tips(msg,'.submit')}},ignoreHidden:true,beforeSubmit:function(){
  178. parent.layer.load(0,{shade:[0.2,'#000'],time:1500});
  179. }})
  180. }
  181. });
  182. };
  183. //颜色
  184. $.fn.color = function (){
  185. return this.each(function (){
  186. $(this).soColorPacker();
  187. });
  188. };
  189. //时间插件不带时间
  190. $.fn.layday = function () {
  191. this.each(function () {
  192. laydate.render({elem:this});
  193. });
  194. };
  195. //时间插件带时间
  196. $.fn.laytime = function () {
  197. this.each(function () {
  198. laydate.render({elem:this,'type':'datetime'});
  199. });
  200. };
  201. //编辑器调用
  202. $.fn.editor = function (uploads){
  203. return this.each(function (){
  204. var editorConfig = {
  205. allowFileManager: false, uploadJson: uploads, urlType: "domain",width: '100%',height:'450px',themeType : 'simple',
  206. items : ['fontname','fontsize','|','forecolor','hilitecolor','bold', 'italic','underline','removeformat','|','justifyleft','justifycenter','justifyright','|', 'image', 'multiimage','media','table','|','link','unlink','|','clearhtml','|','source','fullscreen'],
  207. afterBlur: function () {this.sync()}
  208. };
  209. KindEditor.create(this,editorConfig);
  210. });
  211. };
  212. //编辑器调用
  213. $.fn.minieditor = function (uploads){
  214. return this.each(function (){
  215. var editorConfig = {
  216. allowFileManager : false,uploadJson:uploads,urlType: "domain",width: '100%',height:'250px',themeType : 'simple',
  217. items : ['fontname','fontsize','|','forecolor','hilitecolor','bold', 'italic','underline','removeformat','|', 'justifyleft','justifycenter','justifyright','|','image','link','unlink','|','source','fullscreen'],
  218. afterBlur: function () {this.sync()}
  219. };
  220. KindEditor.create(this,editorConfig);
  221. });
  222. };
  223. })(jQuery);