step.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * @Name: step 基于layui的步骤条面板
  3. * @Author: 李祥
  4. * @License:MIT
  5. * 最近修改时间: 2018/11/16
  6. */
  7. layui.define(["jquery"], function (exports) {
  8. var $ = layui.jquery;
  9. function Step(option) {
  10. this.option=option; // 获取传入的数据
  11. this.elem=option.elem;
  12. // this.methods=option.methods?option.methods:"";
  13. this.title=option.title?option.title:[];
  14. this.description=option.description?option.description:[];
  15. this.canIconClick=option.canIconClick?option.canIconClick:false;
  16. this.isOpenStepLevel=option.isOpenStepLevel?option.isOpenStepLevel:false;
  17. this.len=0; // 页面个数
  18. this.currentStep=(option.currentStep && option.currentStep>=1)?option.currentStep:1; // 当前走到第几步
  19. this.disabledStep=Object.prototype.toString.call(option.disabledStep)==="[object Array]"?option.disabledStep:[];
  20. this.styleLine=$(option.elem).hasClass("layui-step-line");
  21. this.finalStep=1; // 当前走到最远的步骤
  22. this.parameterInit();
  23. this.domRender();
  24. this.init();
  25. this.openStepLevel();
  26. this.changeStep();
  27. }
  28. Step.prototype={
  29. constructor: Step,
  30. // 初始化参数数据
  31. parameterInit: function() {
  32. var self=this;
  33. this.len=$(this.elem).find(".layui-step-content-item").length; // 页面个数
  34. // 不传title参数
  35. if(this.title.length<=0){
  36. $(this.elem).find(".layui-step-content-item").each(function(index,val) {
  37. self.title.push("第"+(index+1)+"步");
  38. })
  39. }
  40. if(this.len!==this.title.length){
  41. throw "title参数长度与页面长度不匹配";
  42. }
  43. // 不传description参数
  44. if(this.description.length<=0){
  45. $(this.elem).find(".layui-step-content-item").each(function(index,val) {
  46. self.description.push("");
  47. })
  48. }
  49. if(this.len!==this.description.length){
  50. throw "description参数长度与页面长度不匹配";
  51. }
  52. // 若当前步超过最大步,则默认为最后一步
  53. this.currentStep=this.currentStep>=this.len?this.len:this.currentStep;
  54. },
  55. domRender: function() {
  56. var self=this;
  57. var titleStr='<div class="layui-step-title layui-clear">'+
  58. '<div class="layui-step-title-item step-first"'
  59. if(this.styleLine){
  60. titleStr+=' style="width: calc('+(100/this.len)+'% - 50px);margin-right: 50px;">'
  61. }else{
  62. titleStr+=' style="width: '+(100/this.len)+'%;">'
  63. }
  64. titleStr+='<div class="step-icon">'+
  65. '<i>1</i>'+
  66. '</div>'+
  67. '<div class="step-text">'+
  68. this.title[0]+
  69. '</div>'+
  70. '<div class="step-description">'+
  71. this.description[0]+
  72. '</div>'+
  73. '</div>';
  74. for(var i=1;i<this.title.length-1;i++){
  75. titleStr+='<div class="layui-step-title-item"';
  76. if(this.styleLine){
  77. titleStr+=' style="width: calc('+(100/this.len)+'% - 50px);margin-right: 50px;">'
  78. }else{
  79. titleStr+=' style="width: '+(100/this.len)+'%;">'
  80. }
  81. titleStr+='<div class="step-icon">'+
  82. '<i>'+(i+1)+'</i>'+
  83. '</div>'+
  84. '<div class="step-text">'+
  85. this.title[i]+
  86. '</div>'+
  87. '<div class="step-description">'+
  88. this.description[i]+
  89. '</div>'+
  90. '</div>';
  91. };
  92. titleStr+='<div class="layui-step-title-item step-last" style="width: '+(100/this.len)+'%;">'+
  93. '<div class="step-icon">'+
  94. '<i>'+this.len+'</i>'+
  95. '</div>'+
  96. '<div class="step-text">'+
  97. this.title[this.title.length-1]+
  98. '</div>'+
  99. '<div class="step-description">'+
  100. this.description[this.title.length-1]+
  101. '</div>'+
  102. '</div>'+
  103. '</div>'
  104. $(this.elem).prepend(titleStr);
  105. // 生成三角
  106. var self=this;
  107. $(this.elem).find(".layui-step-content-item").each(function(index,val) {
  108. if(self.styleLine){
  109. var l=index===0 ? 15 : 5;
  110. $(this).append("<span class='content-item-before' style='left: calc("+((100*index)/self.len)+"% + "+l+"px);'></span>");
  111. }else{
  112. $(this).append("<span class='content-item-before' style='left: calc("+((100/(self.len*2))+((100*index)/self.len))+"% - 10px);'></span>");
  113. }
  114. })
  115. },
  116. // 添加样式
  117. init: function() {
  118. var self=this;
  119. this.disabledStep.forEach(function(val){
  120. $(self.elem).find(".layui-step-title-item").eq(val-1).addClass("step-disabled");
  121. })
  122. $(this.elem).find(".layui-step-title-item").eq(this.currentStep-1).addClass("step-current");
  123. $(this.elem).find(".layui-step-content-item").eq(this.currentStep-1).show();
  124. if(this.currentStep<2) return;
  125. for(var i=this.currentStep-2;i>=0;i--){
  126. $(this.elem).find(".layui-step-title-item").eq(i).addClass("step-finish");
  127. }
  128. },
  129. // 恢复默认样式
  130. reInit: function() {
  131. $(this.elem).find(".layui-step-title-item").each(function(index,val) {
  132. $(val).removeClass("step-disabled");
  133. })
  134. $(this.elem).find(".layui-step-title-item").eq(this.currentStep-1).removeClass("step-current");
  135. $(this.elem).find(".layui-step-content-item").eq(this.currentStep-1).hide();
  136. if(this.currentStep<2) return;
  137. for(var i=this.currentStep-2;i>=0;i--){
  138. $(this.elem).find(".layui-step-title-item").eq(i).removeClass("step-finish");
  139. }
  140. },
  141. // 给上面的icon添加事件
  142. changeStep: function() {
  143. var self=this;
  144. this.canIconClick?(function() {
  145. $(self.elem).on("click",".layui-step-title-item .step-icon",function() {
  146. var index=$(this).parent(".layui-step-title-item").index()+1;
  147. // 判断点击的是否为disabled
  148. if($.inArray(index, self.disabledStep) === -1){
  149. self.goStep(index);
  150. }
  151. })
  152. })():"";
  153. },
  154. // 是否严格按照步骤条顺序执行步骤
  155. openStepLevel: function() {
  156. var self=this;
  157. this.isOpenStepLevel?(function() {
  158. // 如果开启这一项,则默认关闭icon点击事件
  159. self.canIconClick=false;
  160. $(self.elem).off().on("click",".layui-step-title-item .step-icon",function() {
  161. var index=$(this).parent(".layui-step-title-item").index()+1;
  162. // 判断如果当前点击的步骤超过已走过的最大步,则不跳转
  163. if(index>self.finalStep){
  164. return;
  165. }
  166. // 判断点击的是否为disabled
  167. if($.inArray(index, self.disabledStep) === -1){
  168. self.goStep(index);
  169. }
  170. })
  171. })():"";
  172. },
  173. // 跳转第几步
  174. goStep: function(i) {
  175. if((i<1 || i>this.len)){
  176. throw "goStep函数参数不在范围内";
  177. }
  178. // 判断当前页是否禁用,即i是否在数组中
  179. if($.inArray(i, this.disabledStep) === -1){
  180. this.reInit();
  181. this.currentStep=i;
  182. this.init();
  183. }else{
  184. throw "该页已禁用";
  185. }
  186. },
  187. // 跳到第一步
  188. goFirst: function() {
  189. this.goStep(1);
  190. },
  191. // 跳到最后一步
  192. goLast: function() {
  193. this.goStep(this.len);
  194. this.finalStep=this.len;
  195. this.openStepLevel();
  196. },
  197. // 跳到上一步
  198. prev: function () {
  199. if(this.currentStep<=1){
  200. return;
  201. }
  202. this.reInit();
  203. // 先保存当前位置,若前面的全都已经禁用,则可以回到当前位置
  204. var origin=this.currentStep;
  205. this.PrevGo(origin);
  206. },
  207. PrevGo: function(origin) {
  208. this.currentStep--;
  209. // 判断前面的是否全都已经禁用
  210. if(this.currentStep<1){
  211. this.currentStep=origin;
  212. this.init();
  213. return;
  214. }
  215. // 判断当前页是否禁用
  216. if($.inArray(this.currentStep, this.disabledStep) === -1){
  217. this.init();
  218. }else{
  219. this.PrevGo(origin);
  220. }
  221. },
  222. // 跳到下一部
  223. next: function () {
  224. if(this.currentStep>=this.len){
  225. return;
  226. }
  227. this.reInit();
  228. // 先保存当前位置,若后面的全都已经禁用,则可以回到当前位置
  229. var origin=this.currentStep;
  230. this.nextGo(origin);
  231. },
  232. nextGo: function(origin) {
  233. if(this.currentStep===this.finalStep){
  234. // 更新最远步
  235. this.finalStep++;
  236. this.openStepLevel();
  237. }
  238. this.currentStep++;
  239. // 判断后面的是否全都已经禁用
  240. if(this.currentStep>this.len){
  241. this.currentStep=origin;
  242. this.init();
  243. return;
  244. }
  245. // 判断当前步是否禁用
  246. if($.inArray(this.currentStep, this.disabledStep) === -1){
  247. this.init();
  248. }else{
  249. this.nextGo(origin);
  250. }
  251. },
  252. // 禁用某一步
  253. disabled: function (j) {
  254. if(j<=this.currentStep){
  255. throw "已经走过的步骤不能禁用";
  256. }
  257. // 当前步不存在则加入数组,否存重复添加
  258. if($.inArray(j,this.disabledStep)===-1){
  259. this.disabledStep.push(j);
  260. }
  261. //默认为起始从第一步开始,若第一步为disabled,则从前往后找到第一个不是disabled的作为第一步
  262. for(var i=this.currentStep;i<this.len;i++){
  263. if($.inArray(i, this.disabledStep) === -1){
  264. this.reInit();
  265. this.currentStep=i;
  266. this.init();
  267. i=this.len+1;
  268. }
  269. }
  270. },
  271. // 解除禁用
  272. abled: function (j) {
  273. if(j<=this.currentStep){
  274. throw "已经走过的步骤不能解除禁用";
  275. }
  276. // 删除数组数据
  277. var tem=this.disabledStep.concat();
  278. if($.inArray(j, tem) !== -1){
  279. this.disabledStep.splice($.inArray(j, tem),1);
  280. }
  281. this.reInit();
  282. this.init();
  283. }
  284. }
  285. var stepObj; // new的对象,作为内部变量
  286. var step={
  287. option: "",
  288. currentStep: 1,
  289. render: function(option) {
  290. var self=this;
  291. this.option=option || {};
  292. this.option.elem?"":(function() {
  293. throw '缺少参数,需要传入elem元素';
  294. })();
  295. !$(this.option.elem)[0]?(function() {
  296. throw '没有找到'+ self.option.elem +'元素';
  297. })():"";
  298. stepObj=new Step(this.option);
  299. this.currentStep=stepObj.currentStep;
  300. },
  301. goStep: function(i) {
  302. if(typeof i !== "number"){
  303. throw 'goStep参数不合法';
  304. }
  305. stepObj.goStep(i);
  306. this.currentStep=stepObj.currentStep;
  307. },
  308. goFirst: function() {
  309. stepObj.goFirst();
  310. this.currentStep=stepObj.currentStep;
  311. },
  312. goLast: function() {
  313. stepObj.goLast();
  314. this.currentStep=stepObj.currentStep;
  315. },
  316. prev: function() {
  317. stepObj.prev();
  318. this.currentStep=stepObj.currentStep;
  319. },
  320. next: function() {
  321. stepObj.next();
  322. this.currentStep=stepObj.currentStep;
  323. },
  324. disabled: function(i) {
  325. stepObj.disabled(i);
  326. },
  327. abled: function(i) {
  328. stepObj.abled(i);
  329. }
  330. }
  331. exports('step', step);
  332. })