cascader.js 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /**
  2. * 仿element-ui,级联选择器
  3. * 已实现单选、多选、无关联选择
  4. * 其他功能:组件禁用、节点禁用、自定义属性、自定义空面板提示,自定义无选择时的提示、多选标签折叠、回显、搜索、动态加载、最大选中数量限制、禁用项固定等操作。
  5. * element-ui没有的功能:最大选中数量限制、禁用项固定
  6. * author: yixiaco
  7. * gitee: https://gitee.com/yixiacoco/lay_cascader
  8. * github: https://github.com/yixiaco/lay_cascader
  9. */
  10. layui.define(["jquery"], function (exports) {
  11. var $ = layui.jquery;
  12. /**
  13. * 阻止事件冒泡
  14. * @param event
  15. */
  16. function stopPropagation(event) {
  17. event = event || window.event
  18. if (event.stopPropagation) {
  19. event.stopPropagation()
  20. } else {
  21. event.cancelBubble = true
  22. }
  23. }
  24. /**
  25. * 级联各项节点对象
  26. * @param data 原始对象信息
  27. * @param cascader 级联对象
  28. * @param level 层级,从0开始
  29. * @param parentNode 父节点对象
  30. * @constructor
  31. */
  32. function Node(data, cascader, level, parentNode) {
  33. this.data = data;
  34. this.cascader = cascader;
  35. this.config = cascader.config;
  36. this.props = cascader.props;
  37. this.level = level;
  38. this.parentNode = parentNode;
  39. // 引入的icon图标
  40. this.icons = cascader.icons;
  41. //该节点是否被选中 0:未选中,1:选中,2:不定
  42. this._checked = 0;
  43. // 是否正在加载中
  44. this._loading = false;
  45. // 每个Node的唯一标识
  46. this.nodeId = cascader.data.nodeId++;
  47. }
  48. Node.prototype = {
  49. constructor: Node,
  50. /** 最顶级父节点 */
  51. get topParentNode() {
  52. return !this.parentNode && this || this.topParentNode;
  53. },
  54. /** 子节点 */
  55. childrenNode: undefined,
  56. get loading() {
  57. return this._loading;
  58. },
  59. set loading(loading) {
  60. var $li = this.$li;
  61. if ($li) {
  62. var rightIcon = this.icons.right;
  63. var loadingIcon = this.icons.loading;
  64. var $i = $li.find('i');
  65. if (loading) {
  66. $i.addClass(loadingIcon);
  67. $i.removeClass(rightIcon);
  68. } else {
  69. $i.addClass(rightIcon);
  70. $i.removeClass(loadingIcon);
  71. }
  72. }
  73. return this._loading = loading;
  74. },
  75. /** 当前节点的显示文本 */
  76. get label() {
  77. return this.data[this.props.label];
  78. },
  79. /** 当前节点的值 */
  80. get value() {
  81. return this.data[this.props.value];
  82. },
  83. /** 是否禁用 */
  84. get disabled() {
  85. var multiple = this.props.multiple;
  86. var maxSize = this.config.maxSize;
  87. var checkedNodeIds = this.cascader.data.checkedNodeIds;
  88. var disabledName = this.props.disabled;
  89. var checkStrictly = this.props.checkStrictly;
  90. // 检查是否超过最大值限制
  91. if (multiple && maxSize !== 0) {
  92. if (checkedNodeIds.length >= maxSize && checkedNodeIds.indexOf(this.nodeId) === -1) {
  93. // 如果是关联的多选,需要查询叶子节点是否有被选中的项
  94. if (!checkStrictly) {
  95. var leafChildren = this.getAllLeafChildren();
  96. var nodeIds = leafChildren.map(function (value) {
  97. return value.nodeId
  98. });
  99. // 如果叶子节点不包含,则直接返回true
  100. if (!nodeIds.some(function (nodeId) {
  101. return checkedNodeIds.indexOf(nodeId) !== -1;
  102. })) {
  103. return true;
  104. }
  105. } else {
  106. return true;
  107. }
  108. }
  109. }
  110. if (!checkStrictly) {
  111. var path = this.path;
  112. return path.some(function (node) {
  113. return node.data[disabledName];
  114. });
  115. } else {
  116. return this.data[disabledName];
  117. }
  118. },
  119. /** 子节点数据 */
  120. get children() {
  121. return this.data[this.props.children];
  122. },
  123. set children(children) {
  124. this.data[this.props.children] = children;
  125. },
  126. /** 叶子节点 */
  127. get leaf() {
  128. var leaf = this.data[this.props.leaf];
  129. if (typeof leaf === 'boolean') {
  130. return leaf;
  131. }
  132. // 如果children不为空,则判断是否是子节点
  133. if (this.children) {
  134. return this.children.length <= 0;
  135. }
  136. return true;
  137. },
  138. /** 当前单选值 */
  139. get activeNodeId() {
  140. return this.cascader.data.activeNodeId;
  141. },
  142. /** 当前复选框值 */
  143. get checkedNodeIds() {
  144. return this.cascader.data.checkedNodeIds;
  145. },
  146. /** 路径 */
  147. get path() {
  148. var parentNode = this.parentNode;
  149. if (parentNode) {
  150. return parentNode.path.concat([this]);
  151. } else {
  152. return [this];
  153. }
  154. },
  155. /** 是否正在搜索中 */
  156. get isFiltering() {
  157. return this.cascader.isFiltering;
  158. },
  159. /** 输入框的tag标签 */
  160. get $tag() {
  161. var cascader = this.cascader;
  162. var showAllLevels = this.config.showAllLevels;
  163. var disabled = this.config.disabled;
  164. var nodeDisabled = this.disabled;
  165. var disabledFixed = this.config.disabledFixed;
  166. var label = this.getPathLabel(showAllLevels);
  167. var $tag = cascader.get$tag(label, !disabled && (!nodeDisabled || !disabledFixed));
  168. var self = this;
  169. $tag.find('i').click(function (event) {
  170. stopPropagation(event);
  171. self.selectedValue();
  172. cascader.removeTag(self.value, self);
  173. });
  174. return $tag;
  175. },
  176. /**
  177. * 完整路径的标签
  178. * @param showAllLevels
  179. * @returns {string}
  180. */
  181. getPathLabel: function (showAllLevels) {
  182. var path = this.path;
  183. var separator = this.config.separator;
  184. var label;
  185. if (showAllLevels) {
  186. label = path.map(function (node) {
  187. return node.label;
  188. }).join(separator);
  189. } else {
  190. label = path[path.length - 1].label;
  191. }
  192. return label;
  193. },
  194. /**
  195. * 初始化
  196. */
  197. init: function () {
  198. var multiple = this.props.multiple;
  199. var checkStrictly = this.props.checkStrictly;
  200. var fromIcon = this.icons.from;
  201. var rightIcon = this.icons.right;
  202. var icon = '';
  203. var label = this.label;
  204. if (!this.leaf) {
  205. icon = rightIcon;
  206. }
  207. this.$li = $('<li role="menuitem" id="cascader-menu" tabindex="-1" class="el-cascader-node" aria-haspopup="true" aria-owns="cascader-menu"><span class="el-cascader-node__label">' + label + '</span><i class="' + fromIcon + ' ' + icon + '"></i></li>');
  208. // 节点渲染
  209. if (!multiple && !checkStrictly) {
  210. this._renderRadio();
  211. } else if (!multiple && checkStrictly) {
  212. this._renderRadioCheckStrictly();
  213. } else if (multiple && !checkStrictly) {
  214. this._renderMultiple();
  215. } else if (multiple && checkStrictly) {
  216. this._renderMultipleCheckStrictly();
  217. }
  218. },
  219. /**
  220. * 初始化可搜索li
  221. */
  222. initSuggestionLi: function () {
  223. var label = this.getPathLabel(true);
  224. this.$suggestionLi = $('<li tabindex="-1" class="el-cascader__suggestion-item"><span>' + label + '</span></li>');
  225. // 节点渲染
  226. this._renderFiltering();
  227. },
  228. /**
  229. * 绑定到菜单中
  230. * @param $list li节点
  231. */
  232. bind: function ($list) {
  233. this.init();
  234. $list.append(this.$li);
  235. },
  236. /**
  237. * 绑定可搜索到列表中
  238. * @param $list
  239. */
  240. bindSuggestion: function ($list) {
  241. this.initSuggestionLi();
  242. $list.append(this.$suggestionLi);
  243. },
  244. /**
  245. * 可搜索渲染
  246. * @private
  247. */
  248. _renderFiltering: function () {
  249. var $li = this.$suggestionLi;
  250. var nodeId = this.nodeId;
  251. var fromIcon = this.icons.from;
  252. var okIcon = this.icons.ok;
  253. var self = this;
  254. var cascader = this.cascader;
  255. var multiple = this.props.multiple;
  256. var icon = '<i class="' + fromIcon + ' ' + okIcon + ' el-icon-check"></i>';
  257. $li.click(function (event) {
  258. stopPropagation(event);
  259. self.selectedValue();
  260. if (multiple) {
  261. if (self.checkedNodeIds.indexOf(nodeId) === -1) {
  262. $li.removeClass('is-checked');
  263. $li.find('.el-icon-check').remove();
  264. } else {
  265. $li.addClass('is-checked');
  266. $li.append(icon);
  267. }
  268. } else {
  269. // 关闭面板
  270. cascader.close();
  271. }
  272. });
  273. if (multiple && self.checkedNodeIds.indexOf(nodeId) !== -1
  274. || !multiple && self.activeNodeId === nodeId) {
  275. $li.addClass('is-checked');
  276. $li.append(icon)
  277. }
  278. },
  279. /**
  280. * 单选&&关联
  281. * @private
  282. */
  283. _renderRadio: function () {
  284. var $li = this.$li;
  285. var nodeId = this.nodeId;
  286. var fromIcon = this.icons.from;
  287. var okIcon = this.icons.ok;
  288. var level = this.level;
  289. var leaf = this.leaf;
  290. var self = this;
  291. var cascader = this.cascader;
  292. var activeNode = this.cascader.data.activeNode;
  293. var parentNode = this.parentNode;
  294. if (self.activeNodeId && activeNode.path.some(function (node) {
  295. return node.nodeId === nodeId;
  296. })) {
  297. if (self.activeNodeId === nodeId) {
  298. $li.prepend('<i class="' + fromIcon + ' ' + okIcon + ' el-cascader-node__prefix"></i>');
  299. }
  300. $li.addClass('is-active');
  301. $li.addClass('in-checked-path');
  302. }
  303. // 是否禁用
  304. if (this.disabled) {
  305. $li.addClass('is-disabled');
  306. return;
  307. }
  308. $li.addClass('is-selectable');
  309. if (parentNode) {
  310. parentNode.$li.siblings().removeClass('in-active-path');
  311. parentNode.$li.addClass('in-active-path');
  312. }
  313. // 触发下一个节点
  314. this._liClick(function (event) {
  315. stopPropagation(event);
  316. var childrenNode = self.childrenNode;
  317. if (leaf && event.type === 'click') {
  318. self.selectedValue();
  319. // 关闭面板
  320. cascader.close();
  321. }
  322. // 添加下级菜单
  323. cascader._appendMenu(childrenNode, level + 1, self);
  324. });
  325. },
  326. /**
  327. * 单选&&非关联
  328. * @private
  329. */
  330. _renderRadioCheckStrictly: function () {
  331. var $li = this.$li;
  332. var nodeId = this.nodeId;
  333. var level = this.level;
  334. var leaf = this.leaf;
  335. var self = this;
  336. var cascader = this.cascader;
  337. var activeNode = cascader.data.activeNode;
  338. var parentNode = this.parentNode;
  339. $li.addClass('is-selectable');
  340. // 任意一级单选
  341. var $radio = $('<label role="radio" tabindex="0" class="el-radio"><span class="el-radio__input"><span class="el-radio__inner"></span><input type="radio" aria-hidden="true" tabindex="-1" class="el-radio__original" value="' + nodeId + '"></span><span class="el-radio__label"><span></span></span></label>');
  342. this.$radio = $radio;
  343. $li.prepend($radio);
  344. if (parentNode) {
  345. parentNode.$li.siblings().removeClass('in-active-path');
  346. parentNode.$li.addClass('in-active-path');
  347. }
  348. // 触发下一个节点
  349. this._liClick(function (event) {
  350. stopPropagation(event);
  351. var childrenNode = self.childrenNode;
  352. if (!self.disabled && leaf && event.type === 'click') {
  353. self.selectedValue();
  354. }
  355. // 添加下级菜单
  356. cascader._appendMenu(childrenNode, level + 1, self);
  357. });
  358. if (self.activeNodeId && activeNode.path.some(function (node) {
  359. return node.nodeId === nodeId;
  360. })) {
  361. if (self.activeNodeId === nodeId) {
  362. $radio.find('.el-radio__input').addClass('is-checked');
  363. }
  364. $li.addClass('is-active');
  365. $li.addClass('in-checked-path');
  366. }
  367. if (this.disabled) {
  368. $radio.addClass('is-disabled');
  369. $radio.find('.el-radio__input').addClass('is-disabled');
  370. return;
  371. }
  372. // 选中事件
  373. $radio.click(function (event) {
  374. event.preventDefault();
  375. !leaf && self.selectedValue();
  376. });
  377. },
  378. /**
  379. * 多选&&关联
  380. * @private
  381. */
  382. _renderMultiple: function () {
  383. var $li = this.$li;
  384. var level = this.level;
  385. var leaf = this.leaf;
  386. var self = this;
  387. var cascader = this.cascader;
  388. var checked = this._checked;
  389. var parentNode = this.parentNode;
  390. $li.addClass('el-cascader-node');
  391. // 多选框
  392. var $checked = $('<label class="el-checkbox"><span class="el-checkbox__input"><span class="el-checkbox__inner"></span><input type="checkbox" aria-hidden="false" class="el-checkbox__original" value=""></span></label>');
  393. this.$checked = $checked;
  394. $li.prepend($checked);
  395. // 渲染
  396. if (checked === 1) {
  397. this.$checked.find('.el-checkbox__input').addClass('is-checked');
  398. } else if (checked === 2) {
  399. this.$checked.find('.el-checkbox__input').addClass('is-indeterminate');
  400. }
  401. if (parentNode) {
  402. parentNode.$li.siblings().removeClass('in-active-path');
  403. parentNode.$li.addClass('in-active-path');
  404. }
  405. // 触发下一个节点
  406. this._liClick(function (event) {
  407. stopPropagation(event);
  408. var childrenNode = self.childrenNode;
  409. if (!self.disabled && leaf && event.type === 'click') {
  410. // 最后一级就默认选择
  411. self.selectedValue();
  412. }
  413. // 添加下级菜单
  414. cascader._appendMenu(childrenNode, level + 1, self);
  415. });
  416. if (this.disabled) {
  417. $li.addClass('is-disabled');
  418. $checked.addClass('is-disabled');
  419. $checked.find('.el-checkbox__input').addClass('is-disabled');
  420. return;
  421. }
  422. // 选中事件
  423. $checked.click(function (event) {
  424. event.preventDefault();
  425. if (!leaf) {
  426. var childrenNode = self.childrenNode;
  427. self.selectedValue();
  428. cascader._appendMenu(childrenNode, level + 1, self);
  429. }
  430. });
  431. },
  432. /**
  433. * 多选&&非关联
  434. * @private
  435. */
  436. _renderMultipleCheckStrictly: function () {
  437. var $li = this.$li;
  438. var level = this.level;
  439. var leaf = this.leaf;
  440. var self = this;
  441. var cascader = this.cascader;
  442. var checkedNodeIds = cascader.data.checkedNodeIds;
  443. var checkedNodes = cascader.data.checkedNodes;
  444. var nodeId = this.nodeId;
  445. var parentNode = this.parentNode;
  446. $li.addClass('el-cascader-node is-selectable');
  447. // 多选框
  448. var $checked = $('<label class="el-checkbox"><span class="el-checkbox__input"><span class="el-checkbox__inner"></span><input type="checkbox" aria-hidden="false" class="el-checkbox__original" value=""></span></label>');
  449. this.$checked = $checked;
  450. $li.prepend($checked);
  451. // 渲染
  452. var exist = checkedNodes.some(function (node) {
  453. return node.path.some(function (node) {
  454. return node.nodeId === nodeId;
  455. })
  456. });
  457. if (exist) {
  458. $li.addClass('in-checked-path');
  459. if (checkedNodeIds.indexOf(nodeId) !== -1) {
  460. this.$checked.find('.el-checkbox__input').addClass('is-checked');
  461. }
  462. }
  463. if (parentNode) {
  464. parentNode.$li.siblings().removeClass('in-active-path');
  465. parentNode.$li.addClass('in-active-path');
  466. }
  467. // 触发下一个节点
  468. this._liClick(function (event) {
  469. stopPropagation(event);
  470. var childrenNode = self.childrenNode;
  471. if (!self.disabled && leaf && event.type === 'click') {
  472. // 最后一级就默认选择
  473. self.selectedValue();
  474. }
  475. // 添加下级菜单
  476. cascader._appendMenu(childrenNode, level + 1, self);
  477. });
  478. if (this.disabled) {
  479. $checked.addClass('is-disabled');
  480. $checked.find('.el-checkbox__input').addClass('is-disabled');
  481. return;
  482. }
  483. // 选中事件
  484. $checked.click(function (event) {
  485. event.preventDefault();
  486. if (!leaf) {
  487. self.selectedValue();
  488. var childrenNode = self.childrenNode;
  489. // 添加下级菜单
  490. cascader._appendMenu(childrenNode, level + 1, self);
  491. }
  492. });
  493. },
  494. /**
  495. * 向上传递
  496. * @param callback 执行方法,如果返回false,则中断执行
  497. * @param advance 是否先执行一次
  498. * @param self 自身
  499. */
  500. transferParent: function (callback, advance, self) {
  501. if (!self) {
  502. self = this;
  503. }
  504. if (this !== self || advance) {
  505. var goOn = callback && callback(this);
  506. if (goOn === false) {
  507. return;
  508. }
  509. }
  510. this.parentNode && this.parentNode.transferParent(callback, advance, self);
  511. },
  512. /**
  513. * 向下传递
  514. * @param callback 执行的方法,如果返回false,则中断执行
  515. * @param advance 是否先执行一次
  516. * @param self 自身
  517. */
  518. transferChildren: function (callback, advance, self) {
  519. if (!self) {
  520. self = this;
  521. }
  522. if (this !== self || advance) {
  523. var goOn = callback && callback(this);
  524. if (goOn === false) {
  525. return;
  526. }
  527. }
  528. var children = this.getChildren();
  529. if (children && children.length > 0) {
  530. for (var index in children) {
  531. children[index].transferChildren(callback, advance, self);
  532. }
  533. }
  534. },
  535. /**
  536. * 设置级联值
  537. */
  538. selectedValue: function () {
  539. var nodeId = this.nodeId;
  540. var cascader = this.cascader;
  541. var multiple = this.props.multiple;
  542. var checkStrictly = this.props.checkStrictly;
  543. var leaf = this.leaf;
  544. if (!multiple && (leaf || checkStrictly)) {
  545. cascader._setActiveValue(nodeId, this);
  546. } else if (multiple) {
  547. var checkedNodeIds = cascader.data.checkedNodeIds;
  548. var checkedNodes = cascader.data.checkedNodes;
  549. var disabledFixed = this.config.disabledFixed;
  550. var paths;
  551. if (checkStrictly) {
  552. var index = checkedNodeIds.indexOf(nodeId);
  553. if (index === -1) {
  554. paths = checkedNodes.concat([this]);
  555. } else {
  556. paths = checkedNodes.concat();
  557. paths.splice(index, 1);
  558. }
  559. } else {
  560. var allLeafChildren = this.getAllLeafChildren();
  561. var checked;
  562. if (this._checked !== 1 && disabledFixed) {
  563. checked = this._getMultipleChecked(allLeafChildren);
  564. } else {
  565. checked = this._checked;
  566. }
  567. if (checked === 1) {
  568. // 选中->未选中
  569. paths = checkedNodes.filter(function (node1) {
  570. return !allLeafChildren.some(function (node2) {
  571. return node1.nodeId === node2.nodeId;
  572. });
  573. });
  574. } else {
  575. // 未选中、部分选中->选中
  576. var add = allLeafChildren.filter(function (node) {
  577. return checkedNodeIds.indexOf(node.nodeId) === -1;
  578. });
  579. paths = checkedNodes.concat(add);
  580. }
  581. }
  582. var nodeIds = paths.map(function (node) {
  583. return node.nodeId;
  584. });
  585. cascader._setCheckedValue(nodeIds, paths);
  586. }
  587. },
  588. _liLoad: function (event, callback) {
  589. var leaf = this.leaf;
  590. var lazy = this.props.lazy;
  591. var lazyLoad = this.props.lazyLoad;
  592. var children = this.children;
  593. var self = this;
  594. var cascader = this.cascader;
  595. var level = this.level;
  596. var multiple = this.props.multiple;
  597. var checkStrictly = this.props.checkStrictly;
  598. if (!leaf && (!children || children.length === 0) && lazy) {
  599. if (!self.loading) {
  600. self.loading = true;
  601. lazyLoad(self, function (nodes) {
  602. self.loading = false;
  603. self.setChildren(cascader.initNodes(nodes, level + 1, self));
  604. self.children = nodes;
  605. callback && callback(event);
  606. // 多选&关联时,重新同步下父级节点的样式
  607. multiple && !checkStrictly && self.transferParent(function (node) {
  608. node.syncStyle();
  609. }, true);
  610. });
  611. }
  612. } else {
  613. callback && callback(event);
  614. }
  615. },
  616. /**
  617. * 点击li事件
  618. * @param callback
  619. * @private
  620. */
  621. _liClick: function (callback) {
  622. var leaf = this.leaf;
  623. var $li = this.$li;
  624. var self = this;
  625. function load(event) {
  626. self._liLoad(event, callback);
  627. }
  628. if (this.props.expandTrigger === "click" || leaf) {
  629. $li.click(load);
  630. }
  631. if (this.props.expandTrigger === "hover") {
  632. $li.mouseenter(load);
  633. }
  634. },
  635. setChildren: function (children) {
  636. this.childrenNode = children;
  637. },
  638. getChildren: function () {
  639. return this.childrenNode;
  640. },
  641. /**
  642. * 同步样式
  643. */
  644. syncStyle: function () {
  645. var multiple = this.props.multiple;
  646. var checkStrictly = this.props.checkStrictly;
  647. if (multiple) {
  648. //多选
  649. if (checkStrictly) {
  650. this._sync.syncMultipleCheckStrictly(this);
  651. } else {
  652. this._sync.syncMultiple(this);
  653. }
  654. } else {
  655. //单选
  656. if (checkStrictly) {
  657. this._sync.syncRadioCheckStrictly(this);
  658. } else {
  659. this._sync.syncRadio(this);
  660. }
  661. }
  662. },
  663. /**
  664. * 同步本节点样式
  665. */
  666. _sync: {
  667. /**
  668. * 同步单选关联样式
  669. */
  670. syncRadio: function (self) {
  671. var $li = self.$li;
  672. var fromIcon = self.icons.from;
  673. var okIcon = self.icons.ok;
  674. var multiple = self.props.multiple;
  675. var checkStrictly = self.props.checkStrictly;
  676. var nodeId = self.nodeId;
  677. if (!$li || multiple || checkStrictly) {
  678. return;
  679. }
  680. var activeNode = self.cascader.data.activeNode;
  681. if (self.activeNodeId === nodeId) {
  682. var ok = $li.find('.' + okIcon);
  683. if (ok.length === 0) {
  684. $li.prepend('<i class="' + fromIcon + ' ' + okIcon + ' el-cascader-node__prefix"></i>');
  685. }
  686. } else {
  687. $li.find('.' + okIcon).remove();
  688. }
  689. if (activeNode && activeNode.path.some(function (node) {
  690. return node.nodeId === nodeId;
  691. })) {
  692. $li.addClass('is-active');
  693. $li.addClass('in-checked-path');
  694. } else {
  695. $li.removeClass('is-active');
  696. $li.removeClass('in-checked-path');
  697. }
  698. },
  699. /**
  700. * 同步单选非关联样式
  701. */
  702. syncRadioCheckStrictly: function (self) {
  703. var $li = self.$li;
  704. var checkStrictly = self.props.checkStrictly;
  705. var multiple = self.props.multiple;
  706. if (!$li || multiple || !checkStrictly) {
  707. return;
  708. }
  709. var $radio = self.$radio;
  710. var activeNode = self.cascader.data.activeNode;
  711. var nodeId = self.nodeId;
  712. if (self.activeNodeId === nodeId) {
  713. $radio.find('.el-radio__input').addClass('is-checked');
  714. } else {
  715. $radio.find('.el-radio__input').removeClass('is-checked');
  716. }
  717. if (activeNode && activeNode.path.some(function (node) {
  718. return node.nodeId === nodeId;
  719. })) {
  720. $li.addClass('is-active');
  721. $li.addClass('in-checked-path');
  722. } else {
  723. $li.removeClass('is-active');
  724. $li.removeClass('in-checked-path');
  725. }
  726. },
  727. /**
  728. * 同步多选关联样式
  729. */
  730. syncMultiple: function (self) {
  731. var $li = self.$li;
  732. var checkStrictly = self.props.checkStrictly;
  733. var multiple = self.props.multiple;
  734. var disabledFixed = self.config.disabledFixed;
  735. if (!multiple || checkStrictly) {
  736. return;
  737. }
  738. var allLeafChildren = self.getAllLeafChildren(disabledFixed);
  739. // 全部未选中 0
  740. // 全部选中 1
  741. // 部分选中 2
  742. var checked = self._getMultipleChecked(allLeafChildren);
  743. self._checked = checked;
  744. if (!$li) {
  745. return;
  746. }
  747. var $checkbox = self.$checked.find('.el-checkbox__input');
  748. if (checked === 0) {
  749. $checkbox.removeClass('is-checked');
  750. $checkbox.removeClass('is-indeterminate');
  751. } else if (checked === 1) {
  752. $checkbox.removeClass('is-indeterminate');
  753. $checkbox.addClass('is-checked');
  754. } else if (checked === 2) {
  755. $checkbox.removeClass('is-checked');
  756. $checkbox.addClass('is-indeterminate');
  757. }
  758. },
  759. /**
  760. * 同步多选非关联样式
  761. */
  762. syncMultipleCheckStrictly: function (self) {
  763. var $li = self.$li;
  764. var checkStrictly = self.props.checkStrictly;
  765. var multiple = self.props.multiple;
  766. if (!$li || !multiple || !checkStrictly) {
  767. return;
  768. }
  769. var checkedNodes = self.cascader.data.checkedNodes;
  770. var checkedNodeIds = self.checkedNodeIds;
  771. var nodeId = self.nodeId;
  772. var exist = checkedNodes.some(function (node) {
  773. return node.path.some(function (node) {
  774. return node.nodeId === nodeId;
  775. });
  776. });
  777. var $checkbox = self.$checked.find('.el-checkbox__input');
  778. if (checkedNodeIds.some(function (checkedNodeId) {
  779. return checkedNodeId === nodeId;
  780. })) {
  781. // 选中
  782. $checkbox.addClass('is-checked');
  783. } else {
  784. // 未选中
  785. $checkbox.removeClass('is-checked');
  786. }
  787. if (exist) {
  788. $li.addClass('in-checked-path');
  789. } else {
  790. $li.removeClass('in-checked-path');
  791. }
  792. }
  793. },
  794. /**
  795. * 获取叶子节点value值
  796. * @param disabled 是否包含禁用,默认不包含
  797. * @returns {Node[]|*[]}
  798. */
  799. getAllLeafChildren: function (disabled) {
  800. var leaf = this.leaf;
  801. if (leaf) {
  802. return [this];
  803. } else {
  804. var leafs = [];
  805. this.transferChildren(function (node) {
  806. if (node.disabled && !disabled) {
  807. return false;
  808. }
  809. node.leaf && leafs.push(node);
  810. });
  811. return leafs;
  812. }
  813. },
  814. /**
  815. * 展开当前节点
  816. */
  817. expandPanel: function () {
  818. var path = this.path;
  819. var cascader = this.cascader;
  820. path.forEach(function (node, index, array) {
  821. if (index !== array.length - 1) {
  822. var childrenNode = node.childrenNode;
  823. cascader._appendMenu(childrenNode, node.level + 1, node.parentNode);
  824. }
  825. });
  826. },
  827. _getMultipleChecked: function (leafNodes) {
  828. var cascader = this.cascader;
  829. var checkedNodeIds = cascader.data.checkedNodeIds;
  830. var allLeafIds = leafNodes.map(function (node) {
  831. return node.nodeId;
  832. });
  833. // 全部未选中 0
  834. // 全部选中 1
  835. // 部分选中 2
  836. var checked = 0;
  837. for (var i = 0; i < allLeafIds.length; i++) {
  838. var child = allLeafIds[i];
  839. if (checked === 2) {
  840. break;
  841. }
  842. if (checkedNodeIds.indexOf(child) !== -1) {
  843. if (i > 0 && checked !== 1) {
  844. checked = 2;
  845. } else {
  846. checked = 1;
  847. }
  848. } else {
  849. // 当全部选中时,则改为部分选中,否则全部未选中
  850. checked = checked === 1 ? 2 : 0;
  851. }
  852. }
  853. return checked;
  854. }
  855. };
  856. function Cascader(config) {
  857. this.config = $.extend(true, {
  858. elem: '', //绑定元素
  859. value: null, //预设值
  860. options: [], //可选项数据源,键名可通过 Props 属性配置
  861. empty: '暂无数据', //无匹配选项时的内容
  862. placeholder: '请选择',//输入框占位文本
  863. disabled: false, //是否禁用
  864. clearable: false, //是否支持清空选项
  865. showAllLevels: true, //输入框中是否显示选中值的完整路径
  866. collapseTags: false, //多选模式下是否折叠Tag
  867. minCollapseTagsNumber: 1, //最小折叠标签数
  868. separator: ' / ', //选项分隔符
  869. filterable: false, //是否可搜索选项
  870. filterMethod: function (node, keyword) {
  871. return node.path.some(function (node) {
  872. return node.label.indexOf(keyword) !== -1;
  873. });
  874. }, //自定义搜索逻辑,第一个参数是节点node,第二个参数是搜索关键词keyword,通过返回布尔值表示是否命中
  875. debounce: 300, //搜索关键词输入的去抖延迟,毫秒
  876. beforeFilter: function (value) {
  877. return true;
  878. },//筛选之前的钩子,参数为输入的值,若返回 false,则停止筛选
  879. popperClass: '', // 自定义浮层类名 string
  880. extendClass: false, //继承class样式
  881. extendStyle: false, //继承style样式
  882. disabledFixed: false, //固定禁用项,使禁用项不被清理删除,禁用项只能通过函数添加或初始值添加,默认禁用项不可被函数或初始值添加
  883. maxSize: 0, // 多选选中的最大数量,0表示不限制
  884. props: {
  885. strictMode: false, //严格模式,设置value严格按照层级结构.例如:[[1,2,3],[1,2,4]]
  886. expandTrigger: 'click', //次级菜单的展开方式 string click / hover 'click'
  887. multiple: false, //是否多选 boolean - false
  888. checkStrictly: false, //是否严格的遵守父子节点不互相关联 boolean - false
  889. lazy: false, //是否动态加载子节点,需与 lazyLoad 方法结合使用 boolean - false
  890. lazyLoad: function (node, resolve) {
  891. }, //加载动态数据的方法,仅在 lazy 为 true 时有效 function(node, resolve),node为当前点击的节点,resolve为数据加载完成的回调(必须调用)
  892. value: 'value', //指定选项的值为选项对象的某个属性值 string — 'value'
  893. label: 'label', //指定选项标签为选项对象的某个属性值 string — 'label'
  894. children: 'children', //指定选项的子选项为选项对象的某个属性值 string — 'children'
  895. disabled: 'disabled', //指定选项的禁用为选项对象的某个属性值 string — 'disabled'
  896. leaf: 'leaf' //指定选项的叶子节点的标志位为选项对象的某个属性值 string — 'leaf'
  897. }
  898. }, config);
  899. this.data = {
  900. nodeId: 1, //nodeId的自增值
  901. nodes: [], //存储Node对象
  902. menuData: [], //压入菜单的数据
  903. activeNodeId: null, //存放NodeId
  904. activeNode: null, //存放Node
  905. checkedNodeIds: [], //存放多个NodeId
  906. checkedNodes: [] //存放多个Node
  907. };
  908. // 面板是否展开
  909. this.showPanel = false;
  910. this.event = {
  911. // 值变更事件
  912. change: [],
  913. // 打开事件
  914. open: [],
  915. // 关闭事件
  916. close: []
  917. }
  918. // 是否正在搜索中
  919. this.filtering = false;
  920. // 初始化
  921. this._init();
  922. // 面板关闭事件id
  923. this.closeEventId = 0;
  924. // 是否进入maxSize模式
  925. this._maxSizeMode = null;
  926. }
  927. Cascader.prototype = {
  928. constructor: Cascader,
  929. get props() {
  930. return this.config.props;
  931. },
  932. get isFiltering() {
  933. return this.filtering;
  934. },
  935. set isFiltering(filtering) {
  936. if (this.filtering === filtering) {
  937. return;
  938. }
  939. this.filtering = !!filtering;
  940. var $panel = this.$panel;
  941. if (this.filtering) {
  942. $panel.find('.el-cascader-panel').hide();
  943. $panel.find('.el-cascader__suggestion-panel').show();
  944. } else {
  945. $panel.find('.el-cascader-panel').show();
  946. $panel.find('.el-cascader__suggestion-panel').hide();
  947. this.$tagsInput && this.$tagsInput.val('')
  948. }
  949. },
  950. set maxSizeMode(maxSizeMode) {
  951. if (this._maxSizeMode !== maxSizeMode) {
  952. this._maxSizeMode = maxSizeMode;
  953. this.refreshMenu();
  954. }
  955. },
  956. icons: {
  957. from: 'layui-icon',
  958. down: 'layui-icon-triangle-d',
  959. close: 'layui-icon-close',
  960. right: 'layui-icon-right',
  961. ok: 'layui-icon-ok',
  962. loading: 'layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop'
  963. },
  964. // 初始化
  965. _init: function () {
  966. this._checkConfig();
  967. // 初始化输入框
  968. this._initInput();
  969. // 初始化面板
  970. this._initPanel();
  971. // 初始化选项值
  972. this.setOptions(this.config.options);
  973. var self = this;
  974. // 监听滚动条
  975. $(window).scroll(function () {
  976. self._resetXY();
  977. });
  978. // 监听窗口
  979. $(window).resize(function () {
  980. self._resetXY();
  981. });
  982. // 点击事件,展开面板
  983. this.$div.click(function (event) {
  984. if (self.config.disabled) {
  985. return;
  986. }
  987. var show = self.showPanel;
  988. if (!show) {
  989. self.open();
  990. } else {
  991. self.close();
  992. }
  993. });
  994. },
  995. /**
  996. * 检查配置
  997. * @private
  998. */
  999. _checkConfig: function () {
  1000. var elem = this.config.elem;
  1001. if (!elem || $(elem).length === 0) {
  1002. throw new Error("缺少elem节点选择器");
  1003. }
  1004. var maxSize = this.config.maxSize;
  1005. if (typeof maxSize !== 'number' || maxSize < 0) {
  1006. throw new Error("maxSize应是一个大于等于0的有效的number值");
  1007. }
  1008. if (!Array.isArray(this.config.options)) {
  1009. throw new Error("options不是一个有效的数组");
  1010. }
  1011. },
  1012. /**
  1013. * 初始化根目录
  1014. * @private
  1015. */
  1016. _initRoot: function () {
  1017. var lazy = this.props.lazy;
  1018. var lazyLoad = this.props.lazyLoad;
  1019. var self = this;
  1020. var nodes = this.data.nodes;
  1021. if (nodes.length > 0 || !lazy) {
  1022. this._appendMenu(nodes, 0);
  1023. } else if (lazy) {
  1024. this._appendMenu(nodes, 0);
  1025. lazyLoad({
  1026. root: true,
  1027. level: 0
  1028. }, function (nodes) {
  1029. self.data.nodes = self.initNodes(nodes, 0, null);
  1030. self._appendMenu(self.data.nodes, 0);
  1031. });
  1032. }
  1033. },
  1034. /**
  1035. * 设置选项值
  1036. * @param options
  1037. */
  1038. setOptions: function (options) {
  1039. this.config.options = options;
  1040. // 初始化节点
  1041. this.data.nodes = this.initNodes(options, 0, null);
  1042. // 初始化根目录
  1043. this._initRoot();
  1044. // 初始化值
  1045. this.setValue(this.config.value);
  1046. },
  1047. // 面板定位
  1048. _resetXY: function () {
  1049. var $div = this.$div;
  1050. var offset = $div.offset();
  1051. var $panel = this.$panel;
  1052. if ($panel) {
  1053. var windowHeight = window.innerHeight;
  1054. var windowWidth = window.innerWidth;
  1055. var panelHeight = $panel.height();
  1056. var panelWidth = $panel.width();
  1057. var divHeight = $div.height();
  1058. var boundingClientRect = $div[0].getBoundingClientRect();
  1059. var $arrow = $panel.find('.popper__arrow');
  1060. // 距离右边界的偏差值
  1061. var offsetDiff = Math.min(windowWidth - boundingClientRect.x - panelWidth - 5, 0);
  1062. var bottomHeight = windowHeight - (boundingClientRect.top + divHeight);
  1063. if (bottomHeight < panelHeight && boundingClientRect.top > panelHeight + 20) {
  1064. $panel.attr('x-placement', 'top-start')
  1065. // 向上
  1066. $panel.css({
  1067. top: offset.top - 20 - panelHeight + 'px',
  1068. left: offset.left + offsetDiff + 'px'
  1069. });
  1070. } else {
  1071. $panel.attr('x-placement', 'bottom-start');
  1072. // 距离底部边界的偏差值
  1073. var yOffset = Math.max(panelHeight - (windowHeight - boundingClientRect.y - divHeight - 15), 0);
  1074. // 向下
  1075. $panel.css({
  1076. top: offset.top + divHeight - yOffset + 'px',
  1077. left: offset.left + offsetDiff + 'px'
  1078. });
  1079. }
  1080. // 箭头偏移
  1081. $arrow.css("left", 35 - offsetDiff + "px");
  1082. }
  1083. },
  1084. get $menus() {
  1085. return this.$panel && this.$panel.find('.el-cascader-panel .el-cascader-menu');
  1086. },
  1087. // 初始化输入框
  1088. _initInput: function () {
  1089. var $e = $(this.config.elem);
  1090. var self = this;
  1091. // 当绑定的元素带有value属性,并且对象未设置值时,设置一个初始值
  1092. if (this.config.value === null && $e.attr('value')) {
  1093. this.config.value = $e.attr('value');
  1094. }
  1095. var placeholder = this.config.placeholder;
  1096. var fromIcon = this.icons.from;
  1097. var downIcon = this.icons.down;
  1098. var multiple = this.props.multiple;
  1099. var extendClass = this.config.extendClass;
  1100. var extendStyle = this.config.extendStyle;
  1101. this.$div = $('<div class="el-cascader"></div>');
  1102. if (extendStyle) {
  1103. var style = $e.attr('style');
  1104. if (style) {
  1105. this.$div.attr('style', style);
  1106. }
  1107. }
  1108. if (extendClass) {
  1109. var className = $e.attr('class');
  1110. if (className) {
  1111. className.split(' ').forEach(function (name) {
  1112. self.$div.addClass(name);
  1113. });
  1114. }
  1115. }
  1116. this.$input = $('<div class="el-input el-input--suffix">' +
  1117. '<input type="text" readonly="readonly" autocomplete="off" placeholder="' + placeholder + '" class="layui-input layui-unselect">' +
  1118. '<span class="el-input__suffix">' +
  1119. '<span class="el-input__suffix-inner">' +
  1120. '<i class="el-icon-arrow-down ' + fromIcon + ' ' + downIcon + '" style="font-size: 16px;margin-right: 4px;"></i>' +
  1121. '</span></span>' +
  1122. '</div>')
  1123. this.$div.append(this.$input);
  1124. this.$inputRow = this.$input.find('.layui-input');
  1125. // 多选标签
  1126. if (multiple) {
  1127. this.$tags = $('<div class="el-cascader__tags"><!----></div>');
  1128. this.$div.append(this.$tags);
  1129. }
  1130. var element = this._initHideElement($e);
  1131. // 在后面插入元素
  1132. element.after(this.$div);
  1133. this.$icon = this.$input.find('i');
  1134. this._initFilterableInputEvent();
  1135. this.disabled(this.config.disabled);
  1136. },
  1137. /**
  1138. * 初始化隐藏元素input,主要用于layui的表单验证
  1139. * @param $e
  1140. * @private
  1141. */
  1142. _initHideElement: function ($e) {
  1143. var tagName = $e.prop("tagName").toLowerCase();
  1144. // 保存原始元素
  1145. if (tagName === 'input') {
  1146. $e.hide();
  1147. $e.attr('type', 'hidden')
  1148. this.$ec = $e;
  1149. return $e;
  1150. } else {
  1151. var attributes = $e[0].attributes;
  1152. var $input = $('<input />');
  1153. var keys = Object.keys(attributes);
  1154. for (var key in keys) {
  1155. var attribute = attributes[key];
  1156. $input.attr(attribute.name, attribute.value);
  1157. }
  1158. $input.hide();
  1159. $input.attr('type', 'hidden')
  1160. this.$ec = $input;
  1161. $e.before($input);
  1162. $e.remove();
  1163. return $input;
  1164. }
  1165. },
  1166. /**
  1167. * 初始化可搜索监听事件
  1168. * @private
  1169. */
  1170. _initFilterableInputEvent: function () {
  1171. var filterable = this.config.filterable;
  1172. if (!filterable) {
  1173. return;
  1174. }
  1175. var timeoutID;
  1176. var multiple = this.props.multiple;
  1177. var debounce = this.config.debounce;
  1178. var placeholder = this.config.placeholder;
  1179. var beforeFilter = this.config.beforeFilter;
  1180. var filterMethod = this.config.filterMethod;
  1181. var checkStrictly = this.props.checkStrictly;
  1182. var self = this;
  1183. function filter(event) {
  1184. var input = this;
  1185. if (timeoutID) {
  1186. clearTimeout(timeoutID);
  1187. }
  1188. timeoutID = setTimeout(function () {
  1189. timeoutID = null;
  1190. var val = $(input).val();
  1191. if (!val) {
  1192. self.isFiltering = false;
  1193. return;
  1194. }
  1195. self.open();
  1196. if (typeof beforeFilter === 'function' && beforeFilter(val)) {
  1197. self.isFiltering = true;
  1198. var nodes = self.getNodes();
  1199. var filterNodes = nodes.filter(function (node) {
  1200. var disabled;
  1201. if (checkStrictly) {
  1202. disabled = node.disabled;
  1203. } else {
  1204. disabled = node.path.some(function (node) {
  1205. return node.disabled;
  1206. });
  1207. }
  1208. if ((node.leaf || checkStrictly) && !disabled) {
  1209. if (typeof filterMethod === 'function' && filterMethod(node, val)) {
  1210. // 命中
  1211. return true;
  1212. }
  1213. }
  1214. return false;
  1215. });
  1216. self._setSuggestionMenu(filterNodes);
  1217. }
  1218. }, debounce);
  1219. }
  1220. if (multiple) {
  1221. // 多选可搜索
  1222. this.$tagsInput = $('<input type="text" autocomplete="off" placeholder="' + placeholder + '" class="el-cascader__search-input">');
  1223. var $tagsInput = this.$tagsInput;
  1224. this.$tags.append($tagsInput);
  1225. $tagsInput.on('keydown', filter);
  1226. $tagsInput.click(function (event) {
  1227. if (self.isFiltering) {
  1228. stopPropagation(event);
  1229. }
  1230. });
  1231. } else {
  1232. var $inputRow = this.$inputRow;
  1233. // 单选可搜索
  1234. $inputRow.removeAttr('readonly');
  1235. $inputRow.on('keydown', filter);
  1236. $inputRow.click(function (event) {
  1237. if (self.isFiltering) {
  1238. stopPropagation(event);
  1239. }
  1240. });
  1241. }
  1242. },
  1243. // 初始化面板(panel(1))
  1244. _initPanel: function () {
  1245. var $panel = this.$panel;
  1246. var popperClass = this.config.popperClass || '';
  1247. if (!$panel) {
  1248. // z-index:解决和layer.open默认19891016的冲突
  1249. this.$panel = $('<div class="el-popper el-cascader__dropdown ' + popperClass + '" style="position: absolute; z-index: 109891015;display: none;" x-placement="bottom-start"><div class="el-cascader-panel"></div><div class="popper__arrow" style="left: 35px;"></div></div>');
  1250. $panel = this.$panel;
  1251. $panel.appendTo('body');
  1252. $panel.click(function (event) {
  1253. // 阻止事件冒泡
  1254. stopPropagation(event);
  1255. });
  1256. // 初始化可搜索面板
  1257. this._initSuggestionPanel();
  1258. }
  1259. },
  1260. /**
  1261. * 添加菜单(panel(1)->menu(n))
  1262. * @param nodes 当前层级数据
  1263. * @param level 层级,从0开始
  1264. * @param parentNode 父级节点
  1265. * @param _menuItem 刷新时,传入的当前菜单的item数据
  1266. * @private
  1267. */
  1268. _appendMenu: function (nodes, level, parentNode, _menuItem) {
  1269. this._removeMenu(level);
  1270. if (parentNode && parentNode.leaf) {
  1271. return;
  1272. }
  1273. var menuData = this.data.menuData;
  1274. var $div = $('<div class="el-scrollbar el-cascader-menu" role="menu" id="cascader-menu"><div class="el-cascader-menu__wrap el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;"><ul class="el-scrollbar__view el-cascader-menu__list"></ul></div></div>');
  1275. // 重新添加菜单
  1276. this.$panel.find('.el-cascader-panel').append($div);
  1277. // 渲染细项
  1278. this._appendLi($div, nodes);
  1279. var menuItem = {nodes: nodes, level: level, parentNode: parentNode, scrollbar: {top: 0, left: 0}};
  1280. if (_menuItem) {
  1281. menuItem.scrollbar = _menuItem.scrollbar
  1282. }
  1283. // 渲染滚动条
  1284. this._initScrollbar($div, menuItem);
  1285. // 重新定位面板
  1286. this._resetXY();
  1287. menuData.push(menuItem);
  1288. },
  1289. /**
  1290. * 移除菜单
  1291. * @param level
  1292. * @private
  1293. */
  1294. _removeMenu: function (level) {
  1295. // 除了上一层的所有菜单全部移除
  1296. var number = level - 1;
  1297. if (number !== -1) {
  1298. this.$panel.find('.el-cascader-panel .el-cascader-menu:gt(' + number + ')').remove();
  1299. } else {
  1300. this.$panel.find('.el-cascader-panel .el-cascader-menu').remove();
  1301. }
  1302. // 保存菜单数据
  1303. var menuData = this.data.menuData;
  1304. if (menuData.length > level) {
  1305. menuData.splice(level, menuData.length - level);
  1306. }
  1307. },
  1308. /**
  1309. * 添加细项(panel(1)->menu(n)->li(n))
  1310. * @param $menu 当前菜单对象
  1311. * @param nodes 当前层级数据
  1312. * @private
  1313. */
  1314. _appendLi: function ($menu, nodes) {
  1315. var $list = $menu.find('.el-cascader-menu__list');
  1316. if (!nodes || nodes.length === 0) {
  1317. var isEmpty = this.config.empty;
  1318. $list.append('<div class="el-cascader-menu__empty-text">' + isEmpty + '</div>');
  1319. return;
  1320. }
  1321. $.each(nodes, function (index, node) {
  1322. node.bind($list);
  1323. });
  1324. },
  1325. /**
  1326. * 刷新菜单面板
  1327. */
  1328. refreshMenu: function () {
  1329. // 先复制一个数组,避免刷新菜单时,数组的数据被改变
  1330. var data = this.data.menuData.concat([]);
  1331. var self = this;
  1332. data.forEach(function (data) {
  1333. self._appendMenu(data.nodes, data.level, data.parentNode, data);
  1334. })
  1335. },
  1336. /**
  1337. * 初始化可搜索面板
  1338. * @private
  1339. */
  1340. _initSuggestionPanel: function () {
  1341. var filterable = this.config.filterable;
  1342. if (!filterable) {
  1343. return;
  1344. }
  1345. var $suggestionPanel = this.$suggestionPanel;
  1346. if (!$suggestionPanel) {
  1347. this.$suggestionPanel = $('<div class="el-cascader__suggestion-panel el-scrollbar" style="display: none;"><div class="el-scrollbar__wrap" style="margin-bottom: -17px; margin-right: -17px;"><ul class="el-scrollbar__view el-cascader__suggestion-list" style="min-width: 222px;"></ul></div></div>');
  1348. $suggestionPanel = this.$suggestionPanel;
  1349. this.$panel.find('.popper__arrow').before($suggestionPanel);
  1350. $suggestionPanel.click(function (event) {
  1351. // 阻止事件冒泡
  1352. stopPropagation(event);
  1353. });
  1354. }
  1355. },
  1356. /**
  1357. * 设置可搜索菜单
  1358. * @param nodes
  1359. * @private
  1360. */
  1361. _setSuggestionMenu: function (nodes) {
  1362. var $suggestionPanel = this.$suggestionPanel;
  1363. var $list = $suggestionPanel.find('.el-cascader__suggestion-list');
  1364. $list.empty();
  1365. $suggestionPanel.find('.el-scrollbar__bar').remove();
  1366. if (!nodes || nodes.length === 0) {
  1367. $list.append('<li class="el-cascader__empty-text">无匹配数据</li>');
  1368. return;
  1369. }
  1370. $.each(nodes, function (index, node) {
  1371. node.bindSuggestion($list);
  1372. });
  1373. this._initScrollbar($suggestionPanel, {scrollbar: {top: 0, left: 0}});
  1374. this._resetXY();
  1375. },
  1376. /**
  1377. * 初始化节点数据
  1378. * @param data 原始数据
  1379. * @param level 层级
  1380. * @param parentNode 父级节点
  1381. * @returns {*[]}
  1382. */
  1383. initNodes: function (data, level, parentNode) {
  1384. var nodes = [];
  1385. for (var key in data) {
  1386. var datum = data[key];
  1387. var node = new Node(datum, this, level, parentNode);
  1388. nodes.push(node);
  1389. if (node.children && node.children.length > 0) {
  1390. node.setChildren(this.initNodes(node.children, level + 1, node));
  1391. }
  1392. }
  1393. return nodes;
  1394. },
  1395. /**
  1396. * 设置单选值
  1397. * @param nodeId 节点id
  1398. * @param node 节点对象
  1399. * @private
  1400. */
  1401. _setActiveValue: function (nodeId, node) {
  1402. if (this.data.activeNodeId !== nodeId) {
  1403. var activeNode = this.data.activeNode;
  1404. this.data.activeNodeId = nodeId;
  1405. this.data.activeNode = node;
  1406. activeNode && activeNode.transferParent(function (node) {
  1407. node.syncStyle();
  1408. }, true);
  1409. node && node.transferParent(function (node) {
  1410. node.syncStyle();
  1411. }, true);
  1412. // 填充路径
  1413. this.change(node && node.value, node);
  1414. if (nodeId !== null) {
  1415. this._setClear();
  1416. }
  1417. }
  1418. },
  1419. /**
  1420. * 设置多选值
  1421. * @param nodeIds 值数组
  1422. * @param nodes 节点数组
  1423. * @private
  1424. */
  1425. _setCheckedValue: function (nodeIds, nodes) {
  1426. var checkedNodes = this.data.checkedNodes;
  1427. var maxSize = this.config.maxSize;
  1428. var maxSizeMode;
  1429. if (nodeIds.length > 0 && maxSize !== 0 && nodeIds.length >= maxSize) {
  1430. nodeIds = nodeIds.slice(0, maxSize);
  1431. nodes = nodes.slice(0, maxSize);
  1432. maxSizeMode = true
  1433. } else {
  1434. maxSizeMode = false
  1435. }
  1436. this.data.checkedNodeIds = nodeIds || [];
  1437. this.data.checkedNodes = nodes || [];
  1438. var syncPath = [];
  1439. var syncNodeIds = [];
  1440. checkedNodes.forEach(function (node) {
  1441. node.path.forEach(function (node) {
  1442. if (syncNodeIds.indexOf(node.nodeId) === -1) {
  1443. syncPath.push(node);
  1444. syncNodeIds.push(node.nodeId);
  1445. }
  1446. });
  1447. });
  1448. nodes.forEach(function (node) {
  1449. node.path.forEach(function (node) {
  1450. if (syncNodeIds.indexOf(node.nodeId) === -1) {
  1451. syncPath.push(node);
  1452. syncNodeIds.push(node.nodeId);
  1453. }
  1454. });
  1455. });
  1456. syncPath.forEach(function (node) {
  1457. node.syncStyle();
  1458. });
  1459. // 填充路径
  1460. this.change(nodes.map(function (node) {
  1461. return node.value;
  1462. }), nodes);
  1463. this._setClear();
  1464. this.maxSizeMode = maxSizeMode;
  1465. },
  1466. /**
  1467. * 设置值
  1468. * @param value
  1469. */
  1470. setValue: function (value) {
  1471. if (this.data.activeNodeId || this.data.checkedNodeIds.length > 0) {
  1472. // 清空值
  1473. this.clearCheckedNodes();
  1474. }
  1475. if (!value) {
  1476. return;
  1477. }
  1478. var strictMode = this.props.strictMode;
  1479. if (strictMode) {
  1480. if (!Array.isArray(value)) {
  1481. throw new Error("严格模式下,value必须是一个包含父子节点数组结构.");
  1482. }
  1483. }
  1484. var nodes = this.getNodes(this.data.nodes);
  1485. var checkStrictly = this.props.checkStrictly;
  1486. var multiple = this.props.multiple;
  1487. var disabledFixed = this.config.disabledFixed;
  1488. if (multiple) {
  1489. var paths = nodes.filter(function (node) {
  1490. if ((checkStrictly || node.leaf) && (!node.disabled || disabledFixed)) {
  1491. if (strictMode) {
  1492. // 严格模式下
  1493. // some:命中一个就为true
  1494. // every:全部命中为true
  1495. return value.some(function (levelValue) {
  1496. if (!Array.isArray(levelValue)) {
  1497. throw new Error("多选严格模式下,value必须是一个二维数组结构.");
  1498. }
  1499. var path = node.path;
  1500. return levelValue.length === path.length && levelValue.every(function (rowValue, index) {
  1501. return path[index].value === rowValue;
  1502. });
  1503. })
  1504. } else {
  1505. return value.indexOf(node.value) !== -1;
  1506. }
  1507. }
  1508. return false;
  1509. });
  1510. var nodeIds = paths.map(function (node) {
  1511. return node.nodeId;
  1512. });
  1513. this._setCheckedValue(nodeIds, paths);
  1514. // 展开第一个节点
  1515. if (paths.length > 0) {
  1516. var first = paths[0];
  1517. first.expandPanel();
  1518. }
  1519. } else {
  1520. for (var i = 0; i < nodes.length; i++) {
  1521. var node = nodes[i];
  1522. if ((checkStrictly || node.leaf)) {
  1523. var is = false;
  1524. if (strictMode) {
  1525. // 严格模式下
  1526. // every:全部命中为true
  1527. var path = node.path;
  1528. is = value.length === path.length && value.every(function (rowValue, index) {
  1529. return path[index].value === rowValue;
  1530. });
  1531. } else if (node.value === value) {
  1532. is = true;
  1533. }
  1534. if (is) {
  1535. this._setActiveValue(node.nodeId, node);
  1536. // 展开节点
  1537. node.expandPanel();
  1538. break;
  1539. }
  1540. }
  1541. }
  1542. }
  1543. },
  1544. /**
  1545. * 递归获取扁平的节点
  1546. * @param nodes
  1547. * @param container
  1548. * @returns {*[]}
  1549. */
  1550. getNodes: function (nodes, container) {
  1551. if (!container) {
  1552. container = [];
  1553. }
  1554. if (!nodes) {
  1555. nodes = this.data.nodes;
  1556. }
  1557. var self = this;
  1558. nodes.forEach(function (node) {
  1559. container.push(node);
  1560. var children = node.getChildren();
  1561. if (children) {
  1562. self.getNodes(children, container);
  1563. }
  1564. });
  1565. return container;
  1566. },
  1567. /**
  1568. * 初始化滚动条
  1569. * @param $menu 菜单的dom节点对象
  1570. * @param menuItem 当前菜单数据
  1571. * @private
  1572. */
  1573. _initScrollbar: function ($menu, menuItem) {
  1574. var $div = $('<div class="el-scrollbar__bar is-onhoriztal"><div class="el-scrollbar__thumb" style="transform: translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="transform: translateY(0%);"></div></div>');
  1575. $menu.append($div);
  1576. var vertical = $($div[1]).find('.el-scrollbar__thumb');
  1577. var onhoriztal = $($div[0]).find('.el-scrollbar__thumb');
  1578. var scrollbar = $menu.find('.el-scrollbar__wrap');
  1579. var $panel = this.$panel;
  1580. var $lis = $menu.find('li');
  1581. var height = Math.max($panel.height(), $menu.height());
  1582. var hScale = (height - 6) / ($lis.height() * $lis.length);
  1583. var wScale = $panel.width() / $lis.width();
  1584. // 滚动条监听事件
  1585. function _scrollbarEvent(scrollTop, scrollLeft) {
  1586. if (hScale < 1) {
  1587. vertical.css('height', hScale * 100 + '%');
  1588. vertical.css('transform', 'translateY(' + scrollTop / $menu.height() * 100 + '%)');
  1589. }
  1590. if (wScale < 1) {
  1591. onhoriztal.css('width', wScale * 100 + '%');
  1592. onhoriztal.css('transform', 'translateY(' + scrollLeft / $menu.width() * 100 + '%)');
  1593. }
  1594. }
  1595. // 拖动事件
  1596. vertical.mousedown(function (event) {
  1597. event.stopImmediatePropagation();
  1598. stopPropagation(event);
  1599. // 禁止文本选择事件
  1600. var selectstart = function () {
  1601. return false;
  1602. };
  1603. $(document).bind("selectstart", selectstart);
  1604. var y = event.clientY;
  1605. var scrollTop = scrollbar.scrollTop();
  1606. // 移动事件
  1607. var mousemove = function (event) {
  1608. event.stopImmediatePropagation();
  1609. var number = scrollTop + (event.clientY - y) / hScale;
  1610. scrollbar.scrollTop(number);
  1611. };
  1612. $(document).bind('mousemove', mousemove);
  1613. // 鼠标松开事件
  1614. $(document).one('mouseup', function (event) {
  1615. stopPropagation(event);
  1616. event.stopImmediatePropagation();
  1617. $(document).off('mousemove', mousemove);
  1618. $(document).off('selectstart', selectstart);
  1619. });
  1620. });
  1621. // 监听滚动条事件
  1622. scrollbar.scroll(function () {
  1623. var scroll = $(this);
  1624. menuItem.scrollbar.top = scroll.scrollTop()
  1625. menuItem.scrollbar.left = scroll.scrollLeft()
  1626. _scrollbarEvent(menuItem.scrollbar.top, menuItem.scrollbar.left);
  1627. });
  1628. // 初始化滚动条
  1629. scrollbar.scrollTop(menuItem.scrollbar.top);
  1630. _scrollbarEvent(menuItem.scrollbar.top, menuItem.scrollbar.left);
  1631. },
  1632. // 填充路径
  1633. _fillingPath: function () {
  1634. var multiple = this.props.multiple;
  1635. var showAllLevels = this.config.showAllLevels;
  1636. var separator = this.config.separator;
  1637. var collapseTags = this.config.collapseTags;
  1638. var $inputRow = this.$inputRow;
  1639. var placeholder = this.config.placeholder;
  1640. var self = this;
  1641. if (!multiple) {
  1642. var activeNode = this.data.activeNode;
  1643. var path = activeNode && activeNode.path || [];
  1644. if (showAllLevels) {
  1645. this._$inputRowSetValue(path.map(function (node) {
  1646. return node.label;
  1647. }).join(separator));
  1648. } else {
  1649. this._$inputRowSetValue(activeNode && activeNode.label || "");
  1650. }
  1651. } else {
  1652. // 复选框
  1653. // 删除标签
  1654. this.$tags.find('.el-tag').remove();
  1655. var $tagsInput = this.$tagsInput;
  1656. // 清除高度
  1657. $inputRow.css('height', '');
  1658. var checkedNodes = this.data.checkedNodes;
  1659. var minCollapseTagsNumber = Math.max(this.config.minCollapseTagsNumber, 1);
  1660. if (checkedNodes.length > 0) {
  1661. var tags = [];
  1662. var paths = checkedNodes;
  1663. if (collapseTags) {
  1664. // 折叠tags
  1665. paths = checkedNodes.slice(0, Math.min(checkedNodes.length, minCollapseTagsNumber));
  1666. }
  1667. paths.forEach(function (node) {
  1668. tags.push(node.$tag);
  1669. });
  1670. // 判断标签是否折叠
  1671. if (collapseTags) {
  1672. // 判断标签最小折叠数
  1673. if (checkedNodes.length > minCollapseTagsNumber) {
  1674. tags.push(self.get$tag('+ ' + (checkedNodes.length - minCollapseTagsNumber), false));
  1675. }
  1676. }
  1677. tags.forEach(function (tag) {
  1678. if ($tagsInput) {
  1679. $tagsInput.before(tag)
  1680. } else {
  1681. self.$tags.append(tag);
  1682. }
  1683. });
  1684. }
  1685. var tagHeight = self.$tags.height();
  1686. var inputHeight = $inputRow.height();
  1687. if (tagHeight > inputHeight) {
  1688. $inputRow.css('height', tagHeight + 4 + 'px');
  1689. }
  1690. // 重新定位
  1691. this._resetXY();
  1692. if (checkedNodes.length > 0) {
  1693. $inputRow.removeAttr('placeholder');
  1694. $tagsInput && $tagsInput.removeAttr('placeholder', placeholder);
  1695. } else {
  1696. $inputRow.attr('placeholder', placeholder);
  1697. $tagsInput && $tagsInput.attr('placeholder', placeholder);
  1698. }
  1699. }
  1700. },
  1701. /**
  1702. * 设置单选输入框的值
  1703. * @param label
  1704. * @private
  1705. */
  1706. _$inputRowSetValue: function (label) {
  1707. label = label || "";
  1708. var $inputRow = this.$inputRow;
  1709. $inputRow.attr('value', label); //防止被重置
  1710. $inputRow.val(label);
  1711. },
  1712. /**
  1713. * 获取复选框标签对象
  1714. * @param label
  1715. * @param showCloseIcon 是否显示关闭的icon
  1716. * @returns {jQuery|HTMLElement|*}
  1717. */
  1718. get$tag: function (label, showCloseIcon) {
  1719. var fromIcon = this.icons.from;
  1720. var closeIcon = this.icons.close;
  1721. var icon = showCloseIcon ? '<i class="el-tag__close el-icon-close ' + fromIcon + ' ' + closeIcon + '"></i>' : '';
  1722. return $('<span class="el-tag el-tag--info el-tag--small el-tag--light"><span>' + label + '</span>' + icon + '</span>');
  1723. },
  1724. // 设置可清理
  1725. _setClear: function () {
  1726. var self = this;
  1727. function enter() {
  1728. self.$icon.removeClass(self.icons.down);
  1729. self.$icon.addClass(self.icons.close);
  1730. }
  1731. function out() {
  1732. self.$icon.removeClass(self.icons.close);
  1733. self.$icon.addClass(self.icons.down);
  1734. }
  1735. self.$div.mouseenter(function () {
  1736. enter();
  1737. });
  1738. self.$div.mouseleave(function () {
  1739. out();
  1740. });
  1741. self.$icon.off('click');
  1742. var multiple = this.props.multiple;
  1743. var clear;
  1744. if (multiple) {
  1745. clear = this.data.checkedNodeIds.length > 0;
  1746. } else {
  1747. clear = !!this.data.activeNodeId;
  1748. }
  1749. if (clear && !this.config.disabled && this.config.clearable) {
  1750. self.$icon.one('click', function (event) {
  1751. stopPropagation(event);
  1752. self.close();
  1753. self.clearCheckedNodes();
  1754. out();
  1755. self.$icon.off('mouseenter');
  1756. self.$div.off('mouseenter');
  1757. self.$div.off('mouseleave');
  1758. });
  1759. } else {
  1760. out();
  1761. self.$icon.off('mouseenter');
  1762. self.$div.off('mouseenter');
  1763. self.$div.off('mouseleave');
  1764. }
  1765. },
  1766. // 禁用
  1767. disabled: function (isDisabled) {
  1768. this.config.disabled = !!isDisabled;
  1769. if (this.config.disabled) {
  1770. this.$div.addClass('is-disabled');
  1771. this.$div.find('.el-input--suffix').addClass('is-disabled');
  1772. this.$inputRow.attr('disabled', 'disabled');
  1773. this.$tagsInput && this.$tagsInput.attr('disabled', 'disabled').hide()
  1774. } else {
  1775. this.$div.removeClass('is-disabled');
  1776. this.$div.find('.el-input--suffix').removeClass('is-disabled');
  1777. this.$inputRow.removeAttr('disabled');
  1778. this.$tagsInput && this.$tagsInput.removeAttr('disabled').show();
  1779. }
  1780. // 重新设置是否可被清理
  1781. this._setClear();
  1782. // 重新填充路径
  1783. this._fillingPath();
  1784. },
  1785. /**
  1786. * 当选中节点变化时触发 选中节点的值
  1787. * @param value 值
  1788. * @param node 节点
  1789. */
  1790. change: function (value, node) {
  1791. var multiple = this.props.multiple;
  1792. if (multiple) {
  1793. if (value && value.length > 0) {
  1794. this.$ec.attr('value', JSON.stringify(value));
  1795. // this.$ec.val(JSON.stringify(value));
  1796. } else {
  1797. this.$ec.removeAttr('value');
  1798. // this.$ec.val('');
  1799. }
  1800. } else {
  1801. this.$ec.attr('value', value || "");
  1802. // this.$ec.val(value);
  1803. }
  1804. // 填充路径
  1805. this._fillingPath();
  1806. this.event.change.forEach(function (e) {
  1807. typeof e === 'function' && e(value, node)
  1808. })
  1809. },
  1810. /**
  1811. * 当失去焦点时触发 (event: Event)
  1812. * @param eventId 不为空时,必须与closeEventId值相等,防止旧事件触发
  1813. */
  1814. close: function (eventId) {
  1815. if (this.showPanel && (!eventId || this.closeEventId === eventId)) {
  1816. this.showPanel = false;
  1817. this.$div.find('.layui-icon-triangle-d').removeClass('is-reverse');
  1818. this.$panel.slideUp(100);
  1819. this.visibleChange(false);
  1820. // 聚焦颜色
  1821. this.$input.removeClass('is-focus');
  1822. // 可搜索
  1823. var filterable = this.config.filterable;
  1824. if (filterable) {
  1825. this.isFiltering = false;
  1826. this._fillingPath();
  1827. }
  1828. this.event.close.forEach(function (e) {
  1829. typeof e === 'function' && e()
  1830. })
  1831. }
  1832. },
  1833. /**
  1834. * 当获得焦点时触发 (event: Event)
  1835. */
  1836. open: function () {
  1837. if (!this.showPanel) {
  1838. this.showPanel = true;
  1839. this.closeEventId++;
  1840. var self = this;
  1841. // 当前传播事件结束后,添加点击背景关闭面板事件
  1842. setTimeout(function () {
  1843. $(document).one('click', self.close.bind(self, self.closeEventId));
  1844. });
  1845. // 重新定位面板
  1846. this._resetXY();
  1847. // 箭头icon翻转
  1848. this.$div.find('.layui-icon-triangle-d').addClass('is-reverse');
  1849. this.$panel.slideDown(200);
  1850. this.visibleChange(true);
  1851. // 聚焦颜色
  1852. this.$input.addClass('is-focus');
  1853. this.event.open.forEach(function (e) {
  1854. typeof e === 'function' && e()
  1855. })
  1856. }
  1857. },
  1858. /**
  1859. * 下拉框出现/隐藏时触发
  1860. * @param visible 出现则为 true,隐藏则为 false
  1861. */
  1862. visibleChange: function (visible) {
  1863. },
  1864. /**
  1865. * 在多选模式下,移除Tag时触发 移除的Tag对应的节点的值
  1866. * @param tagValue 节点的值
  1867. * @param node 节点对象
  1868. */
  1869. removeTag: function (tagValue, node) {
  1870. },
  1871. /**
  1872. * 获取选中的节点值
  1873. * @returns {null|[]}
  1874. */
  1875. getCheckedValues: function () {
  1876. var strictMode = this.props.strictMode;
  1877. if (this.props.multiple) {
  1878. var checkedNodes = this.data.checkedNodes;
  1879. if (strictMode) {
  1880. return checkedNodes.map(function (node) {
  1881. return node.path.map(function (node1) {
  1882. return node1.value;
  1883. });
  1884. });
  1885. }
  1886. return checkedNodes.map(function (node) {
  1887. return node.value;
  1888. });
  1889. } else {
  1890. var activeNode = this.data.activeNode;
  1891. if (strictMode) {
  1892. return activeNode && activeNode.path.map(function (node) {
  1893. return node.value;
  1894. })
  1895. }
  1896. return activeNode && activeNode.value;
  1897. }
  1898. },
  1899. /**
  1900. * 获取选中的节点
  1901. * @returns {null|[]}
  1902. */
  1903. getCheckedNodes: function () {
  1904. var strictMode = this.props.strictMode;
  1905. if (this.props.multiple) {
  1906. var checkedNodes = this.data.checkedNodes;
  1907. if (strictMode) {
  1908. return checkedNodes && checkedNodes.map(function (node) {
  1909. return node.path;
  1910. });
  1911. }
  1912. return checkedNodes;
  1913. } else {
  1914. var activeNode = this.data.activeNode;
  1915. if (strictMode) {
  1916. return activeNode && activeNode.path;
  1917. }
  1918. return activeNode;
  1919. }
  1920. },
  1921. /**
  1922. * 清空选中的节点
  1923. * @param force 强制清理禁用固定节点
  1924. */
  1925. clearCheckedNodes: function (force) {
  1926. var multiple = this.props.multiple;
  1927. if (multiple) {
  1928. var disabledFixed = this.config.disabledFixed;
  1929. if (!force && disabledFixed) {
  1930. //禁用项被固定,则过滤出禁用项的选值出来
  1931. var checkedNodes = this.data.checkedNodes;
  1932. var disNodes = checkedNodes.filter(function (node) {
  1933. return node.disabled;
  1934. });
  1935. var nodeIds = disNodes.map(function (node) {
  1936. return node.nodeId;
  1937. });
  1938. this._setCheckedValue(nodeIds, disNodes);
  1939. } else {
  1940. this._setCheckedValue([], []);
  1941. }
  1942. } else {
  1943. this._setActiveValue(null, null);
  1944. }
  1945. }
  1946. };
  1947. var thisCas = function () {
  1948. var self = this;
  1949. return {
  1950. /**
  1951. * 设置选项值
  1952. * @param options
  1953. */
  1954. setOptions: function (options) {
  1955. self.setOptions(options);
  1956. },
  1957. /**
  1958. * 覆盖当前值
  1959. * @param value 单选时传对象,多选时传数组
  1960. */
  1961. setValue: function (value) {
  1962. self.setValue(value);
  1963. },
  1964. /**
  1965. * 当节点变更时,执行回调
  1966. * @param callback function(value,node){}
  1967. */
  1968. changeEvent: function (callback) {
  1969. self.event.change.push(callback);
  1970. },
  1971. /**
  1972. * 当面板关闭时,执行回调
  1973. * @param callback function(){}
  1974. */
  1975. closeEvent: function (callback) {
  1976. self.event.close.push(callback);
  1977. },
  1978. /**
  1979. * 当面板打开时,执行回调
  1980. * @param callback function(){}
  1981. */
  1982. openEvent: function (callback) {
  1983. self.event.open.push(callback);
  1984. },
  1985. /**
  1986. * 禁用组件
  1987. * @param disabled true/false
  1988. */
  1989. disabled: function (disabled) {
  1990. self.disabled(disabled);
  1991. },
  1992. /**
  1993. * 收起面板
  1994. */
  1995. close: function () {
  1996. self.close();
  1997. },
  1998. /**
  1999. * 展开面板
  2000. */
  2001. open: function () {
  2002. self.open();
  2003. },
  2004. /**
  2005. * 获取选中的节点,如需获取路径,使用node.path获取,将获取各级节点的node对象
  2006. * @returns {[]|*}
  2007. */
  2008. getCheckedNodes: function () {
  2009. return self.getCheckedNodes();
  2010. },
  2011. /**
  2012. * 获取选中的值
  2013. * @returns {[]|*}
  2014. */
  2015. getCheckedValues: function () {
  2016. return self.getCheckedValues();
  2017. },
  2018. /**
  2019. * 清空选中的节点
  2020. * @param force 强制清理禁用固定节点
  2021. */
  2022. clearCheckedNodes: function (force) {
  2023. self.clearCheckedNodes(force);
  2024. },
  2025. /**
  2026. * 展开面板到节点所在的层级
  2027. * @param value 节点值,只能传单个值,不允许传数组
  2028. */
  2029. expandNode: function (value) {
  2030. var nodes = self.getNodes(self.data.nodes);
  2031. for (var i = 0; i < nodes.length; i++) {
  2032. var node = nodes[i];
  2033. if (node.value === value) {
  2034. node.expandPanel();
  2035. break;
  2036. }
  2037. }
  2038. },
  2039. /**
  2040. * 获取当前配置副本
  2041. */
  2042. getConfig: function () {
  2043. return $.extend(true, {}, self.config);
  2044. },
  2045. /**
  2046. * 获取数据对象副本
  2047. * @returns {*}
  2048. */
  2049. getData: function () {
  2050. return $.extend(true, {}, self.data);
  2051. }
  2052. };
  2053. };
  2054. exports('layCascader', function (option) {
  2055. var ins = new Cascader(option);
  2056. return thisCas.call(ins);
  2057. });
  2058. });