select.data.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;(function ($, window, document, undefined) {
  2. /**
  3. * 数据选择器模块
  4. * @param $trigger
  5. * @param option
  6. * @constructor
  7. */
  8. function selectData($trigger, option) {
  9. var defaults = {
  10. title: '', // 弹窗标题
  11. uri: '', // api uri
  12. duplicate: true, // 是否允许重复数据
  13. dataIndex: '', // 数据索引名称 例如: goods_id, 用于验证数据是否重复
  14. getExistData: $.noop() // 获取已存在数据接口函数
  15. };
  16. this.options = $.extend({}, defaults, option);
  17. // 初始化对象事件
  18. this.init($trigger);
  19. }
  20. selectData.prototype = {
  21. /**
  22. * 初始化
  23. */
  24. init: function ($trigger) {
  25. var _this = this;
  26. if ($trigger === false) {
  27. _this.showModal();
  28. return false;
  29. }
  30. // 选择器触发事件
  31. $trigger.unbind().click(function () {
  32. _this.showModal();
  33. });
  34. },
  35. /**
  36. * 显示数据选择弹窗
  37. */
  38. showModal: function () {
  39. var _this = this;
  40. // 捕获页
  41. layer.open({
  42. type: 2
  43. , id: _this.options.layerId
  44. , title: _this.options.title
  45. , skin: 'modal-select-data'
  46. , area: ['840px', '520px']
  47. , offset: 'auto'
  48. , anim: 1
  49. , closeBtn: 1
  50. , shade: 0.3
  51. , btn: ['确定', '取消']
  52. , content: STORE_URL + '/data.' + this.options.uri
  53. , success: function (layero) {
  54. // 初始化文件库弹窗
  55. _this.initModal(layero);
  56. }
  57. , yes: function (index, layero) {
  58. var iframeWin = window[layero.find('iframe')[0]['name']]
  59. , selectedData = iframeWin.getSelectedData() // 选择的数据
  60. , newData = _this.duplicateData(selectedData); // 去除重复
  61. // 执行回调函数
  62. if (newData && typeof _this.options.done === 'function') {
  63. _this.options.done(JSON.parse(JSON.stringify(newData)), this.$touch);
  64. }
  65. layer.close(index);
  66. }
  67. });
  68. },
  69. /**
  70. * 筛选重复数据
  71. * @param selectedData
  72. */
  73. duplicateData: function (selectedData) {
  74. var _this = this;
  75. if (!selectedData.length) {
  76. return false;
  77. }
  78. if (_this.options.duplicate !== false) {
  79. return selectedData;
  80. }
  81. if (_this.options.dataIndex === '') {
  82. console.error('dataIndex is not defined');
  83. return false;
  84. }
  85. var existData = _this.options.getExistData.call(true);
  86. if (!existData.length) {
  87. return selectedData;
  88. }
  89. var newData = [];
  90. selectedData.forEach(function (item) {
  91. if (existData.indexOf(item[_this.options.dataIndex]) === -1) {
  92. newData.push(item);
  93. existData.push(item[_this.options.dataIndex]);
  94. }
  95. });
  96. return newData;
  97. },
  98. /**
  99. * 初始弹窗
  100. */
  101. initModal: function (element) {
  102. var _this = this;
  103. _this.$element = element;
  104. }
  105. };
  106. /**
  107. * 在Jquery插件中使用selectData对象
  108. * @param options
  109. * @returns {selectData}
  110. */
  111. $.fn.selectData = function (options) {
  112. return new selectData(this, options);
  113. };
  114. /**
  115. * 在Jquery插件中使用selectData对象
  116. * @param options
  117. * @returns {selectData}
  118. */
  119. $.selectData = function (options) {
  120. return new selectData(false, options);
  121. };
  122. })(jQuery, window, document);