1 define([], function () {
4 function FilterNodeWrapperService(constants){
8 wrapForFilter: wrapForFilter,
13 // TODO: add service's description
16 node.childrenFilterConditions = function (children){
17 var allowedNodeTypesForFilter = constants.ALLOWED_NODE_TYPES_FOR_FILTER,
18 conditionTypes = function (item){
19 return allowedNodeTypesForFilter.some(function (elem){
20 return elem === item.type;
22 conditionEmptyChildren = function (item){
23 return item.children.some(function (child){
24 return (child.type !== 'leaf-list' && child.type !== 'list');
26 conditionChildDescription = function (item){
27 return !(item.children.every(function (childDes){
28 return childDes.type === 'description';
31 return children.filter(function (item){
32 if (item.parent.type === 'leaf' || item.parent.parent.type === 'leaf'){
35 return conditionTypes(item) && conditionEmptyChildren(item) && conditionChildDescription(item);
40 node.getChildrenForFilter = function () {
41 return node.childrenFilterConditions(node.getChildren(null, null, constants.NODE_UI_DISPLAY, null));
44 node.deepCopyForFilter = function (additionalProperties) {
45 var copy = node.getCleanCopy(),
47 allowedLeafTypesForFilter = constants.ALLOWED_LEAF_TYPES_FOR_FILTER,
49 addFilterProps = function (childrenArray){
50 if (childrenArray.type === 'leaf' && childrenArray.children && childrenArray.children.length){
51 if (childrenArray.children.some(function (item){
52 return item.type === 'type' && allowedLeafTypesForFilter.indexOf(item.label) !== -1;
54 childrenArray.filterType = '=';
56 if (childrenArray.children.some(function (item){
57 return item.type === 'type' && item.label === 'bits';
59 childrenArray.filterBitsValue = '';
61 if (childrenArray.children.some(function (item){
62 return item.type === 'type' && item.label === 'enumeration';
64 childrenArray.filterSelectboxBitsValue = [];
69 additionalProperties = additionalProperties || ['pathString'];
71 additionalProperties.forEach(function (prop) {
72 if (prop !== 'children' && self.hasOwnProperty(prop) && copy.hasOwnProperty(prop) === false) {
73 copy[prop] = self[prop];
77 this.childrenFilterConditions(this.children).forEach(function (child) {
79 if (child.type === 'leaf'){
80 childCopy = child.deepCopy();
83 childCopy = child.deepCopyForFilter();
86 childCopy.parent = copy;
88 addFilterProps(childCopy);
90 copy.children.push(childCopy);
99 // TODO: add service's description
100 function wrapForFilter(node) {
101 var comparePropToElemByName = function (propName, elemName) {
102 // TODO: also check by namespace - redundancy?
103 return (propName.indexOf(':') > -1 ? propName.split(':')[1] : propName) === elemName;
108 wrapFilter: function (node) {
109 if (this.hasOwnProperty(node.type)) {
110 this[node.type](node);
114 wrapAllFilter: function (node) {
116 self.wrapFilter(node);
117 node.children.forEach(function (child) {
118 self.wrapAllFilter(child);
122 leaf: function (node) {
123 var auxBuildRequest = node.buildRequest,
125 auxClear = node.clear,
126 fnToString = function (string) {
129 valueStr = string.toString();
131 console.warn('cannot convert value', node.value);
136 node.expandedBits = false;
138 node.filterRangeFrom = '';
139 node.filterRangeTo = '';
141 node.buildRequest = function (builder, req) {
142 auxBuildRequest(builder, req);
144 valueStr = fnToString(node.value);
146 var filterTypeArray = {
147 '=': function (element, filterValue, i){
148 return element ? element[i] === filterValue : false;
150 '>': function (element, filterValue, i){
151 return element ? element[i] > filterValue : false;
153 '>=': function (element, filterValue, i){
154 return element ? element[i] >= filterValue : false;
156 '<': function (element, filterValue, i){
157 return element ? element[i] < filterValue : false;
159 '<=': function (element, filterValue, i){
160 return element ? element[i] <= filterValue : false;
162 'contains': function (element, filterValue, i){
163 return element ? element[i] && element[i].indexOf(filterValue) > -1 : false;
165 'regExp': function (element, filterValue, i){
166 var testRegExp = function (patternString, nodeValue) {
167 var pattern = new RegExp(patternString);
168 return pattern.test(nodeValue);
170 return element ? testRegExp(filterValue, element[i]) : false;
172 'range': function (element, from, to, i){
174 return element ? element[i] <= to && element[i] >= from : false;
176 return element ? element[i] >= from : false;
178 return element ? element[i] <= to : false;
182 if (valueStr || (node.filterBitsValue && node.filterBitsValue !== '') ||
183 (node.filterSelectboxBitsValue && node.filterSelectboxBitsValue.length) ||
184 (node.filterRangeFrom && node.filterRangeFrom !== '') ||
185 (node.filterRangeTo && node.filterRangeTo !== '')){
189 if (node.filterSelectboxBitsValue && node.filterSelectboxBitsValue.length){
190 reqFilter.selectboxBitsValue = node.filterSelectboxBitsValue;
191 reqFilter.getResult = function (element, filterValue, i){
192 var selectSomeFun = function (filterSelectboxBitsValue, el){
193 return filterSelectboxBitsValue.some(function (item){
197 return element[i] && selectSomeFun(filterValue, element[i]);
200 if (node.filterBitsValue && node.filterBitsValue !== ''){
201 reqFilter.bitsValue = node.filterBitsValue;
203 reqFilter.value = valueStr;
206 if (node.filterType){
207 reqFilter.filterType = node.filterType;
209 reqFilter.filterType = '=';
212 if (node.filterRangeFrom){
213 reqFilter.filterRangeFrom = node.filterRangeFrom;
216 if (node.filterRangeTo){
217 reqFilter.filterRangeTo = node.filterRangeTo;
220 reqFilter.getFilterResult = filterTypeArray;
223 builder.insertPropertyToObj(req, node.label, reqFilter);
230 node.fill = function (name, data) {
232 if (data.hasOwnProperty('value')){
233 auxFill(name, data.value);
237 if (data.hasOwnProperty('filterType')){
238 match = comparePropToElemByName(name, node.label);
240 node.filterType = data.filterType;
243 if (data.hasOwnProperty('bitsValue')){
244 match = comparePropToElemByName(name, node.label);
246 node.filterBitsValue = data.bitsValue;
249 if (data.hasOwnProperty('selectboxBitsValue')){
250 match = comparePropToElemByName(name, node.label);
252 node.filterSelectboxBitsValue = data.selectboxBitsValue;
255 if (data.hasOwnProperty('filterRangeFrom')){
256 match = comparePropToElemByName(name, node.label);
258 node.filterRangeFrom = data.filterRangeFrom;
261 if (data.hasOwnProperty('filterRangeTo')){
262 match = comparePropToElemByName(name, node.label);
264 node.filterRangeTo = data.filterRangeTo;
268 console.error('fill data are empty');
272 node.clear = function () {
276 if (node.filterType){
277 node.filterType = '=';
279 if (node.filterBitsValue){
280 node.filterBitsValue = '';
282 if (node.filterSelectboxBitsValue){
283 node.filterSelectboxBitsValue = [];
285 if (node.filterRangeFrom){
286 node.filterRangeFrom = '';
288 if (node.filterRangeTo){
289 node.filterRangeTo = '';
295 type: function (node) {
298 length: function (node) {
301 range: function (node) {
304 pattern: function (node) {
307 container: function (node) {
310 rpc: function (node) {
313 input: function (node) {
316 output: function (node) {
319 case: function (node) {
322 choice: function (node) {
325 'leaf-list': function (node) {
328 key: function (node) {
331 list: function (node) {
335 _listElem: function (node) {
339 wrapperFilter.wrapAllFilter(node);
343 FilterNodeWrapperService.$inject = ['constants'];
345 return FilterNodeWrapperService;