simple-plugin.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. *此框架只依赖zepto.js
  3. *sDialog 是dialog弹出框
  4. */
  5. (function($) {
  6. $.extend($, {
  7. sDialog: function(options) {
  8. //dialog弹出框
  9. var opts = $.extend({}, $.sDialog.defaults, options);
  10. function _init() {
  11. var mask_height = ($("body").height() > $(window).height()) ? $("body").height() : $(window).height();
  12. var windowH = parseInt($(window).height());
  13. var warpTop = windowH / 2;
  14. var dTmpl = '<div class="simple-dialog-wrapper">';
  15. if (opts.lock) { //是否有锁定
  16. dTmpl += '<div class="s-dialog-mask" style="height:' + mask_height + 'px;"></div>';
  17. }
  18. dTmpl += '<div style="left:50%;top:' + warpTop + 'px" class="s-dialog-wrapper s-dialog-skin-' + opts.skin + '">' + '<div class="s-dialog-content">' + opts.content + '</div>'
  19. if (opts.okBtn || opts.cancelBtn) {
  20. dTmpl += '<div class="s-dialog-btn-wapper">';
  21. if (opts.okBtn) {
  22. dTmpl += '<a href="javascript:void(0)" class="s-dialog-btn-ok">' + opts.okBtnText + '</a>';
  23. }
  24. if (opts.cancelBtn) {
  25. dTmpl += '<a href="javascript:void(0)" class="s-dialog-btn-cancel">' + opts.cancelBtnText + '</a>';
  26. }
  27. dTmpl += '</div>';
  28. }
  29. dTmpl += '</div>';
  30. dTmpl += '</div>';
  31. $("body").append(dTmpl);
  32. var d_wrapper = $(".s-dialog-wrapper");
  33. var mLeft = -parseInt(d_wrapper.width()) / 2;
  34. d_wrapper.css({
  35. "margin-left": mLeft,
  36. });
  37. //绑定事件
  38. _bind();
  39. }
  40. function _bind() {
  41. var okBtn = $(".s-dialog-btn-ok");
  42. var cancelBtn = $(".s-dialog-btn-cancel");
  43. okBtn.click(_okFn);
  44. cancelBtn.click(_cancelFn);
  45. if (!opts.okBtn && !opts.cancelBtn) {
  46. setTimeout(function() {
  47. _close();
  48. }, opts.autoTime);
  49. }
  50. }
  51. function _okFn() {
  52. opts.okFn();
  53. _close();
  54. }
  55. function _cancelFn() {
  56. opts.cancelFn();
  57. _close();
  58. }
  59. function _close() {
  60. $(".simple-dialog-wrapper").remove();
  61. }
  62. return this.each(function() {
  63. _init();
  64. })();
  65. },
  66. sValid: function() {
  67. var $this = $.sValid;
  68. var sElement = $this.settings.sElement;
  69. for (var i = 0; i < sElement.length; i++) {
  70. var element = sElement[i];
  71. var sEl = $("#"+element).length >0 ? $("#"+element) : $("."+element);
  72. for(var j = 0;j<sEl.length;j++){
  73. $this.check(element,sEl[j]);
  74. }
  75. }
  76. $this.callBackData();
  77. var cEid = $this.errorFiles.eId;
  78. var cEmsg = $this.errorFiles.eMsg;
  79. var cErules = $this.errorFiles.eRules;
  80. var isVlided = false;
  81. if (cEid.length > 0) {
  82. isVlided = false;
  83. } else {
  84. isVlided = true;
  85. }
  86. $this.settings.callback.apply(this, [cEid, cEmsg, cErules]);
  87. $this.destroyData();
  88. return isVlided;
  89. }
  90. });
  91. //sDialog
  92. $.sDialog.defaults = {
  93. autoTime: '2000', //当没有 确定和取消按钮的时候,弹出框自动关闭的时间
  94. "skin": 'block', //皮肤,默认黑色
  95. "content": "我是一个弹出框", //弹出框里面的内容
  96. "width": 100, //没用到
  97. "height": 100, //没用到
  98. "okBtn": true, //是否显示确定按钮
  99. "cancelBtn": true, //是否显示确定按钮
  100. "okBtnText": "确定", //确定按钮的文字
  101. "cancelBtnText": "取消", //取消按钮的文字
  102. "lock": true, //是否显示遮罩
  103. "okFn": function() {}, //点击确定按钮执行的函数
  104. "cancelFn": function() {} //点击取消按钮执行的函数
  105. };
  106. //sValid
  107. $.extend($.sValid, {
  108. defaults: {
  109. rules: {},
  110. messages: {},
  111. callback: function() {}
  112. },
  113. init: function(options) {
  114. //初始化控件参数
  115. var opt = $.extend({}, this.defaults, options);
  116. var rules = opt.rules;
  117. var messages = opt.messages;
  118. var sElement = [];
  119. $.map(rules, function(item, idx) {
  120. sElement.push(idx);
  121. });
  122. this.settings = {};
  123. this.settings["sElement"] = sElement;
  124. this.settings["sRules"] = rules;
  125. this.settings["sMessages"] = messages;
  126. this.settings['callback'] = opt.callback;
  127. },
  128. optional: function(element) {
  129. var val = this.elementValue(element);
  130. return !this.methods.required.call(this, val, element);
  131. },
  132. methods: {
  133. required: function(value, element) {
  134. if (element.nodeName.toLowerCase() === "select") {
  135. var val = $(element).val();
  136. return val && val.length > 0;
  137. }
  138. return $.trim(value).length > 0;
  139. },
  140. maxlength: function(value, element, param) {
  141. var length = $.trim(value).length;
  142. return this.optional(element) || length <= param;
  143. },
  144. minlength: function(value, element, param) {
  145. var length = $.trim(value).length;
  146. return this.optional(element) || length >= param;
  147. },
  148. //是否是合法数字(包括正数、负数)
  149. number: function(value, element, param) {
  150. return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
  151. },
  152. digits: function(value, element, param) {
  153. return this.optional(element) || /^\d+$/.test(value);
  154. },
  155. email: function(value, element, param) {
  156. return this.optional(element) || /^[a-z0-9-]{1,30}@[a-z0-9-]{1,65}(\.[a-z0-9-]{1,65})*$/.test(value);
  157. }
  158. },
  159. elementValue: function(element) {
  160. var type = $(element).attr("type");
  161. var value = $(element).val();
  162. if (typeof value === "string") {
  163. return value.replace(/\r/g, "");
  164. }
  165. return value;
  166. },
  167. rulesFormat: {
  168. required: true,
  169. email: true
  170. },
  171. errorFiles: {
  172. eId: [],
  173. eRules: {},
  174. eMsg: {}
  175. },
  176. check: function(element,mEl) {
  177. var settingsRules = [];
  178. var methods = this.methods;
  179. var rules = this.settings["sRules"];
  180. var mVal = this.elementValue.call(this, mEl);
  181. var mParam = "";
  182. var errorFiles = this.errorFiles;
  183. var errRules = [];
  184. //rules
  185. if (typeof rules[element] === "string") {
  186. if ($.inArray(rules[element], settingsRules) < 0) {
  187. settingsRules.push(rules[element]);
  188. }
  189. } else {
  190. $.each(rules[element], function(idx, item) {
  191. if ($.inArray(idx, settingsRules) < 0) {
  192. settingsRules.push(idx);
  193. if (idx == "maxlength" || idx == "minlength") {
  194. mParam = parseInt(item);
  195. }
  196. }
  197. })
  198. }
  199. //checked
  200. for (var i = 0; i < settingsRules.length; i++) {
  201. if (!methods[settingsRules[i]].call(this, mVal, mEl, mParam)) {
  202. errRules.push(settingsRules[i]);
  203. errorFiles['eRules'][element] = errRules;
  204. if ($.inArray(element, errorFiles['eId']) < 0) {
  205. errorFiles['eId'].push(element);
  206. }
  207. }
  208. }
  209. },
  210. callBackData: function() {
  211. var errorFiles = this.errorFiles;
  212. var errId = errorFiles.eId;
  213. var eMsg = errorFiles.eMsg;
  214. var eRules = errorFiles.eRules;
  215. var sMessages = this.settings.sMessages;
  216. for (var i = 0; i < errId.length; i++) {
  217. if (typeof sMessages[errId[i]] === "string") {
  218. eMsg[errId[i] + "_" + eRules[errId[i]]] = sMessages[errId[i]];
  219. } else {
  220. if ($.isArray(eRules[errId[i]])) {
  221. for (var j = 0; j < eRules[errId[i]].length; j++) {
  222. eMsg[errId[i] + "_" + eRules[errId[i]][j]] = sMessages[errId[i]][eRules[errId[i]][j]]
  223. }
  224. }
  225. }
  226. }
  227. },
  228. destroyData: function() {
  229. this.errorFiles = {
  230. eId: [],
  231. eRules: {},
  232. eMsg: {}
  233. };
  234. }
  235. });
  236. })(Zepto);