layui-xtree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //**********************************
  2. //** 本插件依赖 贤心 layui form模块
  3. //** 由 小巷 制作 QQ:151446298
  4. //** 版本3.0 时间 2018-01-10 20:48
  5. //**********************************
  6. //构造
  7. function layuiXtree(options) {
  8. var _this = this;
  9. _this._containerid = options.elem;
  10. _this.tool = options.tool;
  11. _this.checkbox = options.checkbox;
  12. typeof options.toolbar == 'function' && (_this.toolbar = options.toolbar);
  13. _this._container = document.getElementById(options.elem); //容器
  14. _this._container.style.minHeight = "100px";
  15. _this._options = options;
  16. _this.Loading(options);
  17. }
  18. //封装IE8 Class选择
  19. layuiXtree.prototype.getByClassName = function (cn) {
  20. if (document.getElementsByClassName) return this._container.getElementsByClassName(cn);
  21. var _xlist = this._container.childNodes;
  22. var _xtemp = new Array();
  23. for (var i = 0; i < _xlist.length; i++) {
  24. var _xchild = _xlist[i];
  25. var _xclassNames = _xchild.getAttribute('class').split(' ');
  26. for (var j = 0; j < _xclassNames.length; j++) {
  27. if (_xclassNames[j] == cn) {
  28. _xtemp.push(_xchild);
  29. break;
  30. }
  31. }
  32. }
  33. return _xtemp;
  34. }
  35. //在一个对象下面找子级
  36. layuiXtree.prototype.getChildByClassName = function (obj, cn) {
  37. var _xlist = obj.childNodes;
  38. var _xtemp = new Array();
  39. for (var i = 0; i < _xlist.length; i++) {
  40. var _xchild = _xlist[i];
  41. var _xclassNames = _xchild.getAttribute('class').split(' ');
  42. for (var j = 0; j < _xclassNames.length; j++) {
  43. if (_xclassNames[j] == cn) {
  44. _xtemp.push(_xchild);
  45. break;
  46. }
  47. }
  48. }
  49. return _xtemp;
  50. }
  51. //加载特效,且获取数据
  52. layuiXtree.prototype.Loading = function (options) {
  53. var _this = this;
  54. _this.xloading = document.createElement("span"); //创建加载对象
  55. _this.xloading.setAttribute('class', 'layui-icon layui-anim layui-anim-rotate layui-anim-loop');
  56. _this.xloading.innerHTML = '&#xe63e;';
  57. _this.xloading.style.fontSize = "50px";
  58. _this.xloading.style.color = "#009688";
  59. _this.xloading.style.fontWeight = "bold";
  60. _this.xloading.style.marginLeft = _this._container.offsetWidth / 2 - 25 + 'px';
  61. _this.xloading.style.marginTop = _this._container.offsetHeight / 2 - 50 + 'px';
  62. _this._container.innerHTML = '';
  63. _this._container.appendChild(_this.xloading); //加载显示
  64. if (typeof (options.data) == 'object') {
  65. _this._dataJson = options.data;
  66. _this.Initial(options);
  67. return;
  68. }
  69. //如果是字符串url,进行异步加载
  70. var obj = new XMLHttpRequest();
  71. obj.open('get', options.data, true);
  72. obj.onreadystatechange = function () {
  73. if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { //回调成功
  74. if (JSON.parse(obj.responseText).menu) {
  75. _this._dataJson = eval('(' + obj.responseText + ')').menu;
  76. //_this._dataJson = eval('(' + JSON.parse(obj.responseText).menu + ')'); //将返回的数据转为json
  77. }
  78. _this.Initial(options);
  79. }
  80. };
  81. obj.send();
  82. }
  83. //data验证后的数据初始化
  84. layuiXtree.prototype.Initial = function (o) {
  85. var _this = this;
  86. _this._form = o.form; //layui from对象
  87. _this._domStr = ""; //结构字符串
  88. _this._isopen = o.isopen != null ? o.isopen : true;
  89. if (o.color == null) o.color = {
  90. open: '#2F4056',
  91. close: '#2F4056',
  92. end: '#2F4056'
  93. }; //图标颜色
  94. _this._iconOpenColor = o.color.open != null ? o.color.open : "#2F4056";
  95. _this._iconCloseColor = o.color.close != null ? o.color.close : "#2F4056";
  96. _this._iconEndColor = o.color.end != null ? o.color.end : "#2F4056";
  97. if (o.icon == null) o.icon = {
  98. open: '&#xe625;',
  99. close: '&#xe623;',
  100. end: '&#xe621;'
  101. }; //图标样式
  102. _this._iconOpen = o.icon.open != null ? o.icon.open : '&#xe625;';
  103. _this._iconClose = o.icon.close != null ? o.icon.close : '&#xe623;';
  104. _this._iconEnd = o.icon.end != null ? o.icon.end : '&#xe621;';
  105. _this._click = o.click != null ? o.click : function () {};
  106. _this._ckall = o.ckall != null ? o.ckall : false; //全选是否启用
  107. _this._ckallSuccess = o.ckallback != null ? o.ckallback : function () {}; //全选回调
  108. _this.CreateCkAll();
  109. _this.dataBind(_this._dataJson);
  110. _this.Rendering();
  111. }
  112. //全选框
  113. layuiXtree.prototype.CreateCkAll = function () {
  114. var _this = this;
  115. if (_this._ckall) {
  116. _this._domStr += '<div class="layui-xtree-item">';
  117. _this._domStr +=
  118. '<input type="checkbox" class="layui-xtree-checkbox layui-xtree-ckall" title="全选" lay-skin="primary" lay-filter="xtreeckall' +
  119. _this._containerid + '">';
  120. _this._domStr += '</div>';
  121. }
  122. }
  123. //生产结构
  124. layuiXtree.prototype.dataBind = function (d) {
  125. var _this = this;
  126. if (d.length > 0) {
  127. for (i in d) {
  128. var xtree_isend = '';
  129. var xtree_ischecked = '';
  130. var xtree_isdisabled = d[i].disabled ? ' disabled="disabled" ' : '';
  131. _this._domStr += '<div class="layui-xtree-item">';
  132. if (!d[i].childrenMenu) {
  133. d[i].childrenMenu = [];
  134. }
  135. if (d[i].childrenMenu.length > 0)
  136. _this._domStr += '<i class="layui-icon layui-xtree-icon" data-xtree="' + (_this._isopen ? '1' : '0') +
  137. '">' + (_this._isopen ? _this._iconOpen : _this._iconClose) + '</i>';
  138. else {
  139. _this._domStr += '<i class="layui-icon layui-xtree-icon-null">' + (d[i].icon || _this._iconEnd) +
  140. '</i>';
  141. xtree_isend = 'data-xend="1"';
  142. xtree_ischecked = !!d[i].roleId ? ' checked ' : '';
  143. xtree_isdisabled = d[i].disabled ? ' disabled="disabled" ' : '';
  144. }
  145. if (!!_this.checkbox) {
  146. _this._domStr += '<input type="checkbox" class="layui-xtree-checkbox" ' + xtree_isend +
  147. xtree_ischecked +
  148. xtree_isdisabled + ' value="' + d[i].id + '" title="' + d[i].name +
  149. '" lay-skin="primary" lay-filter="xtreeck' + _this._containerid + '">';
  150. }else{
  151. _this._domStr += '<div class="layui-inline" style="margin-top: 10px;margin-bottom:0;"><span>'+d[i].name+'</span></div>';
  152. }
  153. if (!!_this.tool) {
  154. _this._domStr +=
  155. '<div class="layui-inline" style="margin-top: 10px;margin-bottom:0;"><a class="layui-btn layui-btn-xs" name="tree_event" event="edit"id="' +
  156. d[i].id +
  157. '">修改</a><a class="layui-btn layui-btn-danger layui-btn-xs" name="tree_event" event="del" id="' +
  158. d[i].id + '">删除</a></div>';
  159. }
  160. _this.dataBind(d[i].childrenMenu);
  161. _this._domStr += '</div>';
  162. }
  163. }
  164. }
  165. //工具栏监听回调
  166. layuiXtree.prototype.toolbar = function (obj) {
  167. }
  168. //通过ID获取
  169. layuiXtree.prototype.idGetObj = function (id) {
  170. var _this = this;
  171. var obj = {};
  172. (function get(d) {
  173. if (d.length > 0) {
  174. for (i in d) {
  175. if (!d[i].childrenMenu) {
  176. d[i].childrenMenu = [];
  177. }
  178. if (d[i].id == id) {
  179. obj = d[i];
  180. }
  181. get(d[i].childrenMenu);
  182. }
  183. }
  184. })(_this._dataJson);
  185. return obj;
  186. }
  187. //渲染呈现
  188. layuiXtree.prototype.Rendering = function () {
  189. var _this = this;
  190. _this._container.innerHTML = _this._domStr;
  191. _this._domStr = "";
  192. //检查选中状态
  193. var xtree_ckitems = _this.getByClassName('layui-xtree-checkbox');
  194. for (var i = 0; i < xtree_ckitems.length; i++) {
  195. if (xtree_ckitems[i].getAttribute('data-xend') == '1' && xtree_ckitems[i].checked) {
  196. _this.ParentCheckboxChecked(xtree_ckitems[i]);
  197. }
  198. }
  199. _this._form.render('checkbox'); //layui渲染
  200. //绑定工具条事件
  201. var tree_event = document.getElementsByName('tree_event');
  202. for (var tr_i in tree_event) {
  203. tree_event[tr_i].onclick = function (e) {
  204. _this.toolbar({
  205. event: this.getAttribute('event'),
  206. data: _this.idGetObj(this.id)
  207. });
  208. };
  209. }
  210. var xtree_items = _this.getByClassName('layui-xtree-item');
  211. var xtree_icons = _this.getByClassName('layui-xtree-icon');
  212. var xtree_nullicons = _this.getByClassName('layui-xtree-icon-null');
  213. for (var i = 0; i < xtree_items.length; i++) {
  214. if (xtree_items[i].parentNode == _this._container)
  215. xtree_items[i].style.margin = '5px 0 0 10px';
  216. else {
  217. xtree_items[i].style.margin = '5px 0 0 45px';
  218. if (!_this._isopen) xtree_items[i].style.display = 'none';
  219. }
  220. }
  221. for (var i = 0; i < xtree_icons.length; i++) {
  222. xtree_icons[i].style.position = "relative";
  223. xtree_icons[i].style.top = "3px";
  224. xtree_icons[i].style.margin = "0 5px 0 0";
  225. xtree_icons[i].style.fontSize = "18px";
  226. xtree_icons[i].style.color = _this._isopen ? _this._iconOpenColor : _this._iconCloseColor;
  227. xtree_icons[i].style.cursor = "pointer";
  228. xtree_icons[i].onclick = function () {
  229. var xtree_chi = this.parentNode.childNodes;
  230. if (this.getAttribute('data-xtree') == 1) {
  231. for (var j = 0; j < xtree_chi.length; j++) {
  232. if (xtree_chi[j].getAttribute('class') == 'layui-xtree-item')
  233. xtree_chi[j].style.display = 'none';
  234. }
  235. this.setAttribute('data-xtree', '0')
  236. this.innerHTML = _this._iconClose;
  237. this.style.color = _this._iconCloseColor;
  238. } else {
  239. for (var j = 0; j < xtree_chi.length; j++) {
  240. if (xtree_chi[j].getAttribute('class') == 'layui-xtree-item')
  241. xtree_chi[j].style.display = 'block';
  242. }
  243. this.setAttribute('data-xtree', '1')
  244. this.innerHTML = _this._iconOpen;
  245. this.style.color = _this._iconOpenColor;
  246. }
  247. }
  248. }
  249. for (var i = 0; i < xtree_nullicons.length; i++) {
  250. xtree_nullicons[i].style.position = "relative";
  251. xtree_nullicons[i].style.top = "3px";
  252. xtree_nullicons[i].style.margin = "0 5px 0 0";
  253. xtree_nullicons[i].style.fontSize = "18px";
  254. xtree_nullicons[i].style.color = _this._iconEndColor;
  255. }
  256. _this._form.on('checkbox(xtreeck' + _this._containerid + ')', function (da) {
  257. //获取当前点击复选框的容器下面的所有子级容器
  258. var xtree_chis = da.elem.parentNode.getElementsByClassName('layui-xtree-item');
  259. //遍历它们,选中状态与它们的父级一致(类似全选功能)
  260. for (var i = 0; i < xtree_chis.length; i++) {
  261. if (!_this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].disabled) {
  262. _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].checked = da.elem.checked;
  263. if (da.elem.checked) _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].nextSibling
  264. .classList.add('layui-form-checked');
  265. else _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].nextSibling.classList
  266. .remove('layui-form-checked');
  267. }
  268. }
  269. _this.ParendCheck(da.elem);
  270. _this._click(da);
  271. });
  272. var _xtree_disableds = _this.getByClassName('layui-disabled');
  273. for (var i = 0; i < _xtree_disableds.length; i++) {
  274. _xtree_disableds[i].getElementsByTagName('span')[0].style.color = "#B5B5B5";
  275. }
  276. //全选按钮
  277. if (_this._ckall) {
  278. _this._form.on('checkbox(xtreeckall' + _this._containerid + ')', function (data) {
  279. var xtree_allck = data.elem.parentNode.parentNode.getElementsByClassName('layui-form-checkbox');
  280. for (var i = 0; i < xtree_allck.length; i++) {
  281. if (xtree_allck[i].getAttribute('class').indexOf('layui-checkbox-disbaled') == -1) {
  282. if (data.elem.checked) {
  283. xtree_allck[i].classList.add('layui-form-checked');
  284. } else {
  285. xtree_allck[i].classList.remove('layui-form-checked');
  286. }
  287. xtree_allck[i].parentNode.getElementsByClassName('layui-xtree-checkbox')[0].checked =
  288. data.elem.checked;
  289. }
  290. }
  291. _this._ckallSuccess();
  292. });
  293. }
  294. }
  295. //更新渲染
  296. layuiXtree.prototype.render = function () {
  297. var _this = this;
  298. _this.Loading(_this._options);
  299. }
  300. //子节点选中改变,父节点更改自身状态
  301. layuiXtree.prototype.ParendCheck = function (ckelem) {
  302. var _this = this;
  303. var xtree_p = ckelem.parentNode.parentNode;
  304. if (xtree_p.getAttribute('class') == 'layui-xtree-item') {
  305. var xtree_all = _this.getChildByClassName(xtree_p, 'layui-xtree-item');
  306. var xtree_count = 0;
  307. for (var i = 0; i < xtree_all.length; i++) {
  308. if (_this.getChildByClassName(xtree_all[i], 'layui-xtree-checkbox')[0].checked) {
  309. xtree_count++;
  310. }
  311. }
  312. if (xtree_count <= 0) {
  313. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = false;
  314. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.remove(
  315. 'layui-form-checked');
  316. } else {
  317. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = true;
  318. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.add(
  319. 'layui-form-checked');
  320. }
  321. this.ParendCheck(_this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0]);
  322. }
  323. }
  324. //渲染之前按照选中的末级去改变父级选中状态
  325. layuiXtree.prototype.ParentCheckboxChecked = function (e) {
  326. var _this = this;
  327. if (e.parentNode.parentNode.getAttribute('class') == 'layui-xtree-item') {
  328. var _pe = _this.getChildByClassName(e.parentNode.parentNode, 'layui-xtree-checkbox')[0];
  329. _pe.checked = true;
  330. _this.ParentCheckboxChecked(_pe);
  331. }
  332. }
  333. //获取全部选中的末级checkbox对象
  334. layuiXtree.prototype.GetChecked = function () {
  335. var _this = this;
  336. var arr = new Array();
  337. var arrIndex = 0;
  338. var cks = _this.getByClassName('layui-xtree-checkbox');
  339. for (var i = 0; i < cks.length; i++) {
  340. if (cks[i].checked && cks[i].getAttribute('data-xend') == '1') {
  341. arr[arrIndex] = cks[i];
  342. arrIndex++;
  343. }
  344. }
  345. return arr;
  346. }
  347. //获取全部选中的checkbox对象value
  348. layuiXtree.prototype.GetCheckedVal = function () {
  349. var _this = this;
  350. var arr = new Array();
  351. var arrIndex = 0;
  352. var cks = _this.getByClassName('layui-xtree-checkbox');
  353. for (var i = 0; i < cks.length; i++) {
  354. if (cks[i].checked) {
  355. arr[arrIndex] = cks[i].value;
  356. arrIndex++;
  357. }
  358. }
  359. return arr;
  360. }
  361. //获取全部的原始checkbox对象
  362. layuiXtree.prototype.GetAllCheckBox = function () {
  363. var _this = this;
  364. var arr = new Array();
  365. var arrIndex = 0;
  366. var cks = _this.getByClassName('layui-xtree-checkbox');
  367. for (var i = 0; i < cks.length; i++) {
  368. arr[arrIndex] = cks[i];
  369. arrIndex++;
  370. }
  371. return arr;
  372. }
  373. //根据值来获取其父级的checkbox原dom对象
  374. layuiXtree.prototype.GetParent = function (a) {
  375. var _this = this;
  376. var cks = _this.getByClassName('layui-xtree-checkbox');
  377. for (var i = 0; i < cks.length; i++) {
  378. if (cks[i].value == a) {
  379. if (cks[i].parentNode.parentNode.getAttribute('id') == _this._container.getAttribute('id')) return null;
  380. return _this.getChildByClassName(cks[i].parentNode.parentNode, 'layui-xtree-checkbox')[0];
  381. }
  382. }
  383. return null;
  384. }