diff src/djol/VectorStoreQuery.js @ 8:f0035923ff3e

добавлена библиотека для работы с openlayers 3+
author cin
date Mon, 21 Aug 2017 17:47:00 +0300
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/djol/VectorStoreQuery.js	Mon Aug 21 17:47:00 2017 +0300
@@ -0,0 +1,67 @@
+define([ "ol" ], function(ol) {
+
+    function buildFilter(filter) {
+        if (filter instanceof Function)
+            return filter;
+        if (filter) {
+            var match = function(str) {
+                return {
+                    test : function(x) {
+                        if (x === null || x === undefined)
+                            return false;
+                        return x.toString().startsWith(str);
+                    }
+                };
+            };
+
+            for ( var p in filter) {
+                if (typeof (filter[p]) == "string" && filter[p].endsWith("*"))
+                    filter[p] = match(filter[p].substr(0, filter[p].length));
+            }
+            return function(ft) {
+                for ( var p in filter) {
+                    if (filter[p] && filter[p].test ? !filter[p]
+                        .test(ft.get(p)) : ft.get(p) != filter[p])
+                        return false;
+                }
+                return true;
+            };
+        }
+        throw new Error("Unsupported filter");
+    }
+
+    /**
+     * @constructor
+     * @example
+     * 
+     * <pre>
+     * var store = new VectorStore({
+     *     source : vectorSource
+     * });
+     * 
+     * var req = new VectorStoreQuery({
+     *     city : &quot;Moscow&quot;
+     * }, [ 30, 50, 40, 60 ]);
+     * 
+     * store.query(req).then(showResults);
+     * </pre>
+     */
+    return function(filter, extent) {
+        var match = filter && buildFilter(filter);
+
+        var query = function(ft) {
+            if (extent) {
+                var g = gt.getGeometry();
+                if (!g || !ol.extent.intersects(extent, g.getExtent()))
+                    return false;
+            }
+
+            return !match || match(ft);
+        };
+
+        query.extent = extent;
+        query.predicate = match;
+
+        return query;
+    };
+});
\ No newline at end of file