(function(factory) { "use strict"; if (typeof define === 'function' && define.amd) { define(['jquery'], factory); } else if (window.jQuery && !window.jQuery.fn.ProxyMapper) { factory(window.jQuery); } } (function($) { 'use strict'; var ProxyMapper = function(mapping, data, options) { this.mapping = mapping; this.data = data; console.log(options); this._default_options = { fillValue: 0, functions: { dates: function (value) {return value;}, labels: function (value) {return value;}, values: function (value) {return value;} } }; this.options = $.extend({}, this._default_options, options); this.result = {}; this.result.dates = []; this.mappingI2 = {}; this.perform_mapping(); return this.result; }; ProxyMapper.prototype = { constructor: ProxyMapper, perform_mapping: function(data) { if (this.mapping.dates.length > 0) { this.c_dates(this.data, this.mapping.dates); // probe and fetch all dates } if (this.mapping.labels.length > 0) { this.c_labels(this.data, this.mapping.labels); // probe and fetch all labels } if (this.mapping.labels.length > 0 && this.mapping.values.length > 0) { this.c_values(this.data, this.mapping.values); // fetch values and overwrite default values for (var k in this.result) { this.result[k] = this.result[k].filter(function(n){ return n != undefined }); } } }, c_dates: function(intermediate, instructions) { var that = this; var matchingFun = function (intermediate, instructions, additionalData) { let index = instructions; let val = intermediate[index]; if (that.mappingI2[val] === undefined) { that.mappingI2[val] = that.result['dates'].length; let nval = that.options.functions.dates(val); that.result['dates'].push(nval); } }; this.iter(intermediate, instructions, matchingFun, {}); }, c_labels: function(intermediate, instructions, valuesLength) { var that = this; var matchingFun = function (intermediate, instructions, additionalData) { let index = instructions; if (index == 'l') { // labels are the key themself for (let label in intermediate) { let val = []; for (var i=0; i 0) { curI = this.fetch_value(node, sub_instructions); } else { console.log('Should never happend'); } additionalData[i_type] = curI; } this.iter(node, instructions.slice(1), matchingFun, additionalData); } } else if (this.isObject(intermediate)) { for (var k in intermediate) { var node = intermediate[k]; if (flag_register_i) { let sub_instructions = additionalData.mapping.index[i_type] let curI; if (sub_instructions.length > 0) { curI = this.fetch_value(node, sub_instructions); } else { curI = k; } additionalData[i_type] = curI; } this.iter(node, instructions.slice(1), matchingFun, additionalData); } } }, isObject: function(v) { return v !== null && typeof v === 'object'; } }; $.proxyMapper = ProxyMapper; $.fn.proxyMapper = function(options) { var pickerArgs = arguments; return this.each(function() { var $this = $(this), inst = $this.data('proxyMapper'), options = ((typeof option === 'object') ? option : {}); if ((!inst) && (typeof option !== 'string')) { $this.data('proxyMapper', new ProxyMapper($this, options)); } else { if (typeof option === 'string') { inst[option].apply(inst, Array.prototype.slice.call(pickerArgs, 1)); } } }); } $.fn.proxyMapper.constructor = ProxyMapper; }) );