ligerPopupEdit.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /**
  2. * jQuery ligerUI 1.2.2
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2013 [ 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");//为什么不会触发mouseon 和 mousedown事件
  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.showFunction instanceof Function){
  116. p.showFunction();
  117. }else if(p.showFunction=="selectUser"){
  118. selectUser(g.inputText);
  119. }
  120. if (p.disabled) return;
  121. if (g.trigger('buttonClick') == false) return false;
  122. });
  123. g.inputText.click(function ()
  124. {
  125. if (p.disabled) return;
  126. }).blur(function ()
  127. {
  128. if (p.disabled) return;
  129. g.wrapper.removeClass("l-text-focus");
  130. }).focus(function ()
  131. {
  132. if (p.disabled) return;
  133. g.wrapper.addClass("l-text-focus");
  134. });
  135. g.wrapper.hover(function ()
  136. {
  137. if (p.disabled) return;
  138. g.wrapper.addClass("l-text-over");
  139. }, function ()
  140. {
  141. if (p.disabled) return;
  142. g.wrapper.removeClass("l-text-over");
  143. });
  144. g.set(p);
  145. },
  146. destroy: function ()
  147. {
  148. if (this.wrapper) this.wrapper.remove();
  149. this.options = null;
  150. $.ligerui.remove(this);
  151. },
  152. clear: function ()
  153. {
  154. var g = this, p = this.options;
  155. g.inputText.val("");
  156. g.valueField.val("");
  157. },
  158. _setCss: function (css)
  159. {
  160. if (css)
  161. {
  162. this.wrapper.addClass(css);
  163. }
  164. },
  165. //取消选择
  166. _setCancelable: function (value)
  167. {
  168. var g = this, p = this.options;
  169. if (!value && g.unselect)
  170. {
  171. g.unselect.remove();
  172. g.unselect = null;
  173. }
  174. if (!value && !g.unselect) return;
  175. g.unselect = $('<div class="l-trigger l-trigger-cancel"><div class="l-trigger-icon"></div></div>').hide();
  176. g.wrapper.hover(function ()
  177. {
  178. // g.unselect.show();
  179. }, function ()
  180. {
  181. g.unselect.hide();
  182. })
  183. if (!p.disabled && p.cancelable)
  184. {
  185. g.wrapper.append(g.unselect);
  186. }
  187. g.unselect.hover(function ()
  188. {
  189. this.className = "l-trigger-hover l-trigger-cancel";
  190. }, function ()
  191. {
  192. this.className = "l-trigger l-trigger-cancel";
  193. }).click(function ()
  194. {
  195. g.clear();
  196. });
  197. },
  198. _setDisabled: function (value)
  199. {
  200. if (value)
  201. {
  202. this.wrapper.addClass('l-text-disabled');
  203. } else
  204. {
  205. this.wrapper.removeClass('l-text-disabled');
  206. }
  207. },
  208. _setWidth: function (value)
  209. {
  210. var g = this;
  211. if (value > 20)
  212. {
  213. g.wrapper.css({ width: value });
  214. g.inputText.css({ width: value - 20 });
  215. }
  216. },
  217. _setHeight: function (value)
  218. {
  219. var g = this;
  220. if (value > 10)
  221. {
  222. g.wrapper.height(value);
  223. g.inputText.height(value - 2);
  224. }
  225. },
  226. _getText: function ()
  227. {
  228. return $(this.inputText).val();
  229. },
  230. _getValue: function ()
  231. {
  232. return $(this.valueField).val();
  233. },
  234. getValue: function ()
  235. {
  236. return this._getValue();
  237. },
  238. getText: function ()
  239. {
  240. return this._getText();
  241. },
  242. //设置值到 隐藏域
  243. setValue: function (value, text)
  244. {
  245. var g = this, p = this.options;
  246. if (arguments.length >= 2)
  247. {
  248. g.setValue(value);
  249. g.setText(text);
  250. return;
  251. }
  252. g.valueField.val(value);
  253. },
  254. //设置值到 文本框
  255. setText: function (text)
  256. {
  257. var g = this, p = this.options;
  258. if (p.render)
  259. {
  260. g.inputText.val(p.render(text));
  261. }
  262. else
  263. {
  264. g.inputText.val(text);
  265. }
  266. },
  267. addValue: function (value, text)
  268. {
  269. var g = this, p = this.options;
  270. if (!value) return;
  271. var v = g.getValue(), t = g.getText();
  272. if (!v)
  273. {
  274. g.setValue(value);
  275. g.setText(text);
  276. } else
  277. {
  278. var arrV = [], arrT = [], old = v.split(p.split), value = value.split(p.split), text = text.split(p.split);
  279. for (var i = 0, l = value.length; i < l; i++)
  280. {
  281. if ($.inArray(value[i], old) == -1)
  282. {
  283. arrV.push(value[i]);
  284. arrT.push(text[i]);
  285. }
  286. }
  287. if (arrV.length)
  288. {
  289. g.setValue(v + p.split + arrV.join(p.split));
  290. g.setText(t + p.split + arrT.join(p.split));
  291. }
  292. }
  293. },
  294. removeValue: function (value, text)
  295. {
  296. var g = this, p = this.options;
  297. if (!value) return;
  298. var v = g.getValue(), t = g.getText();
  299. if (!v) return;
  300. var oldV = v.split(p.split), oldT = t.split(p.split), value = value.split(p.split);
  301. for (var i = 0, index = -1, l = value.length; i < l; i++)
  302. {
  303. if ((index = $.inArray(value[i], oldV)) != -1)
  304. {
  305. oldV.splice(index, 1);
  306. oldT.splice(index, 1);
  307. }
  308. }
  309. g.setValue(oldV.join(p.split));
  310. g.setText(oldT.join(p.split));
  311. },
  312. _setGrid: function (value)
  313. {
  314. if (!value) return;
  315. var g = this, p = this.options;
  316. var gridOptions = $.extend({
  317. parms: p.parms
  318. }, p.grid);
  319. this.bind('buttonClick', function ()
  320. {
  321. if (!g.popupFn)
  322. {
  323. var options = {
  324. grid: gridOptions,
  325. condition: p.condition,
  326. valueField: p.valueField,
  327. textField: p.textField,
  328. split: p.split,
  329. onSelect: function (e)
  330. {
  331. if (g.trigger('select', e) == false) return;
  332. if (p.grid.checkbox)
  333. {
  334. g.addValue(e.value, e.text);
  335. g.removeValue(e.remvoeValue, e.remvoeText);
  336. } else
  337. {
  338. g.setValue(e.value);
  339. g.setText(e.text);
  340. }
  341. g.trigger('selected', e);
  342. },
  343. selectInit: function (rowdata)
  344. {
  345. var value = g.getValue();
  346. if (!value) return false;
  347. if (!p.valueField || !rowdata[p.valueField]) return false;
  348. return $.inArray(rowdata[p.valueField].toString(), value.split(p.split)) != -1;
  349. }
  350. };
  351. g.popupFn = $.ligerui.getPopupFn(options);
  352. }
  353. g.popupFn();
  354. });
  355. }
  356. });
  357. //创建一个可查询、可分页列表的选取弹出框 需要dialog,grid,form等插件的支持
  358. $.ligerui.getPopupFn = function (p)
  359. {
  360. p = $.extend({
  361. title: '选择数据', //窗口标题
  362. width: 700, //窗口宽度
  363. height: 320, //列表高度
  364. top: null,
  365. left: null,
  366. split: ';',
  367. valueField: null, //接收表格的value字段名
  368. textField: null, //接收表格的text字段名
  369. grid: null, //表格的参数 同ligerGrid
  370. condition: null, //搜索表单的参数 同ligerForm
  371. onSelect: function (p) { }, //选取函数
  372. selectInit: function (rowdata) { return false } //选择初始化
  373. }, p);
  374. if (!p.grid) return;
  375. var win, grid, condition, lastSelected = [];
  376. return function ()
  377. {
  378. show();
  379. return false;
  380. };
  381. function show()
  382. {
  383. function getGridHeight(height)
  384. {
  385. height = height || p.height;
  386. height -= conditionPanel.height();
  387. return height;
  388. }
  389. if (win)
  390. {
  391. grid._showData();
  392. win.show();
  393. grid.refreshSize();
  394. lastSelected = grid.selected.concat();
  395. return;
  396. }
  397. var panle = $("<div></div>");
  398. var conditionPanel = $("<div></div>");
  399. var gridPanel = $("<div></div>");
  400. panle.append(conditionPanel).append(gridPanel);
  401. if (p.condition)
  402. {
  403. var conditionParm = $.extend({
  404. labelWidth: 60,
  405. space: 20
  406. }, p.condition);
  407. condition = conditionPanel.ligerForm(conditionParm);
  408. } else
  409. {
  410. conditionPanel.remove();
  411. }
  412. var gridParm = $.extend({
  413. columnWidth: 120,
  414. alternatingRow: false,
  415. frozen: true,
  416. rownumbers: true
  417. }, p.grid, {
  418. width: "100%",
  419. height: getGridHeight(),
  420. isChecked: p.selectInit,
  421. isSelected: p.selectInit,
  422. inWindow: false
  423. });
  424. //grid
  425. grid = gridPanel.ligerGrid(gridParm);
  426. //搜索按钮
  427. if (p.condition)
  428. {
  429. var containerBtn1 = $('<li style="margin-right:9px"><div></div></li>');
  430. $("ul:first", conditionPanel).append(containerBtn1).after('<div class="l-clear"></div>');
  431. $("div", containerBtn1).ligerButton({
  432. text: '搜索',
  433. click: function ()
  434. {
  435. var rules = $.ligerui.getConditions(conditionPanel);
  436. grid.setParm('condition', $.ligerui.toJSON(rules));
  437. grid.reload();
  438. }
  439. });
  440. }
  441. //dialog
  442. win = $.ligerDialog.open({
  443. title: p.title,
  444. width: p.width,
  445. height: 'auto',
  446. top: p.top,
  447. left: p.left,
  448. target: panle,
  449. isResize: true,
  450. cls: 'l-selectorwin',
  451. onContentHeightChange: function (height)
  452. {
  453. grid.set('height', getGridHeight(height));
  454. return false;
  455. },
  456. onStopResize: function ()
  457. {
  458. grid.refreshSize();
  459. },
  460. buttons: [
  461. { text: '选择', onclick: function (item, dialog) { toSelect(); dialog.hide(); } },
  462. { text: '取消', onclick: function (item, dialog) { dialog.hide(); } }
  463. ]
  464. });
  465. grid.refreshSize();
  466. }
  467. function toSelect()
  468. {
  469. var selected = grid.selected || [];
  470. var value = [], text = [], data = [];
  471. $(selected).each(function (i, rowdata)
  472. {
  473. p.valueField && value.push(rowdata[p.valueField]);
  474. p.textField && text.push(rowdata[p.textField]);
  475. var o = $.extend(true, {}, this);
  476. grid.formatRecord(o, true);
  477. data.push(o);
  478. });
  479. var unSelected = [];
  480. $(lastSelected).each(function ()
  481. {
  482. if ($.inArray(this, selected) == -1 && $.inArray(this, grid.rows) != -1)
  483. {
  484. unSelected.push(this);
  485. }
  486. });
  487. var removeValue = [], removeText = [], removeData = [];
  488. $(unSelected).each(function (i, rowdata)
  489. {
  490. p.valueField && removeValue.push(rowdata[p.valueField]);
  491. p.textField && removeText.push(rowdata[p.textField]);
  492. var o = $.extend(true, {}, this);
  493. grid.formatRecord(o, true);
  494. removeData.push(o);
  495. });
  496. p.onSelect({
  497. value: value.join(p.split),
  498. text: text.join(p.split),
  499. data: data,
  500. remvoeValue: removeValue.join(p.split),
  501. remvoeText: removeText.join(p.split),
  502. removeData: removeData
  503. });
  504. }
  505. };
  506. })(jQuery);