-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.searchParams.min.js
1 lines (1 loc) · 6.33 KB
/
jquery.searchParams.min.js
1
!function(t){t.fn.searchParams=function(o){return e.methods[o]?e.methods[o].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof o&&o?void t.error("jQuery.searchParams: Method `"+o+"` is undefined"):e.methods.init.apply(this,arguments)};let e={methods:{init:function(e){let s=t.extend({plusDirection:"top",selectOptions:[],customRowType:[],plusBtnClass:"",crossBtnClass:""},e);0!==s.selectOptions.length?"top"===s.plusDirection||"bottom"===s.plusDirection?(o.var.globalCtx=this,o.var.globalSettings=e,o.methods.initRowsMap(),o.methods.initSingleNamesMap(),this.append('<div class="col-lg-12" type="sp_wrapper"></div>'),o.methods.addRow(this,s.selectOptions)):t.error('jQuery.searchParams: options.plusDirection can be only "top" or "bottom"'):t.error("jQuery.searchParams: options.selectOptions required")},rowsCount:function(){return o.var.rowsCount},exportJSON:function(){let e=t(o.var.globalCtx).find(".row"),s={};for(let n=0;n<e.length;n++){let l=t(t(e[n]).find('[type="sp_select"]').find("select")[0]).find("option:selected"),a=l.attr("name"),i="";""!==(i=l.attr("s_type").startsWith("select")?t(t(e[n]).find('[type="sp_value"]').find("select")[0]).find("option:selected").attr("name"):t(e[n]).find('[type="sp_value"]').find("input").val())&&(void 0===s[a]&&(s[a]=[]),o.methods.isExists(s[a],i)||s[a].push(i))}if(0!==Object.entries(s).length)return{values:s}},importJSON:function(t){o.var.globalCtx.find('[type="sp_wrapper"]').html("");for(const[e,s]of Object.entries(t.values))for(let t=0;t<s.length;t++)o.methods.addRowWithParams(e,s[t]);o.var.rowsCount=o.var.globalCtx.find(".row").length},exportJsonStr:function(){return JSON.stringify(e.methods.exportJSON())},importJsonStr:function(t){e.methods.importJSON(JSON.parse(t))}}},o={methods:{addRow:function(){o.methods.removePlus();let e=t(o.const.rowBaseTmpl);o.methods.addSelect(e),o.methods.addValue(e),o.methods.addCross(e),o.var.globalCtx.find('[type="sp_wrapper"]').append(e),o.methods.toggleSingleAvailability(),o.var.rowsCount++,o.methods.addPlus()},addRowWithParams:function(e,s){o.methods.removePlus();let n=t(o.const.rowBaseTmpl);o.methods.addSelect(n),n.find(`select option[name="${e}"]`).prop("selected",!0),o.methods.addValue(n),0!==n.find("input").length?n.find("input").val(s):n.find('[type="sp_value"]').find(`select option[name="${s}"]`).prop("selected",!0),o.methods.addCross(n),o.var.globalCtx.find('[type="sp_wrapper"]').append(n),o.var.rowsCount++,o.methods.addPlus()},isExists:function(t,e){for(let o=0;o<t.length;o++)if(t[o]===e)return!0;return!1},addSelect:function(t){t.find('[type="sp_select"]').append(o.methods.getSelect(t))},addValue:function(t){t.find('[type="sp_value"]').append(o.var.rowsTypeMap.get(t.find('[type="sp_select"]').find("select option:selected").attr("s_type")))},changeValue:function(e,s){e.find('[type="sp_value"]').html(o.var.rowsTypeMap.get(t(s[0].options[s[0].options.selectedIndex]).attr("s_type")))},addPlus:function(){let e;e=""===o.var.globalSettings.plusBtnClass?"btn btn-block btn-outline-success":"btn btn-block "+o.var.globalSettings.plusBtnClass;let s=t(`<button class="${e}"><i class="fa fa-plus" aria-hidden="true"></i></button>`);s.on("click",function(){o.methods.addRow()}),"top"===o.var.globalSettings.plusDirection?t(o.var.globalCtx.find(".row")[0]).find('[type="sp_plus"]').append(s):o.var.globalCtx.find(".row").last().find('[type="sp_plus"]').append(s)},removePlus:function(){o.var.globalCtx.find('[type="sp_plus"]').find("button").remove()},addCross:function(e){let s;s=""===o.var.globalSettings.crossBtnClass?"btn btn-block btn-outline-danger":"btn btn-block "+o.var.globalSettings.crossBtnClass;let n=t(`<button class="${s}"><i class="fa fa-times" aria-hidden="true"></i></button>`);n.on("click",function(){o.methods.removeRow(e)}),e.find('[type="sp_cross"]').append(n)},removeRow:function(t){t.remove(),o.var.rowsCount--,o.methods.isEmpty()?o.methods.addRow():(o.methods.removePlus(),o.methods.addPlus())},isEmpty:function(){return 0===o.var.rowsCount},getSelect:function(e){let s=t('<select class="form-control"></select>');for(let t=0;t<o.var.globalSettings.selectOptions.length;t++)o.methods.addParam(s,o.var.globalSettings.selectOptions[t]);for(let t=0;t<o.var.globalSettings.customRowType.length;t++)o.methods.addParam(s,o.var.globalSettings.customRowType[t]);return s.on("change",function(){o.methods.changeValue(e,t(this)),o.methods.toggleSingleAvailability()}),s},toggleSingleAvailability:function(){let e=o.var.globalCtx.find('[type="sp_select"]').find("option");e.removeAttr("disabled");let s=o.var.globalCtx.find('[type="sp_select"]').find("option:selected");e.each(function(n){let l=t(e[n]).attr("name");if(o.var.singleNames[l])for(let e=0;e<s.length;e++)if(t(s[e]).attr("name")===l){o.var.globalCtx.find('[type="sp_select"]').find(`option[name="${l}"]`).attr("disabled",!0)}})},addParam:function(e,o){let s=t("<option></option>");s.attr("name",o.name),s.attr("s_type",o.type),s.append(o.tittle),e.append(s)},initRowsMap:function(){o.var.rowsTypeMap=new Map,o.var.rowsTypeMap.set("text",'<input class="form-control" type="text">'),o.var.rowsTypeMap.set("color",'<input class="form-control" type="color">'),o.var.rowsTypeMap.set("number",'<input class="form-control" type="number">'),o.var.rowsTypeMap.set("date",'<input class="form-control" type="date">');let e=o.var.globalSettings.customRowType;if(void 0!==e&&0!==e.length)for(let s=0;s<e.length;s++)if(e[s].type.startsWith("select")){let n=t('<select class="form-control"></select>');t(e[s].params).each(function(o){let l=t("<option></option>");l.attr("name",e[s].params[o].value),l.append(e[s].params[o].tittle),n.append(l)}),o.var.rowsTypeMap.set(e[s].type,n[0].outerHTML)}else o.var.rowsTypeMap.set(e[s].type,e[s].code)},initSingleNamesMap:function(){for(let t=0;t<o.var.globalSettings.selectOptions.length;t++){let e=o.var.globalSettings.selectOptions[t];!0===e.isSingle&&(o.var.singleNames[e.name]=!0)}for(let t=0;t<o.var.globalSettings.customRowType.length;t++){let e=o.var.globalSettings.customRowType[t];!0===e.isSingle&&(o.var.singleNames[e.name]=!0)}}},var:{globalCtx:void 0,globalSettings:void 0,rowsCount:0,rowsTypeMap:{},singleNames:{},secretKey:"5aee0674d9cb426c8f7737d4d24072f2"},const:{rowBaseTmpl:'<div class="row"><div class="col-lg-3 form-group" type="sp_select"></div><div class="col-lg-7 form-group" type="sp_value"></div><div class="col-lg-1 form-group" type="sp_cross"></div><div class="col-lg-1 form-group" type="sp_plus"></div></div>'}}}(jQuery);