{
name: 'filter',
type: AV.Types.PLATFORM_OBJECT,
- values: [tizen.AttributeFilter,
+ values: [
+ tizen.AttributeFilter,
tizen.AttributeRangeFilter,
- tizen.CompositeFilter],
+ tizen.CompositeFilter
+ ],
optional: true,
nullable: true
},
});
//TODO: Move filtering to native code
- //try {
- // _contacts = Common.filter(_contacts, args.filter);
- //} catch (e) {
- // native_.callIfPossible(errorCallback, e);
- // return;
- //}
+ try {
+ _contacts = C.filter(_contacts, args.filter);
+ } catch (e) {
+ native_.callIfPossible(errorCallback, e);
+ return;
+ }
//TODO: Move sorting to native code
- //_contacts = Common.sort(_contacts, args.sortMode);
+ _contacts = C.sort(_contacts, args.sortMode);
native_.callIfPossible(successCallback, _contacts);
};
};
var _editGuard = new EditGuard();
+
+//TODO: Move sorting and filtering to native code
+var Common = function() {};
+Common.prototype.sort = function(arr, sortMode) {
+ var _getSortProperty = function(obj, props) {
+ for (var i = 0; i < props.length; ++i) {
+ if (!obj.hasOwnProperty(props[i])) {
+ return null;
+ }
+ obj = obj[props[i]];
+ }
+ return obj;
+ };
+
+ if (sortMode instanceof tizen.SortMode) {
+ var props = sortMode.attributeName.split('.');
+ arr.sort(function(a, b) {
+ var aValue = _getSortProperty(a, props);
+ var bValue = _getSortProperty(b, props);
+
+ if (sortMode.order === 'DESC') {
+ return aValue < bValue;
+ }
+ return bValue < aValue;
+ });
+ }
+ return arr;
+};
+
+Common.prototype.filter = function(arr, filter) {
+ if (Type.isNullOrUndefined(arr))
+ return arr;
+ if (filter instanceof tizen.AttributeFilter ||
+ filter instanceof tizen.AttributeRangeFilter ||
+ filter instanceof tizen.CompositeFilter) {
+ arr = arr.filter(function(element) {
+ return filter._filter(element);
+ });
+ }
+ return arr;
+};
+
+Common.prototype.repackFilter = function (filter) {
+ if (filter instanceof tizen.AttributeFilter) {
+ return {
+ filterType: 'AttributeFilter',
+ attributeName: filter.attributeName,
+ matchFlag: filter.matchFlag,
+ matchValue: filter.matchValue
+ };
+ }
+ if (filter instanceof tizen.AttributeRangeFilter) {
+ return {
+ filterType: 'AttributeRangeFilter',
+ attributeName: filter.attributeName,
+ initialValue: Type.isNullOrUndefined(filter.initialValue) ? null : filter.initialValue,
+ endValue: Type.isNullOrUndefined(filter.endValue) ? null : filter.endValue
+ };
+ }
+ if (filter instanceof tizen.CompositeFilter) {
+ var _f = [];
+ var filters = filter.filters;
+
+ for (var i = 0; i < filters.length; ++i) {
+ _f.push(this.repackFilter(filters[i]));
+ }
+
+ return {
+ filterType: 'CompositeFilter',
+ type: filter.type,
+ filters: _f
+ };
+ }
+
+ return null;
+};
+
+var C = new Common();
// TODO implement contact filtering/sorting.
var data = {
- filter: null, //Common.repackFilter(args.filter),
+ filter: C.repackFilter(args.filter),
sortMode: args.sortMode
};
retval.push(self.get(String(_result[i])));
}
//TODO: Move sorting to native code
- //retval = Common.sort(retval, args.sortMode);
+ retval = C.sort(retval, args.sortMode);
args.successCallback(retval);
} else {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));