UU跑腿标准版

ligerPopupEdit.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * jQuery ligerUI 1.2.4
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2014 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. $.fn.ligerPopupEdit = function (options)
  12. {
  13. return $.ligerui.run.call(this, "ligerPopupEdit", arguments);
  14. };
  15. $.fn.ligerGetPopupEditManager = function ()
  16. {
  17. return $.ligerui.run.call(this, "ligerGetPopupEditManager", arguments);
  18. };
  19. $.ligerDefaults.PopupEdit = {
  20. valueFieldID: null, //生成的value input:hidden 字段名
  21. css: null, //附加css
  22. onButtonClick: null, //利用这个参数来调用其他函数,比如打开一个新窗口来选择值
  23. nullText: null, //不能为空时的提示
  24. disabled: false, //是否无效
  25. cancelable: true,
  26. width: 200,
  27. heigth: null,
  28. render: null, //显示函数
  29. split: ';',
  30. grid: null, //在 可查询、可分页列表的弹出框 中选择值
  31. condition: null, // 条件字段,比如 {fields:[{ name : 'Title' ,op : 'like', vt : 'string',type:'text' }]}
  32. valueField: 'id', //值字段
  33. textField: 'text', //显示字段
  34. parms: null,
  35. onSelect: null, //选择事件,可阻止
  36. onSelected: null, //选择后事件
  37. valueFieldCssClass : null
  38. };
  39. //扩展方法
  40. $.ligerMethos.PopupEdit = $.ligerMethos.PopupEdit || {};
  41. $.ligerui.controls.PopupEdit = function (element, options)
  42. {
  43. $.ligerui.controls.PopupEdit.base.constructor.call(this, element, options);
  44. };
  45. $.ligerui.controls.PopupEdit.ligerExtend($.ligerui.controls.Input, {
  46. __getType: function ()
  47. {
  48. return 'PopupEdit';
  49. },
  50. _extendMethods: function ()
  51. {
  52. return $.ligerMethos.PopupEdit;
  53. },
  54. _init: function ()
  55. {
  56. $.ligerui.controls.PopupEdit.base._init.call(this);
  57. },
  58. _render: function ()
  59. {
  60. var g = this, p = this.options;
  61. g.inputText = null;
  62. //文本框初始化
  63. if (this.element.tagName.toLowerCase() == "input")
  64. {
  65. this.element.readOnly = true;
  66. g.inputText = $(this.element);
  67. g.textFieldID = this.element.id;
  68. }
  69. if (g.inputText[0].name == undefined) g.inputText[0].name = g.textFieldID;
  70. //隐藏域初始化
  71. g.valueField = null;
  72. if (p.valueFieldID)
  73. {
  74. g.valueField = $("#" + p.valueFieldID + ":input");
  75. if (g.valueField.length == 0) g.valueField = $('<input type="hidden"/>');
  76. g.valueField[0].id = g.valueField[0].name = p.valueFieldID;
  77. }
  78. else
  79. {
  80. g.valueField = $('<input type="hidden"/>');
  81. g.valueField[0].id = g.valueField[0].name = g.textFieldID + "_val";
  82. }
  83. if (g.valueField[0].name == undefined) g.valueField[0].name = g.valueField[0].id;
  84. if (p.valueFieldCssClass)
  85. {
  86. g.valueField.addClass(p.valueFieldCssClass);
  87. }
  88. //开关
  89. g.link = $('<div class="l-trigger"><div class="l-trigger-icon"></div></div>');
  90. //外层
  91. g.wrapper = g.inputText.wrap('<div class="l-text l-text-popup"></div>').parent();
  92. g.wrapper.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  93. g.wrapper.append(g.link);
  94. g.wrapper.append(g.valueField);
  95. g.inputText.addClass("l-text-field");
  96. //开关 事件
  97. g.link.hover(function ()
  98. {
  99. if (p.disabled) return;
  100. this.className = "l-trigger-hover";
  101. }, function ()
  102. {
  103. if (p.disabled) return;
  104. this.className = "l-trigger";
  105. }).mousedown(function ()
  106. {
  107. if (p.disabled) return;
  108. this.className = "l-trigger-pressed";
  109. }).mouseup(function ()
  110. {
  111. if (p.disabled) return;
  112. this.className = "l-trigger-hover";
  113. }).click(function ()
  114. {
  115. if (p.disabled) return;
  116. if (g.trigger('buttonClick') == false) return false;
  117. });
  118. g.inputText.click(function ()
  119. {
  120. if (p.disabled) return;
  121. }).blur(function ()
  122. {
  123. if (p.disabled) return;
  124. g.wrapper.removeClass("l-text-focus");
  125. }).focus(function ()
  126. {
  127. if (p.disabled) return;
  128. g.wrapper.addClass("l-text-focus");
  129. });
  130. g.wrapper.hover(function ()
  131. {
  132. if (p.disabled) return;
  133. g.wrapper.addClass("l-text-over");
  134. }, function ()
  135. {
  136. if (p.disabled) return;
  137. g.wrapper.removeClass("l-text-over");
  138. });
  139. g.set(p);
  140. },
  141. destroy: function ()
  142. {
  143. if (this.wrapper) this.wrapper.remove();
  144. this.options = null;
  145. $.ligerui.remove(this);
  146. },
  147. clear: function ()
  148. {
  149. var g = this, p = this.options;
  150. g.inputText.val("");
  151. g.valueField.val("");
  152. },
  153. _setCss: function (css)
  154. {
  155. if (css)
  156. {
  157. this.wrapper.addClass(css);
  158. }
  159. },
  160. //取消选择
  161. _setCancelable: function (value)
  162. {
  163. var g = this, p = this.options;
  164. if (!value && g.unselect)
  165. {
  166. g.unselect.remove();
  167. g.unselect = null;
  168. }
  169. if (!value && !g.unselect) return;
  170. g.unselect = $('<div class="l-trigger l-trigger-cancel"><div class="l-trigger-icon"></div></div>').hide();
  171. g.wrapper.hover(function ()
  172. {
  173. g.unselect.show();
  174. }, function ()
  175. {
  176. g.unselect.hide();
  177. })
  178. if (!p.disabled && p.cancelable)
  179. {
  180. g.wrapper.append(g.unselect);
  181. }
  182. g.unselect.hover(function ()
  183. {
  184. this.className = "l-trigger-hover l-trigger-cancel";
  185. }, function ()
  186. {
  187. this.className = "l-trigger l-trigger-cancel";
  188. }).click(function ()
  189. {
  190. g.clear();
  191. });
  192. },
  193. _setDisabled: function (value)
  194. {
  195. if (value)
  196. {
  197. this.wrapper.addClass('l-text-disabled');
  198. } else
  199. {
  200. this.wrapper.removeClass('l-text-disabled');
  201. }
  202. },
  203. _setWidth: function (value)
  204. {
  205. var g = this;
  206. if (value > 20)
  207. {
  208. g.wrapper.css({ width: value });
  209. g.inputText.css({ width: value - 20 });
  210. }
  211. },
  212. _setHeight: function (value)
  213. {
  214. var g = this;
  215. if (value > 10)
  216. {
  217. g.wrapper.height(value);
  218. g.inputText.height(value - 2);
  219. }
  220. },
  221. getData : function()
  222. {
  223. var g = this, p = this.options;
  224. var data = [];
  225. var v = $(g.valueField).val(), t = $(g.inputText).val();
  226. var values = v ? v.split(p.split) : null, texts = t ? t.split(p.split) : null;
  227. $(values).each(function (i)
  228. {
  229. var o = {};
  230. o[p.textField] = texts[i];
  231. o[p.valueField] = values[i];
  232. data.push(o);
  233. });
  234. return data;
  235. },
  236. _getText: function ()
  237. {
  238. return $(this.inputText).val();
  239. },
  240. _getValue: function ()
  241. {
  242. return $(this.valueField).val();
  243. },
  244. getValue: function ()
  245. {
  246. return this._getValue();
  247. },
  248. getText: function ()
  249. {
  250. return this._getText();
  251. },
  252. //设置值到 隐藏域
  253. setValue: function (value, text)
  254. {
  255. var g = this, p = this.options;
  256. if (arguments.length >= 2)
  257. {
  258. g.setValue(value);
  259. g.setText(text);
  260. return;
  261. }
  262. g.valueField.val(value);
  263. },
  264. //设置值到 文本框
  265. setText: function (text)
  266. {
  267. var g = this, p = this.options;
  268. if (p.render)
  269. {
  270. g.inputText.val(p.render(text));
  271. }
  272. else
  273. {
  274. g.inputText.val(text);
  275. }
  276. },
  277. addValue: function (value, text)
  278. {
  279. var g = this, p = this.options;
  280. if (!value) return;
  281. var v = g.getValue(), t = g.getText();
  282. if (!v)
  283. {
  284. g.setValue(value);
  285. g.setText(text);
  286. } else
  287. {
  288. var arrV = [], arrT = [], old = v.split(p.split), value = value.split(p.split), text = text.split(p.split);
  289. for (var i = 0, l = value.length; i < l; i++)
  290. {
  291. if ($.inArray(value[i], old) == -1)
  292. {
  293. arrV.push(value[i]);
  294. arrT.push(text[i]);
  295. }
  296. }
  297. if (arrV.length)
  298. {
  299. g.setValue(v + p.split + arrV.join(p.split));
  300. g.setText(t + p.split + arrT.join(p.split));
  301. }
  302. }
  303. },
  304. removeValue: function (value, text)
  305. {
  306. var g = this, p = this.options;
  307. if (!value) return;
  308. var v = g.getValue(), t = g.getText();
  309. if (!v) return;
  310. var oldV = v.split(p.split), oldT = t.split(p.split), value = value.split(p.split);
  311. for (var i = 0, index = -1, l = value.length; i < l; i++)
  312. {
  313. if ((index = $.inArray(value[i], oldV)) != -1)
  314. {
  315. oldV.splice(index, 1);
  316. oldT.splice(index, 1);
  317. }
  318. }
  319. g.setValue(oldV.join(p.split));
  320. g.setText(oldT.join(p.split));
  321. },
  322. _setGrid: function (value)
  323. {
  324. if (!value) return;
  325. var g = this, p = this.options;
  326. var gridOptions = $.extend({
  327. parms: p.parms
  328. }, p.grid);
  329. this.bind('buttonClick', function ()
  330. {
  331. if (!g.popupFn)
  332. {
  333. var options = {
  334. grid: gridOptions,
  335. condition: p.condition,
  336. valueField: p.valueField,
  337. textField: p.textField,
  338. split: p.split,
  339. lastSelected : g.getData(),
  340. onSelect: function (e)
  341. {
  342. if (g.trigger('select', e) == false) return;
  343. if (p.grid.checkbox)
  344. {
  345. g.addValue(e.value, e.text);
  346. g.removeValue(e.remvoeValue, e.remvoeText);
  347. } else
  348. {
  349. g.setValue(e.value);
  350. g.setText(e.text);
  351. }
  352. g.trigger('selected', e);
  353. },
  354. selectInit: function (rowdata)
  355. {
  356. var value = g.getValue();
  357. if (!value) return false;
  358. if (!p.valueField || !rowdata[p.valueField]) return false;
  359. return $.inArray(rowdata[p.valueField].toString(), value.split(p.split)) != -1;
  360. }
  361. };
  362. g.popupFn = $.ligerui.getPopupFn(options);
  363. }
  364. g.popupFn();
  365. });
  366. }
  367. });
  368. //创建一个可查询、可分页列表的选取弹出框 需要dialog,grid,form等插件的支持
  369. $.ligerui.getPopupFn = function (p)
  370. {
  371. p = $.extend({
  372. title: '选择数据', //窗口标题
  373. width: 700, //窗口宽度
  374. height: 320, //列表高度
  375. top: null,
  376. left: null,
  377. split: ';',
  378. valueField: null, //接收表格的value字段名
  379. textField: null, //接收表格的text字段名
  380. grid: null, //表格的参数 同ligerGrid
  381. condition: null, //搜索表单的参数 同ligerForm
  382. onSelect: function (p) { }, //选取函数
  383. selectInit: function (rowdata) { return false } //选择初始化
  384. }, p);
  385. if (!p.grid) return;
  386. var win, grid, condition, lastSelected = p.lastSelected || [];
  387. return function ()
  388. {
  389. show();
  390. return false;
  391. };
  392. function show()
  393. {
  394. function getGridHeight(height)
  395. {
  396. height = height || p.height;
  397. height -= conditionPanel.height();
  398. return height;
  399. }
  400. if (win)
  401. {
  402. grid._showData();
  403. win.show();
  404. grid.refreshSize();
  405. lastSelected = grid.selected.concat();
  406. return;
  407. }
  408. var panle = $("<div></div>");
  409. var conditionPanel = $("<div></div>");
  410. var gridPanel = $("<div></div>");
  411. panle.append(conditionPanel).append(gridPanel);
  412. if (p.condition)
  413. {
  414. var conditionParm = $.extend({
  415. labelWidth: 60,
  416. space: 20
  417. }, p.condition);
  418. condition = conditionPanel.ligerForm(conditionParm);
  419. } else
  420. {
  421. conditionPanel.remove();
  422. }
  423. var gridParm = $.extend({
  424. columnWidth: 120,
  425. alternatingRow: false,
  426. frozen: true,
  427. rownumbers: true
  428. }, p.grid, {
  429. width: "100%",
  430. height: getGridHeight(),
  431. isChecked: p.selectInit,
  432. isSelected: p.selectInit,
  433. inWindow: false
  434. });
  435. //grid
  436. grid = gridPanel.ligerGrid(gridParm);
  437. //搜索按钮
  438. if (p.condition)
  439. {
  440. var containerBtn1 = $('<li style="margin-right:9px"><div></div></li>');
  441. $("ul:first", conditionPanel).append(containerBtn1).after('<div class="l-clear"></div>');
  442. $("div", containerBtn1).ligerButton({
  443. text: '搜索',
  444. click: function ()
  445. {
  446. var rules = $.ligerui.getConditions(conditionPanel);
  447. grid.setParm('condition', $.ligerui.toJSON(rules));
  448. grid.reload();
  449. }
  450. });
  451. }
  452. //dialog
  453. win = $.ligerDialog.open({
  454. title: p.title,
  455. width: p.width,
  456. height: 'auto',
  457. top: p.top,
  458. left: p.left,
  459. target: panle,
  460. isResize: true,
  461. cls: 'l-selectorwin',
  462. onContentHeightChange: function (height)
  463. {
  464. grid.set('height', getGridHeight(height));
  465. return false;
  466. },
  467. onStopResize: function ()
  468. {
  469. grid.refreshSize();
  470. },
  471. buttons: [
  472. { text: '选择', onclick: function (item, dialog) { toSelect(); dialog.hide(); } },
  473. { text: '取消', onclick: function (item, dialog) { dialog.hide(); } }
  474. ]
  475. });
  476. grid.refreshSize();
  477. }
  478. function exist(value,data)
  479. {
  480. for (var i = 0; data && data[i]; i++)
  481. {
  482. var item = data[i];
  483. if (item[p.valueField] == value) return true;
  484. }
  485. return false;
  486. }
  487. function toSelect()
  488. {
  489. var selected = grid.selected || [];
  490. var value = [], text = [], data = [];
  491. $(selected).each(function (i, rowdata)
  492. {
  493. p.valueField && value.push(rowdata[p.valueField]);
  494. p.textField && text.push(rowdata[p.textField]);
  495. var o = $.extend(true, {}, this);
  496. grid.formatRecord(o, true);
  497. data.push(o);
  498. });
  499. var unSelected = [];
  500. $(lastSelected).each(function (i,item)
  501. {
  502. if (!exist(item[p.valueField], selected) && exist(item[p.valueField], grid.rows))
  503. {
  504. unSelected.push(item);
  505. }
  506. });
  507. var removeValue = [], removeText = [], removeData = [];
  508. $(unSelected).each(function (i, rowdata)
  509. {
  510. p.valueField && removeValue.push(rowdata[p.valueField]);
  511. p.textField && removeText.push(rowdata[p.textField]);
  512. var o = $.extend(true, {}, this);
  513. grid.formatRecord(o, true);
  514. removeData.push(o);
  515. });
  516. p.onSelect({
  517. value: value.join(p.split),
  518. text: text.join(p.split),
  519. data: data,
  520. remvoeValue: removeValue.join(p.split),
  521. remvoeText: removeText.join(p.split),
  522. removeData: removeData
  523. });
  524. }
  525. };
  526. })(jQuery);