123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- ;(function ($, window, document, undefined) {
- /**
- * 数据选择器模块
- * @param $trigger
- * @param option
- * @constructor
- */
- function selectData($trigger, option) {
- var defaults = {
- title: '', // 弹窗标题
- uri: '', // api uri
- duplicate: true, // 是否允许重复数据
- dataIndex: '', // 数据索引名称 例如: goods_id, 用于验证数据是否重复
- getExistData: $.noop() // 获取已存在数据接口函数
- };
- this.options = $.extend({}, defaults, option);
- // 初始化对象事件
- this.init($trigger);
- }
- selectData.prototype = {
- /**
- * 初始化
- */
- init: function ($trigger) {
- var _this = this;
- if ($trigger === false) {
- _this.showModal();
- return false;
- }
- // 选择器触发事件
- $trigger.unbind().click(function () {
- _this.showModal();
- });
- },
- /**
- * 显示数据选择弹窗
- */
- showModal: function () {
- var _this = this;
- // 捕获页
- layer.open({
- type: 2
- , id: _this.options.layerId
- , title: _this.options.title
- , skin: 'modal-select-data'
- , area: ['840px', '520px']
- , offset: 'auto'
- , anim: 1
- , closeBtn: 1
- , shade: 0.3
- , btn: ['确定', '取消']
- , content: STORE_URL + '/data.' + this.options.uri
- , success: function (layero) {
- // 初始化文件库弹窗
- _this.initModal(layero);
- }
- , yes: function (index, layero) {
- var iframeWin = window[layero.find('iframe')[0]['name']]
- , selectedData = iframeWin.getSelectedData() // 选择的数据
- , newData = _this.duplicateData(selectedData); // 去除重复
- // 执行回调函数
- if (newData && typeof _this.options.done === 'function') {
- _this.options.done(JSON.parse(JSON.stringify(newData)), this.$touch);
- }
- layer.close(index);
- }
- });
- },
- /**
- * 筛选重复数据
- * @param selectedData
- */
- duplicateData: function (selectedData) {
- var _this = this;
- if (!selectedData.length) {
- return false;
- }
- if (_this.options.duplicate !== false) {
- return selectedData;
- }
- if (_this.options.dataIndex === '') {
- console.error('dataIndex is not defined');
- return false;
- }
- var existData = _this.options.getExistData.call(true);
- if (!existData.length) {
- return selectedData;
- }
- var newData = [];
- selectedData.forEach(function (item) {
- if (existData.indexOf(item[_this.options.dataIndex]) === -1) {
- newData.push(item);
- existData.push(item[_this.options.dataIndex]);
- }
- });
- return newData;
- },
- /**
- * 初始弹窗
- */
- initModal: function (element) {
- var _this = this;
- _this.$element = element;
- }
- };
- /**
- * 在Jquery插件中使用selectData对象
- * @param options
- * @returns {selectData}
- */
- $.fn.selectData = function (options) {
- return new selectData(this, options);
- };
- /**
- * 在Jquery插件中使用selectData对象
- * @param options
- * @returns {selectData}
- */
- $.selectData = function (options) {
- return new selectData(false, options);
- };
- })(jQuery, window, document);
|