MapCollection¶
-
class
webix.
MapCollection
()¶ Mapcollection mixin
References¶
- components
DataCollection()
.- helpers
assert_error()
,bind()
,isArray()
,uid()
.
Referenced by¶
- components
DataCollection()
.- views
datatable()
,property()
.
External references¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | webix.MapCollection = {
$init:function(){
this.$ready.push(this._create_scheme_init);
this.attachEvent("onStructureUpdate", this._create_scheme_init);
this.attachEvent("onStructureLoad", function(){
if(!this._scheme_init_order.length)
this._create_scheme_init();
});
},
_create_scheme_init:function(order){
var order = this._scheme_init_order = [];
var config = this._settings;
if (config.columns)
this._build_data_map(config.columns);
if (this._settings.map)
this._process_field_map(config.map);
if (this._scheme_init_order.length){
try {
this.data._scheme_init = Function("obj",order.join("\n"));
} catch(e){
webix.assert_error("Invalid data map:"+order.join("\n"));
}
}
},
_process_field_map:function(map){
for (var key in map)
this._scheme_init_order.push(this._process_single_map(key, map[key]));
},
_process_single_map:function(id, map, extra){
var start = "";
var end = "";
if (map.indexOf("(date)")===0){
start = "webix.i18n.parseFormatDate("; end=")";
if (extra && !extra.format) extra.format = webix.i18n.dateFormatStr;
map = map.replace("(date)","");
} else if (map.indexOf("(number)")===0){
start = "("; end=")*1";
map = map.replace("(number)","");
}
if (map !== ""){
map=map.replace(/\{obj\.([^}]*)\}/g,"\"+(obj.$1||'')+\"");
map=map.replace(/#([^#'";, ]+)#/gi,"\"+(obj.$1||'')+\"");
} else
map = "\"+(obj."+id+"||'')+\"";
return "obj."+id+" = "+start+'"'+map+'"'+end+";";
},
_build_data_map:function(columns){ //for datatable
for (var i=0; i<columns.length; i++){
var map = columns[i].map;
var id = columns[i].id;
if (!id) {
id = columns[i].id = "i"+webix.uid();
if (!columns[i].header)
columns[i].header = "";
}
if (map)
this._scheme_init_order.push(this._process_single_map(id, map, columns[i]));
this._map_options(columns[i]);
}
},
_map_options:function(element){
var options = element.options||element.collection;
if(options){
if (typeof options === "string"){
//id of some other view
var options_view = webix.$$(options);
//or url
if (!options_view){
options_view = new webix.DataCollection({ url: options });
this._destroy_with_me.push(options_view);
}
//if it was a view, special check for suggests
if (options_view.getBody) options_view = options_view.getBody();
this._bind_collection(options_view, element);
} else if (!options.loadNext){
if (options[0] && typeof options[0] == "object"){
//[{ id:1, value:"one"}, ...]
options = new webix.DataCollection({ data:options });
this._bind_collection(options, element);
this._destroy_with_me.push(options);
} else {
//["one", "two"]
//or
//{ 1: "one", 2: "two"}
if (webix.isArray(options)){
var data = {};
for (var ij=0; ij<options.length; ij++) data[options[ij]] = options[ij];
element.options = options = data;
}
element.template = element.template || this._collection_accesser(options, element.id, element.optionslist);
}
} else {
//data collection or view
this._bind_collection(options, element);
}
}
},
_bind_collection:function(options, element){
if (element){
delete element.options;
element.collection = options;
element.template = element.template || this._bind_accesser(options, element.id, element.optionslist);
var id = options.data.attachEvent("onStoreUpdated", webix.bind(function(){
this.refresh();
if(this.refreshFilter)
this.refreshFilter(element.id);
}, this));
this.attachEvent("onDestruct", function(){
if (!options.$destructed) options.data.detachEvent(id);
});
}
},
_collection_accesser:function(options, id, multi){
if (multi){
var separator = typeof multi=="string"?multi:",";
return function(obj, common){
var value = obj[id] || obj.value;
if (!value) return "";
var ids = value.split(separator);
for (var i = 0; i < ids.length; i++)
ids[i] = options[ids[i]] || "";
return ids.join(", ");
};
} else {
return function(obj, common){
return options[obj[id]]||obj.value||"";
};
}
},
_bind_accesser:function(col, id, multi){
if (multi) {
var separator = typeof multi=="string"?multi:",";
return function(obj, common){
var value = obj[id] || obj.value;
if (!value) return "";
var ids = value.split(separator);
for (var i = 0; i < ids.length; i++){
var data = col.data.pull[ids[i]];
ids[i] = data ? (data.value || "") : "";
}
return ids.join(", ");
};
} else {
return function(obj, common){
var prop = obj[id]||obj.value,
data = col.data.pull[prop];
if (data && (data.value || data.value ===0))
return data.value;
return "";
};
}
}
};
|