您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

513 行
12 KiB

  1. /***
  2. * Contains basic SlickGrid editors.
  3. * @module Editors
  4. * @namespace Slick
  5. */
  6. (function ($) {
  7. // register namespace
  8. $.extend(true, window, {
  9. "Slick": {
  10. "Editors": {
  11. "Text": TextEditor,
  12. "Integer": IntegerEditor,
  13. "Date": DateEditor,
  14. "YesNoSelect": YesNoSelectEditor,
  15. "Checkbox": CheckboxEditor,
  16. "PercentComplete": PercentCompleteEditor,
  17. "LongText": LongTextEditor
  18. }
  19. }
  20. });
  21. function TextEditor(args) {
  22. var $input;
  23. var defaultValue;
  24. var scope = this;
  25. this.init = function () {
  26. $input = $("<INPUT type=text class='editor-text' />")
  27. .appendTo(args.container)
  28. .bind("keydown.nav", function (e) {
  29. if (e.keyCode === $.ui.keyCode.LEFT || e.keyCode === $.ui.keyCode.RIGHT) {
  30. e.stopImmediatePropagation();
  31. }
  32. })
  33. .focus()
  34. .select();
  35. };
  36. this.destroy = function () {
  37. $input.remove();
  38. };
  39. this.focus = function () {
  40. $input.focus();
  41. };
  42. this.getValue = function () {
  43. return $input.val();
  44. };
  45. this.setValue = function (val) {
  46. $input.val(val);
  47. };
  48. this.loadValue = function (item) {
  49. defaultValue = item[args.column.field] || "";
  50. $input.val(defaultValue);
  51. $input[0].defaultValue = defaultValue;
  52. $input.select();
  53. };
  54. this.serializeValue = function () {
  55. return $input.val();
  56. };
  57. this.applyValue = function (item, state) {
  58. item[args.column.field] = state;
  59. };
  60. this.isValueChanged = function () {
  61. return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
  62. };
  63. this.validate = function () {
  64. if (args.column.validator) {
  65. var validationResults = args.column.validator($input.val());
  66. if (!validationResults.valid) {
  67. return validationResults;
  68. }
  69. }
  70. return {
  71. valid: true,
  72. msg: null
  73. };
  74. };
  75. this.init();
  76. }
  77. function IntegerEditor(args) {
  78. var $input;
  79. var defaultValue;
  80. var scope = this;
  81. this.init = function () {
  82. $input = $("<INPUT type=text class='editor-text' />");
  83. $input.bind("keydown.nav", function (e) {
  84. if (e.keyCode === $.ui.keyCode.LEFT || e.keyCode === $.ui.keyCode.RIGHT) {
  85. e.stopImmediatePropagation();
  86. }
  87. });
  88. $input.appendTo(args.container);
  89. $input.focus().select();
  90. };
  91. this.destroy = function () {
  92. $input.remove();
  93. };
  94. this.focus = function () {
  95. $input.focus();
  96. };
  97. this.loadValue = function (item) {
  98. defaultValue = item[args.column.field];
  99. $input.val(defaultValue);
  100. $input[0].defaultValue = defaultValue;
  101. $input.select();
  102. };
  103. this.serializeValue = function () {
  104. return parseInt($input.val(), 10) || 0;
  105. };
  106. this.applyValue = function (item, state) {
  107. item[args.column.field] = state;
  108. };
  109. this.isValueChanged = function () {
  110. return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
  111. };
  112. this.validate = function () {
  113. if (isNaN($input.val())) {
  114. return {
  115. valid: false,
  116. msg: "Please enter a valid integer"
  117. };
  118. }
  119. return {
  120. valid: true,
  121. msg: null
  122. };
  123. };
  124. this.init();
  125. }
  126. function DateEditor(args) {
  127. var $input;
  128. var defaultValue;
  129. var scope = this;
  130. var calendarOpen = false;
  131. this.init = function () {
  132. $input = $("<INPUT type=text class='editor-text' />");
  133. $input.appendTo(args.container);
  134. $input.focus().select();
  135. $input.datepicker({
  136. showOn: "button",
  137. buttonImageOnly: true,
  138. buttonImage: "../images/calendar.gif",
  139. beforeShow: function () {
  140. calendarOpen = true
  141. },
  142. onClose: function () {
  143. calendarOpen = false
  144. }
  145. });
  146. $input.width($input.width() - 18);
  147. };
  148. this.destroy = function () {
  149. $.datepicker.dpDiv.stop(true, true);
  150. $input.datepicker("hide");
  151. $input.datepicker("destroy");
  152. $input.remove();
  153. };
  154. this.show = function () {
  155. if (calendarOpen) {
  156. $.datepicker.dpDiv.stop(true, true).show();
  157. }
  158. };
  159. this.hide = function () {
  160. if (calendarOpen) {
  161. $.datepicker.dpDiv.stop(true, true).hide();
  162. }
  163. };
  164. this.position = function (position) {
  165. if (!calendarOpen) {
  166. return;
  167. }
  168. $.datepicker.dpDiv
  169. .css("top", position.top + 30)
  170. .css("left", position.left);
  171. };
  172. this.focus = function () {
  173. $input.focus();
  174. };
  175. this.loadValue = function (item) {
  176. defaultValue = item[args.column.field];
  177. $input.val(defaultValue);
  178. $input[0].defaultValue = defaultValue;
  179. $input.select();
  180. };
  181. this.serializeValue = function () {
  182. return $input.val();
  183. };
  184. this.applyValue = function (item, state) {
  185. item[args.column.field] = state;
  186. };
  187. this.isValueChanged = function () {
  188. return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
  189. };
  190. this.validate = function () {
  191. return {
  192. valid: true,
  193. msg: null
  194. };
  195. };
  196. this.init();
  197. }
  198. function YesNoSelectEditor(args) {
  199. var $select;
  200. var defaultValue;
  201. var scope = this;
  202. this.init = function () {
  203. $select = $("<SELECT tabIndex='0' class='editor-yesno'><OPTION value='yes'>Yes</OPTION><OPTION value='no'>No</OPTION></SELECT>");
  204. $select.appendTo(args.container);
  205. $select.focus();
  206. };
  207. this.destroy = function () {
  208. $select.remove();
  209. };
  210. this.focus = function () {
  211. $select.focus();
  212. };
  213. this.loadValue = function (item) {
  214. $select.val((defaultValue = item[args.column.field]) ? "yes" : "no");
  215. $select.select();
  216. };
  217. this.serializeValue = function () {
  218. return ($select.val() == "yes");
  219. };
  220. this.applyValue = function (item, state) {
  221. item[args.column.field] = state;
  222. };
  223. this.isValueChanged = function () {
  224. return ($select.val() != defaultValue);
  225. };
  226. this.validate = function () {
  227. return {
  228. valid: true,
  229. msg: null
  230. };
  231. };
  232. this.init();
  233. }
  234. function CheckboxEditor(args) {
  235. var $select;
  236. var defaultValue;
  237. var scope = this;
  238. this.init = function () {
  239. $select = $("<INPUT type=checkbox value='true' class='editor-checkbox' hideFocus>");
  240. $select.appendTo(args.container);
  241. $select.focus();
  242. };
  243. this.destroy = function () {
  244. $select.remove();
  245. };
  246. this.focus = function () {
  247. $select.focus();
  248. };
  249. this.loadValue = function (item) {
  250. defaultValue = !!item[args.column.field];
  251. if (defaultValue) {
  252. $select.attr("checked", "checked");
  253. } else {
  254. $select.removeAttr("checked");
  255. }
  256. };
  257. this.serializeValue = function () {
  258. return !!$select.attr("checked");
  259. };
  260. this.applyValue = function (item, state) {
  261. item[args.column.field] = state;
  262. };
  263. this.isValueChanged = function () {
  264. return (this.serializeValue() !== defaultValue);
  265. };
  266. this.validate = function () {
  267. return {
  268. valid: true,
  269. msg: null
  270. };
  271. };
  272. this.init();
  273. }
  274. function PercentCompleteEditor(args) {
  275. var $input, $picker;
  276. var defaultValue;
  277. var scope = this;
  278. this.init = function () {
  279. $input = $("<INPUT type=text class='editor-percentcomplete' />");
  280. $input.width($(args.container).innerWidth() - 25);
  281. $input.appendTo(args.container);
  282. $picker = $("<div class='editor-percentcomplete-picker' />").appendTo(args.container);
  283. $picker.append("<div class='editor-percentcomplete-helper'><div class='editor-percentcomplete-wrapper'><div class='editor-percentcomplete-slider' /><div class='editor-percentcomplete-buttons' /></div></div>");
  284. $picker.find(".editor-percentcomplete-buttons").append("<button val=0>Not started</button><br/><button val=50>In Progress</button><br/><button val=100>Complete</button>");
  285. $input.focus().select();
  286. $picker.find(".editor-percentcomplete-slider").slider({
  287. orientation: "vertical",
  288. range: "min",
  289. value: defaultValue,
  290. slide: function (event, ui) {
  291. $input.val(ui.value)
  292. }
  293. });
  294. $picker.find(".editor-percentcomplete-buttons button").bind("click", function (e) {
  295. $input.val($(this).attr("val"));
  296. $picker.find(".editor-percentcomplete-slider").slider("value", $(this).attr("val"));
  297. })
  298. };
  299. this.destroy = function () {
  300. $input.remove();
  301. $picker.remove();
  302. };
  303. this.focus = function () {
  304. $input.focus();
  305. };
  306. this.loadValue = function (item) {
  307. $input.val(defaultValue = item[args.column.field]);
  308. $input.select();
  309. };
  310. this.serializeValue = function () {
  311. return parseInt($input.val(), 10) || 0;
  312. };
  313. this.applyValue = function (item, state) {
  314. item[args.column.field] = state;
  315. };
  316. this.isValueChanged = function () {
  317. return (!($input.val() == "" && defaultValue == null)) && ((parseInt($input.val(), 10) || 0) != defaultValue);
  318. };
  319. this.validate = function () {
  320. if (isNaN(parseInt($input.val(), 10))) {
  321. return {
  322. valid: false,
  323. msg: "Please enter a valid positive number"
  324. };
  325. }
  326. return {
  327. valid: true,
  328. msg: null
  329. };
  330. };
  331. this.init();
  332. }
  333. /*
  334. * An example of a "detached" editor.
  335. * The UI is added onto document BODY and .position(), .show() and .hide() are implemented.
  336. * KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
  337. */
  338. function LongTextEditor(args) {
  339. var $input, $wrapper;
  340. var defaultValue;
  341. var scope = this;
  342. this.init = function () {
  343. var $container = $("body");
  344. $wrapper = $("<DIV style='z-index:10000;position:absolute;background:white;padding:5px;border:3px solid gray; -moz-border-radius:10px; border-radius:10px;'/>")
  345. .appendTo($container);
  346. $input = $("<TEXTAREA hidefocus rows=5 style='backround:white;width:250px;height:80px;border:0;outline:0'>")
  347. .appendTo($wrapper);
  348. $("<DIV style='text-align:right'><BUTTON>Save</BUTTON><BUTTON>Cancel</BUTTON></DIV>")
  349. .appendTo($wrapper);
  350. $wrapper.find("button:first").bind("click", this.save);
  351. $wrapper.find("button:last").bind("click", this.cancel);
  352. $input.bind("keydown", this.handleKeyDown);
  353. scope.position(args.position);
  354. $input.focus().select();
  355. };
  356. this.handleKeyDown = function (e) {
  357. if (e.which == $.ui.keyCode.ENTER && e.ctrlKey) {
  358. scope.save();
  359. } else if (e.which == $.ui.keyCode.ESCAPE) {
  360. e.preventDefault();
  361. scope.cancel();
  362. } else if (e.which == $.ui.keyCode.TAB && e.shiftKey) {
  363. e.preventDefault();
  364. args.grid.navigatePrev();
  365. } else if (e.which == $.ui.keyCode.TAB) {
  366. e.preventDefault();
  367. args.grid.navigateNext();
  368. }
  369. };
  370. this.save = function () {
  371. args.commitChanges();
  372. };
  373. this.cancel = function () {
  374. $input.val(defaultValue);
  375. args.cancelChanges();
  376. };
  377. this.hide = function () {
  378. $wrapper.hide();
  379. };
  380. this.show = function () {
  381. $wrapper.show();
  382. };
  383. this.position = function (position) {
  384. $wrapper
  385. .css("top", position.top - 5)
  386. .css("left", position.left - 5)
  387. };
  388. this.destroy = function () {
  389. $wrapper.remove();
  390. };
  391. this.focus = function () {
  392. $input.focus();
  393. };
  394. this.loadValue = function (item) {
  395. $input.val(defaultValue = item[args.column.field]);
  396. $input.select();
  397. };
  398. this.serializeValue = function () {
  399. return $input.val();
  400. };
  401. this.applyValue = function (item, state) {
  402. item[args.column.field] = state;
  403. };
  404. this.isValueChanged = function () {
  405. return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
  406. };
  407. this.validate = function () {
  408. return {
  409. valid: true,
  410. msg: null
  411. };
  412. };
  413. this.init();
  414. }
  415. })(jQuery);