[Testbot] Plone 5.0 - Python 2.7 - Build # 1338 - Failure!

jenkins at plone.org jenkins at plone.org
Thu Jan 30 23:44:10 UTC 2014


-------------------------------------------------------------------------------
Plone 5.0 - Python 2.7 - Build # 1338 - Failure!
-------------------------------------------------------------------------------

http://jenkins.plone.org/job/plone-5.0-python-2.7/1338/


-------------------------------------------------------------------------------
CHANGES
-------------------------------------------------------------------------------

Repository: mockup
Branch: refs/heads/master
Date: 2014-01-30T13:36:23-08:00
Author: Nathan Van Gheem (vangheem) <vangheem at gmail.com>
Commit: https://github.com/plone/mockup/commit/186c5a413d68a7ac73df36c9dff7c30b8eb5da31

move queryhelper pattern into utils module

Files changed:
M js/config.js
M js/patterns/livesearch.js
M js/patterns/relateditems.js
M js/patterns/structure/pattern.js
M js/utils.js
M tests/utils-test.js
D js/patterns/queryhelper.js

diff --git a/js/config.js b/js/config.js
index ac29c1d..d6addb0 100644
--- a/js/config.js
+++ b/js/config.js
@@ -59,7 +59,6 @@ var requirejsOptions = {
     'mockup-patterns-pickadate': 'js/patterns/pickadate',
     'mockup-patterns-picture': 'js/patterns/picture',
     'mockup-patterns-preventdoublesubmit': 'js/patterns/preventdoublesubmit',
-    'mockup-patterns-queryhelper': 'js/patterns/queryhelper',
     'mockup-patterns-querystring': 'js/patterns/querystring',
     'mockup-patterns-relateditems': 'js/patterns/relateditems',
     'mockup-patterns-select2': 'js/patterns/select2',
diff --git a/js/patterns/livesearch.js b/js/patterns/livesearch.js
index 9fbacea..8684e35 100644
--- a/js/patterns/livesearch.js
+++ b/js/patterns/livesearch.js
@@ -64,8 +64,8 @@ define([
   'mockup-patterns-base',
   'mockup-patterns-toggle',
   'mockup-patterns-select2', // TODO: is this still a dependency
-  'mockup-patterns-queryhelper'
-], function($, _, Base, Toggle, Select2, QueryHelper) {
+  'mockup-utils'
+], function($, _, Base, Toggle, Select2, utils) {
   "use strict";
 
   var Livesearch = Base.extend({
@@ -124,8 +124,8 @@ define([
 
     init: function() {
       var self = this;
-      self.query = new QueryHelper(self.$el,
-        $.extend(true, {}, self.options, {basePattern: self}));
+      self.query = new utils.QueryHelper(
+          $.extend(true, {}, self.options, {pattern: self}));
 
       this.$results = $(self.options.resultsTarget, self.$el);
 
diff --git a/js/patterns/queryhelper.js b/js/patterns/queryhelper.js
deleted file mode 100644
index 333015c..0000000
--- a/js/patterns/queryhelper.js
+++ /dev/null
@@ -1,214 +0,0 @@
-/* Queryhelper pattern.
- *
- * Options:
- *    basePattern(object): TODO (null)
- *    vocabularyUrl(string): TODO (null)
- *    searchParam(string): query string param to pass to search url. ('SearchableText')
- *    attributes(array): TODO (['UID','Title', 'Description', 'getURL', 'Type'])
- *    batchSize(integer): number of results to retrive (10)
- *    baseCriteria(array): TODO ([])
- *    pathDepth(integer): TODO (1)
- *
- * Documentation:
- *    # TODO
- *
- * License:
- *    Copyright (C) 2010 Plone Foundation
- *
- *    This program is free software; you can redistribute it and/or modify it
- *    under the terms of the GNU General Public License as published by the
- *    Free Software Foundation; either version 2 of the License.
- *
- *    This program is distributed in the hope that it will be useful, but
- *    WITHOUT ANY WARRANTY; without even the implied warranty of
- *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- *    Public License for more details.
- *
- *    You should have received a copy of the GNU General Public License along
- *    with this program; if not, write to the Free Software Foundation, Inc.,
- *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-
-define([
-  'jquery',
-  'mockup-patterns-base'
-], function($, Base) {
-  "use strict";
-
-  var QueryHelper = Base.extend({
-    name: "queryhelper",
-    defaults: {
-      basePattern: null, // must be passed in
-      vocabularyUrl: null,
-      searchParam: 'SearchableText', // query string param to pass to search url
-      attributes: ['UID','Title', 'Description', 'getURL', 'Type'],
-      batchSize: 10, // number of results to retrive
-      baseCriteria: [],
-      pathDepth: 1
-    },
-
-    init: function() {
-      /* if basePattern argument provided, it can implement the interface of:
-       *    - browsing: boolean if currently browsing
-       *    - currentPath: string of current path to apply to search if browsing
-       *    - basePath: default path to provide if no subpath used
-       */
-      var self = this;
-      self.basePattern = self.options.basePattern;
-      if(self.basePattern === undefined){
-        self.basePattern = {
-          browsing: false,
-          basePath: '/'
-        };
-      }
-
-      if (self.options.url && !self.options.vocabularyUrl) {
-        self.options.vocabularyUrl = self.options.url;
-      }
-      if(self.options.vocabularyUrl !== undefined &&
-          self.options.vocabularyUrl !== null){
-        self.valid = true;
-      }else{
-        self.valid = false;
-      }
-    },
-    getCurrentPath: function(){
-      var self = this;
-      var pattern = self.basePattern;
-      var currentPath;
-      /* If currentPath is set on the QueryHelper object, use that first.
-       * Then, check on the pattern.
-       * Finally, see if it is a function and call it if it is.
-       */
-      if(self.currentPath){
-        currentPath = self.currentPath;
-      }else{
-        currentPath = pattern.currentPath;
-      }
-      if(typeof(currentPath) === 'function'){
-        currentPath = currentPath();
-      }
-      var path = currentPath;
-      if(!path){
-        if(pattern.options.basePath){
-          path = pattern.options.basePath;
-        }else{
-          path = '/';
-        }
-      }
-      return path;
-    },
-    getCriterias: function(term, options){
-      if(options === undefined){
-        options = {};
-      }
-      options = $.extend({}, {
-        useBaseCriteria: true,
-        additionalCriterias: []
-      }, options);
-
-      var self = this;
-      var criterias = [];
-      if(options.useBaseCriteria){
-        criterias = self.options.baseCriteria.slice(0);
-      }
-      if(term){
-        term += '*';
-        criterias.push({
-          i: self.options.searchParam,
-          o: 'plone.app.querystring.operation.string.contains',
-          v: term
-        });
-      }
-      var pattern = self.basePattern;
-      if(pattern.browsing){
-        criterias.push({
-          i: 'path',
-          o: 'plone.app.querystring.operation.string.path',
-          v: self.getCurrentPath() + '::' + self.options.pathDepth
-        });
-      }
-      criterias = criterias.concat(options.additionalCriterias);
-      return criterias;
-    },
-    getBatch: function(page){
-      if(!page){
-        page = 1;
-      }
-      var self = this;
-      return {
-        page: page,
-        size: self.options.batchSize
-      };
-    },
-    selectAjax: function(){
-      var self = this;
-      return {
-        url: self.options.vocabularyUrl,
-        dataType: 'JSON',
-        quietMillis: 100,
-        data: function(term, page) {
-          return self.getQueryData(term, page);
-        },
-        results: function (data, page) {
-          var more = (page * 10) < data.total; // whether or not there are more results available
-          // notice we return the value of more so Select2 knows if more results can be loaded
-          return {results: data.results, more: more};
-        }
-      };
-    },
-    getUrl: function(){
-      var self = this;
-      var url = self.options.vocabularyUrl;
-      if(url.indexOf('?') === -1){
-        url += '?';
-      }else{
-        url += '&';
-      }
-      return url + $.param(self.getQueryData());
-    },
-    getQueryData: function(term, page){
-      var self = this;
-      var data = {
-        query: JSON.stringify({
-          criteria: self.getCriterias(term)
-        }),
-        attributes: JSON.stringify(self.options.attributes)
-      };
-      if(page){
-        data.batch = JSON.stringify(self.getBatch(page));
-      }
-      return data;
-    },
-    search: function(term, operation, value, callback, useBaseCriteria){
-      if(useBaseCriteria === undefined){
-        useBaseCriteria = true;
-      }
-      var self = this;
-      var criteria = [];
-      if(useBaseCriteria){
-        criteria = self.options.baseCriteria.slice(0);
-      }
-      criteria.push({
-        i: term,
-        o: operation,
-        v: value
-      });
-      var data = {
-        query: JSON.stringify({ criteria: criteria }),
-        attributes: JSON.stringify(self.options.attributes)
-      };
-      $.ajax({
-        url: self.options.vocabularyUrl,
-        dataType: 'JSON',
-        data: data,
-        success: callback
-      });
-    }
-  });
-
-  return QueryHelper;
-
-});
-
diff --git a/js/patterns/relateditems.js b/js/patterns/relateditems.js
index 1965ef7..6523c5c 100644
--- a/js/patterns/relateditems.js
+++ b/js/patterns/relateditems.js
@@ -82,9 +82,9 @@ define([
   'underscore',
   'mockup-patterns-base',
   'mockup-patterns-select2',
-  'mockup-patterns-queryhelper',
+  'mockup-utils',
   'mockup-patterns-tree'
-], function($, _, Base, Select2, QueryHelper, Tree) {
+], function($, _, Base, Select2, utils, Tree) {
   "use strict";
 
   var RelatedItems = Base.extend({
@@ -323,14 +323,12 @@ define([
     init: function() {
       var self = this;
 
-      self.query = new QueryHelper(
-        self.$el,
-        $.extend(true, {}, self.options, {basePattern: self})
+      self.query = new utils.QueryHelper(
+        $.extend(true, {}, self.options, {pattern: self})
       );
-      self.treeQuery = new QueryHelper(
-        self.$el,
+      self.treeQuery = new utils.QueryHelper(
         $.extend(true, {}, self.options, {
-          basePattern: self,
+          pattern: self,
           baseCriteria: [{
             i: 'Type',
             o: 'plone.app.querystring.operation.list.contains',
diff --git a/js/patterns/structure/pattern.js b/js/patterns/structure/pattern.js
index cd7fdff..390e893 100644
--- a/js/patterns/structure/pattern.js
+++ b/js/patterns/structure/pattern.js
@@ -45,7 +45,7 @@
 define([
   'jquery',
   'mockup-patterns-base',
-  'mockup-patterns-queryhelper',
+  'mockup-utils',
   'js/patterns/structure/views/app',
   'text!js/patterns/structure/templates/paging.xml',
   'text!js/patterns/structure/templates/selection_button.xml',
@@ -53,7 +53,7 @@ define([
   'text!js/patterns/structure/templates/tablerow.xml',
   'text!js/patterns/structure/templates/table.xml',
   'text!js/ui/templates/popover.xml'
-], function($, Base, QueryHelper, AppView) {
+], function($, Base, utils, AppView) {
   "use strict";
 
   var Structure = Base.extend({
@@ -141,8 +141,8 @@ define([
       var self = this;
       self.browsing = true; // so all queries will be correct with QueryHelper
       self.options.collectionUrl = self.options.vocabularyUrl;
-      self.options.queryHelper = new QueryHelper(self.$el,
-        $.extend(true, {}, self.options, {basePattern: self}));
+      self.options.queryHelper = new utils.QueryHelper(
+        $.extend(true, {}, self.options, {pattern: self}));
 
       delete self.options.attributes; // not compatible with backbone
 
diff --git a/js/utils.js b/js/utils.js
index d29d203..58f9fbb 100644
--- a/js/utils.js
+++ b/js/utils.js
@@ -31,7 +31,182 @@ define([
 ], function($) {
   "use strict";
 
-  var utils = {
+  var QueryHelper = function(options){
+    /* if pattern argument provided, it can implement the interface of:
+      *    - browsing: boolean if currently browsing
+      *    - currentPath: string of current path to apply to search if browsing
+      *    - basePath: default path to provide if no subpath used
+      */
+
+    var self = this;
+    var defaults = {
+      pattern: null, // must be passed in
+      vocabularyUrl: null,
+      searchParam: 'SearchableText', // query string param to pass to search url
+      attributes: ['UID','Title', 'Description', 'getURL', 'Type'],
+      batchSize: 10, // number of results to retrive
+      baseCriteria: [],
+      pathDepth: 1
+    };
+    self.options = $.extend({}, defaults, options);
+    self.pattern = self.options.pattern;
+    if(self.pattern === undefined || self.pattern === null){
+      self.pattern = {
+        browsing: false,
+        basePath: '/'
+      };
+    }
+
+    if (self.options.url && !self.options.vocabularyUrl) {
+      self.options.vocabularyUrl = self.options.url;
+    }else if(self.pattern.vocabularyUrl){
+      self.options.vocabularyUrl = self.pattern.vocabularyUrl;
+    }
+    if(self.options.vocabularyUrl !== undefined &&
+        self.options.vocabularyUrl !== null){
+      self.valid = true;
+    }else{
+      self.valid = false;
+    }
+
+    self.getCurrentPath = function(){
+      var pattern = self.pattern;
+      var currentPath;
+      /* If currentPath is set on the QueryHelper object, use that first.
+       * Then, check on the pattern.
+       * Finally, see if it is a function and call it if it is.
+       */
+      if(self.currentPath){
+        currentPath = self.currentPath;
+      }else{
+        currentPath = pattern.currentPath;
+      }
+      if(typeof(currentPath) === 'function'){
+        currentPath = currentPath();
+      }
+      var path = currentPath;
+      if(!path){
+        if(pattern.basePath){
+          path = pattern.basePath;
+        }else if(pattern.options.basePath){
+          path = pattern.options.basePath;
+        }else{
+          path = '/';
+        }
+      }
+      return path;
+    };
+
+    self.getCriterias = function(term, options){
+      if(options === undefined){
+        options = {};
+      }
+      options = $.extend({}, {
+        useBaseCriteria: true,
+        additionalCriterias: []
+      }, options);
+
+      var criterias = [];
+      if(options.useBaseCriteria){
+        criterias = self.options.baseCriteria.slice(0);
+      }
+      if(term){
+        term += '*';
+        criterias.push({
+          i: self.options.searchParam,
+          o: 'plone.app.querystring.operation.string.contains',
+          v: term
+        });
+      }
+      if(self.pattern.browsing){
+        criterias.push({
+          i: 'path',
+          o: 'plone.app.querystring.operation.string.path',
+          v: self.getCurrentPath() + '::' + self.options.pathDepth
+        });
+      }
+      criterias = criterias.concat(options.additionalCriterias);
+      return criterias;
+    };
+
+    self.getBatch = function(page){
+      if(!page){
+        page = 1;
+      }
+      return {
+        page: page,
+        size: self.options.batchSize
+      };
+    };
+
+    self.selectAjax = function(){
+      return {
+        url: self.options.vocabularyUrl,
+        dataType: 'JSON',
+        quietMillis: 100,
+        data: function(term, page) {
+          return self.getQueryData(term, page);
+        },
+        results: function (data, page) {
+          var more = (page * 10) < data.total; // whether or not there are more results available
+          // notice we return the value of more so Select2 knows if more results can be loaded
+          return {results: data.results, more: more};
+        }
+      };
+    };
+
+    self.getUrl = function(){
+      var url = self.options.vocabularyUrl;
+      if(url.indexOf('?') === -1){
+        url += '?';
+      }else{
+        url += '&';
+      }
+      return url + $.param(self.getQueryData());
+    };
+
+    self.getQueryData = function(term, page){
+      var data = {
+        query: JSON.stringify({
+          criteria: self.getCriterias(term)
+        }),
+        attributes: JSON.stringify(self.options.attributes)
+      };
+      if(page){
+        data.batch = JSON.stringify(self.getBatch(page));
+      }
+      return data;
+    };
+
+    self.search = function(term, operation, value, callback, useBaseCriteria){
+      if(useBaseCriteria === undefined){
+        useBaseCriteria = true;
+      }
+      var criteria = [];
+      if(useBaseCriteria){
+        criteria = self.options.baseCriteria.slice(0);
+      }
+      criteria.push({
+        i: term,
+        o: operation,
+        v: value
+      });
+      var data = {
+        query: JSON.stringify({ criteria: criteria }),
+        attributes: JSON.stringify(self.options.attributes)
+      };
+      $.ajax({
+        url: self.options.vocabularyUrl,
+        dataType: 'JSON',
+        data: data,
+        success: callback
+      });
+    };
+
+    return self;
+  };
+
+  return {
 
     parseBodyTag: function(txt){
       return $((/<body[^>]*>((.|[\n\r])*)<\/body>/im).exec(txt)[0]
@@ -58,9 +233,7 @@ define([
         val = $.trim(val).toLowerCase();
       }
       return ['true', true, 1].indexOf(val) !== -1;
-    }
+    },
+    QueryHelper: QueryHelper
   };
-
-  return utils;
-
 });
diff --git a/tests/utils-test.js b/tests/utils-test.js
index 03d8ac2..52d7607 100644
--- a/tests/utils-test.js
+++ b/tests/utils-test.js
@@ -103,6 +103,62 @@ define([
       });
     });
 
+
+    describe('QueryHelper', function() {
+
+      it("getQueryData correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var qd = qh.getQueryData('foobar');
+        expect(qd.query).to.equal('{"criteria":[{"i":"SearchableText","o":"plone.app.querystring.operation.string.contains","v":"foobar*"}]}');
+      });
+      it("getQueryData use attributes correctly", function() {
+        var qh = new utils.QueryHelper({
+          vocabularyUrl: 'http://foobar.com/',
+          attributes: ['one', 'two']});
+        var qd = qh.getQueryData('foobar');
+        expect(qd.attributes).to.equal('["one","two"]');
+      });
+      it("getQueryData set batch", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var qd = qh.getQueryData('foobar', 1);
+        expect(qd.batch).to.equal('{"page":1,"size":' + qh.options.batchSize + '}');
+      });
+
+      it("selectAjax gets data correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var sa = qh.selectAjax();
+        expect(sa.data('foobar').query).to.equal('{"criteria":[{"i":"SearchableText","o":"plone.app.querystring.operation.string.contains","v":"foobar*"}]}');
+      });
+      it("selectAjax formats results correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var sa = qh.selectAjax();
+        var data = sa.results({total: 100, results: [1,2,3]}, 1);
+        expect(data.results.length).to.equal(3);
+        expect(data.more).to.equal(true);
+      });
+
+      it("getUrl correct", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        expect(qh.getUrl()).to.equal(
+          'http://foobar.com/?query=%7B%22criteria%22%3A%5B%5D%7D&attributes=%5B%22UID%22%2C%22Title%22%2C%22Description%22%2C%22getURL%22%2C%22Type%22%5D');
+      });
+      it("getUrl correct and url query params already present", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/?foo=bar'});
+        expect(qh.getUrl()).to.equal(
+          'http://foobar.com/?foo=bar&query=%7B%22criteria%22%3A%5B%5D%7D&attributes=%5B%22UID%22%2C%22Title%22%2C%22Description%22%2C%22getURL%22%2C%22Type%22%5D');
+      });
+
+
+      it("browsing adds path criteria", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/?foo=bar'});
+        qh.pattern.browsing = true;
+        expect(qh.getQueryData('foobar').query).to.contain('plone.app.querystring.operation.string.path');
+      });
+
+
+    });
+
+
   });
 
 });


Repository: mockup
Branch: refs/heads/master
Date: 2014-01-30T14:54:53-08:00
Author: Rok Garbas (garbas) <rok at garbas.si>
Commit: https://github.com/plone/mockup/commit/24e0d7d2266b308613137c071f5b187176aa175a

Merge pull request #261 from plone/vangheem-queryhelper

move queryhelper pattern into utils module

Files changed:
M js/config.js
M js/patterns/livesearch.js
M js/patterns/relateditems.js
M js/patterns/structure/pattern.js
M js/utils.js
M tests/utils-test.js
D js/patterns/queryhelper.js

diff --git a/js/config.js b/js/config.js
index ac29c1d..d6addb0 100644
--- a/js/config.js
+++ b/js/config.js
@@ -59,7 +59,6 @@ var requirejsOptions = {
     'mockup-patterns-pickadate': 'js/patterns/pickadate',
     'mockup-patterns-picture': 'js/patterns/picture',
     'mockup-patterns-preventdoublesubmit': 'js/patterns/preventdoublesubmit',
-    'mockup-patterns-queryhelper': 'js/patterns/queryhelper',
     'mockup-patterns-querystring': 'js/patterns/querystring',
     'mockup-patterns-relateditems': 'js/patterns/relateditems',
     'mockup-patterns-select2': 'js/patterns/select2',
diff --git a/js/patterns/livesearch.js b/js/patterns/livesearch.js
index 9fbacea..8684e35 100644
--- a/js/patterns/livesearch.js
+++ b/js/patterns/livesearch.js
@@ -64,8 +64,8 @@ define([
   'mockup-patterns-base',
   'mockup-patterns-toggle',
   'mockup-patterns-select2', // TODO: is this still a dependency
-  'mockup-patterns-queryhelper'
-], function($, _, Base, Toggle, Select2, QueryHelper) {
+  'mockup-utils'
+], function($, _, Base, Toggle, Select2, utils) {
   "use strict";
 
   var Livesearch = Base.extend({
@@ -124,8 +124,8 @@ define([
 
     init: function() {
       var self = this;
-      self.query = new QueryHelper(self.$el,
-        $.extend(true, {}, self.options, {basePattern: self}));
+      self.query = new utils.QueryHelper(
+          $.extend(true, {}, self.options, {pattern: self}));
 
       this.$results = $(self.options.resultsTarget, self.$el);
 
diff --git a/js/patterns/queryhelper.js b/js/patterns/queryhelper.js
deleted file mode 100644
index 333015c..0000000
--- a/js/patterns/queryhelper.js
+++ /dev/null
@@ -1,214 +0,0 @@
-/* Queryhelper pattern.
- *
- * Options:
- *    basePattern(object): TODO (null)
- *    vocabularyUrl(string): TODO (null)
- *    searchParam(string): query string param to pass to search url. ('SearchableText')
- *    attributes(array): TODO (['UID','Title', 'Description', 'getURL', 'Type'])
- *    batchSize(integer): number of results to retrive (10)
- *    baseCriteria(array): TODO ([])
- *    pathDepth(integer): TODO (1)
- *
- * Documentation:
- *    # TODO
- *
- * License:
- *    Copyright (C) 2010 Plone Foundation
- *
- *    This program is free software; you can redistribute it and/or modify it
- *    under the terms of the GNU General Public License as published by the
- *    Free Software Foundation; either version 2 of the License.
- *
- *    This program is distributed in the hope that it will be useful, but
- *    WITHOUT ANY WARRANTY; without even the implied warranty of
- *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- *    Public License for more details.
- *
- *    You should have received a copy of the GNU General Public License along
- *    with this program; if not, write to the Free Software Foundation, Inc.,
- *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-
-define([
-  'jquery',
-  'mockup-patterns-base'
-], function($, Base) {
-  "use strict";
-
-  var QueryHelper = Base.extend({
-    name: "queryhelper",
-    defaults: {
-      basePattern: null, // must be passed in
-      vocabularyUrl: null,
-      searchParam: 'SearchableText', // query string param to pass to search url
-      attributes: ['UID','Title', 'Description', 'getURL', 'Type'],
-      batchSize: 10, // number of results to retrive
-      baseCriteria: [],
-      pathDepth: 1
-    },
-
-    init: function() {
-      /* if basePattern argument provided, it can implement the interface of:
-       *    - browsing: boolean if currently browsing
-       *    - currentPath: string of current path to apply to search if browsing
-       *    - basePath: default path to provide if no subpath used
-       */
-      var self = this;
-      self.basePattern = self.options.basePattern;
-      if(self.basePattern === undefined){
-        self.basePattern = {
-          browsing: false,
-          basePath: '/'
-        };
-      }
-
-      if (self.options.url && !self.options.vocabularyUrl) {
-        self.options.vocabularyUrl = self.options.url;
-      }
-      if(self.options.vocabularyUrl !== undefined &&
-          self.options.vocabularyUrl !== null){
-        self.valid = true;
-      }else{
-        self.valid = false;
-      }
-    },
-    getCurrentPath: function(){
-      var self = this;
-      var pattern = self.basePattern;
-      var currentPath;
-      /* If currentPath is set on the QueryHelper object, use that first.
-       * Then, check on the pattern.
-       * Finally, see if it is a function and call it if it is.
-       */
-      if(self.currentPath){
-        currentPath = self.currentPath;
-      }else{
-        currentPath = pattern.currentPath;
-      }
-      if(typeof(currentPath) === 'function'){
-        currentPath = currentPath();
-      }
-      var path = currentPath;
-      if(!path){
-        if(pattern.options.basePath){
-          path = pattern.options.basePath;
-        }else{
-          path = '/';
-        }
-      }
-      return path;
-    },
-    getCriterias: function(term, options){
-      if(options === undefined){
-        options = {};
-      }
-      options = $.extend({}, {
-        useBaseCriteria: true,
-        additionalCriterias: []
-      }, options);
-
-      var self = this;
-      var criterias = [];
-      if(options.useBaseCriteria){
-        criterias = self.options.baseCriteria.slice(0);
-      }
-      if(term){
-        term += '*';
-        criterias.push({
-          i: self.options.searchParam,
-          o: 'plone.app.querystring.operation.string.contains',
-          v: term
-        });
-      }
-      var pattern = self.basePattern;
-      if(pattern.browsing){
-        criterias.push({
-          i: 'path',
-          o: 'plone.app.querystring.operation.string.path',
-          v: self.getCurrentPath() + '::' + self.options.pathDepth
-        });
-      }
-      criterias = criterias.concat(options.additionalCriterias);
-      return criterias;
-    },
-    getBatch: function(page){
-      if(!page){
-        page = 1;
-      }
-      var self = this;
-      return {
-        page: page,
-        size: self.options.batchSize
-      };
-    },
-    selectAjax: function(){
-      var self = this;
-      return {
-        url: self.options.vocabularyUrl,
-        dataType: 'JSON',
-        quietMillis: 100,
-        data: function(term, page) {
-          return self.getQueryData(term, page);
-        },
-        results: function (data, page) {
-          var more = (page * 10) < data.total; // whether or not there are more results available
-          // notice we return the value of more so Select2 knows if more results can be loaded
-          return {results: data.results, more: more};
-        }
-      };
-    },
-    getUrl: function(){
-      var self = this;
-      var url = self.options.vocabularyUrl;
-      if(url.indexOf('?') === -1){
-        url += '?';
-      }else{
-        url += '&';
-      }
-      return url + $.param(self.getQueryData());
-    },
-    getQueryData: function(term, page){
-      var self = this;
-      var data = {
-        query: JSON.stringify({
-          criteria: self.getCriterias(term)
-        }),
-        attributes: JSON.stringify(self.options.attributes)
-      };
-      if(page){
-        data.batch = JSON.stringify(self.getBatch(page));
-      }
-      return data;
-    },
-    search: function(term, operation, value, callback, useBaseCriteria){
-      if(useBaseCriteria === undefined){
-        useBaseCriteria = true;
-      }
-      var self = this;
-      var criteria = [];
-      if(useBaseCriteria){
-        criteria = self.options.baseCriteria.slice(0);
-      }
-      criteria.push({
-        i: term,
-        o: operation,
-        v: value
-      });
-      var data = {
-        query: JSON.stringify({ criteria: criteria }),
-        attributes: JSON.stringify(self.options.attributes)
-      };
-      $.ajax({
-        url: self.options.vocabularyUrl,
-        dataType: 'JSON',
-        data: data,
-        success: callback
-      });
-    }
-  });
-
-  return QueryHelper;
-
-});
-
diff --git a/js/patterns/relateditems.js b/js/patterns/relateditems.js
index 1965ef7..6523c5c 100644
--- a/js/patterns/relateditems.js
+++ b/js/patterns/relateditems.js
@@ -82,9 +82,9 @@ define([
   'underscore',
   'mockup-patterns-base',
   'mockup-patterns-select2',
-  'mockup-patterns-queryhelper',
+  'mockup-utils',
   'mockup-patterns-tree'
-], function($, _, Base, Select2, QueryHelper, Tree) {
+], function($, _, Base, Select2, utils, Tree) {
   "use strict";
 
   var RelatedItems = Base.extend({
@@ -323,14 +323,12 @@ define([
     init: function() {
       var self = this;
 
-      self.query = new QueryHelper(
-        self.$el,
-        $.extend(true, {}, self.options, {basePattern: self})
+      self.query = new utils.QueryHelper(
+        $.extend(true, {}, self.options, {pattern: self})
       );
-      self.treeQuery = new QueryHelper(
-        self.$el,
+      self.treeQuery = new utils.QueryHelper(
         $.extend(true, {}, self.options, {
-          basePattern: self,
+          pattern: self,
           baseCriteria: [{
             i: 'Type',
             o: 'plone.app.querystring.operation.list.contains',
diff --git a/js/patterns/structure/pattern.js b/js/patterns/structure/pattern.js
index cd7fdff..390e893 100644
--- a/js/patterns/structure/pattern.js
+++ b/js/patterns/structure/pattern.js
@@ -45,7 +45,7 @@
 define([
   'jquery',
   'mockup-patterns-base',
-  'mockup-patterns-queryhelper',
+  'mockup-utils',
   'js/patterns/structure/views/app',
   'text!js/patterns/structure/templates/paging.xml',
   'text!js/patterns/structure/templates/selection_button.xml',
@@ -53,7 +53,7 @@ define([
   'text!js/patterns/structure/templates/tablerow.xml',
   'text!js/patterns/structure/templates/table.xml',
   'text!js/ui/templates/popover.xml'
-], function($, Base, QueryHelper, AppView) {
+], function($, Base, utils, AppView) {
   "use strict";
 
   var Structure = Base.extend({
@@ -141,8 +141,8 @@ define([
       var self = this;
       self.browsing = true; // so all queries will be correct with QueryHelper
       self.options.collectionUrl = self.options.vocabularyUrl;
-      self.options.queryHelper = new QueryHelper(self.$el,
-        $.extend(true, {}, self.options, {basePattern: self}));
+      self.options.queryHelper = new utils.QueryHelper(
+        $.extend(true, {}, self.options, {pattern: self}));
 
       delete self.options.attributes; // not compatible with backbone
 
diff --git a/js/utils.js b/js/utils.js
index d29d203..58f9fbb 100644
--- a/js/utils.js
+++ b/js/utils.js
@@ -31,7 +31,182 @@ define([
 ], function($) {
   "use strict";
 
-  var utils = {
+  var QueryHelper = function(options){
+    /* if pattern argument provided, it can implement the interface of:
+      *    - browsing: boolean if currently browsing
+      *    - currentPath: string of current path to apply to search if browsing
+      *    - basePath: default path to provide if no subpath used
+      */
+
+    var self = this;
+    var defaults = {
+      pattern: null, // must be passed in
+      vocabularyUrl: null,
+      searchParam: 'SearchableText', // query string param to pass to search url
+      attributes: ['UID','Title', 'Description', 'getURL', 'Type'],
+      batchSize: 10, // number of results to retrive
+      baseCriteria: [],
+      pathDepth: 1
+    };
+    self.options = $.extend({}, defaults, options);
+    self.pattern = self.options.pattern;
+    if(self.pattern === undefined || self.pattern === null){
+      self.pattern = {
+        browsing: false,
+        basePath: '/'
+      };
+    }
+
+    if (self.options.url && !self.options.vocabularyUrl) {
+      self.options.vocabularyUrl = self.options.url;
+    }else if(self.pattern.vocabularyUrl){
+      self.options.vocabularyUrl = self.pattern.vocabularyUrl;
+    }
+    if(self.options.vocabularyUrl !== undefined &&
+        self.options.vocabularyUrl !== null){
+      self.valid = true;
+    }else{
+      self.valid = false;
+    }
+
+    self.getCurrentPath = function(){
+      var pattern = self.pattern;
+      var currentPath;
+      /* If currentPath is set on the QueryHelper object, use that first.
+       * Then, check on the pattern.
+       * Finally, see if it is a function and call it if it is.
+       */
+      if(self.currentPath){
+        currentPath = self.currentPath;
+      }else{
+        currentPath = pattern.currentPath;
+      }
+      if(typeof(currentPath) === 'function'){
+        currentPath = currentPath();
+      }
+      var path = currentPath;
+      if(!path){
+        if(pattern.basePath){
+          path = pattern.basePath;
+        }else if(pattern.options.basePath){
+          path = pattern.options.basePath;
+        }else{
+          path = '/';
+        }
+      }
+      return path;
+    };
+
+    self.getCriterias = function(term, options){
+      if(options === undefined){
+        options = {};
+      }
+      options = $.extend({}, {
+        useBaseCriteria: true,
+        additionalCriterias: []
+      }, options);
+
+      var criterias = [];
+      if(options.useBaseCriteria){
+        criterias = self.options.baseCriteria.slice(0);
+      }
+      if(term){
+        term += '*';
+        criterias.push({
+          i: self.options.searchParam,
+          o: 'plone.app.querystring.operation.string.contains',
+          v: term
+        });
+      }
+      if(self.pattern.browsing){
+        criterias.push({
+          i: 'path',
+          o: 'plone.app.querystring.operation.string.path',
+          v: self.getCurrentPath() + '::' + self.options.pathDepth
+        });
+      }
+      criterias = criterias.concat(options.additionalCriterias);
+      return criterias;
+    };
+
+    self.getBatch = function(page){
+      if(!page){
+        page = 1;
+      }
+      return {
+        page: page,
+        size: self.options.batchSize
+      };
+    };
+
+    self.selectAjax = function(){
+      return {
+        url: self.options.vocabularyUrl,
+        dataType: 'JSON',
+        quietMillis: 100,
+        data: function(term, page) {
+          return self.getQueryData(term, page);
+        },
+        results: function (data, page) {
+          var more = (page * 10) < data.total; // whether or not there are more results available
+          // notice we return the value of more so Select2 knows if more results can be loaded
+          return {results: data.results, more: more};
+        }
+      };
+    };
+
+    self.getUrl = function(){
+      var url = self.options.vocabularyUrl;
+      if(url.indexOf('?') === -1){
+        url += '?';
+      }else{
+        url += '&';
+      }
+      return url + $.param(self.getQueryData());
+    };
+
+    self.getQueryData = function(term, page){
+      var data = {
+        query: JSON.stringify({
+          criteria: self.getCriterias(term)
+        }),
+        attributes: JSON.stringify(self.options.attributes)
+      };
+      if(page){
+        data.batch = JSON.stringify(self.getBatch(page));
+      }
+      return data;
+    };
+
+    self.search = function(term, operation, value, callback, useBaseCriteria){
+      if(useBaseCriteria === undefined){
+        useBaseCriteria = true;
+      }
+      var criteria = [];
+      if(useBaseCriteria){
+        criteria = self.options.baseCriteria.slice(0);
+      }
+      criteria.push({
+        i: term,
+        o: operation,
+        v: value
+      });
+      var data = {
+        query: JSON.stringify({ criteria: criteria }),
+        attributes: JSON.stringify(self.options.attributes)
+      };
+      $.ajax({
+        url: self.options.vocabularyUrl,
+        dataType: 'JSON',
+        data: data,
+        success: callback
+      });
+    };
+
+    return self;
+  };
+
+  return {
 
     parseBodyTag: function(txt){
       return $((/<body[^>]*>((.|[\n\r])*)<\/body>/im).exec(txt)[0]
@@ -58,9 +233,7 @@ define([
         val = $.trim(val).toLowerCase();
       }
       return ['true', true, 1].indexOf(val) !== -1;
-    }
+    },
+    QueryHelper: QueryHelper
   };
-
-  return utils;
-
 });
diff --git a/tests/utils-test.js b/tests/utils-test.js
index 03d8ac2..52d7607 100644
--- a/tests/utils-test.js
+++ b/tests/utils-test.js
@@ -103,6 +103,62 @@ define([
       });
     });
 
+
+    describe('QueryHelper', function() {
+
+      it("getQueryData correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var qd = qh.getQueryData('foobar');
+        expect(qd.query).to.equal('{"criteria":[{"i":"SearchableText","o":"plone.app.querystring.operation.string.contains","v":"foobar*"}]}');
+      });
+      it("getQueryData use attributes correctly", function() {
+        var qh = new utils.QueryHelper({
+          vocabularyUrl: 'http://foobar.com/',
+          attributes: ['one', 'two']});
+        var qd = qh.getQueryData('foobar');
+        expect(qd.attributes).to.equal('["one","two"]');
+      });
+      it("getQueryData set batch", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var qd = qh.getQueryData('foobar', 1);
+        expect(qd.batch).to.equal('{"page":1,"size":' + qh.options.batchSize + '}');
+      });
+
+      it("selectAjax gets data correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var sa = qh.selectAjax();
+        expect(sa.data('foobar').query).to.equal('{"criteria":[{"i":"SearchableText","o":"plone.app.querystring.operation.string.contains","v":"foobar*"}]}');
+      });
+      it("selectAjax formats results correctly", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        var sa = qh.selectAjax();
+        var data = sa.results({total: 100, results: [1,2,3]}, 1);
+        expect(data.results.length).to.equal(3);
+        expect(data.more).to.equal(true);
+      });
+
+      it("getUrl correct", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/'});
+        expect(qh.getUrl()).to.equal(
+          'http://foobar.com/?query=%7B%22criteria%22%3A%5B%5D%7D&attributes=%5B%22UID%22%2C%22Title%22%2C%22Description%22%2C%22getURL%22%2C%22Type%22%5D');
+      });
+      it("getUrl correct and url query params already present", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/?foo=bar'});
+        expect(qh.getUrl()).to.equal(
+          'http://foobar.com/?foo=bar&query=%7B%22criteria%22%3A%5B%5D%7D&attributes=%5B%22UID%22%2C%22Title%22%2C%22Description%22%2C%22getURL%22%2C%22Type%22%5D');
+      });
+
+
+      it("browsing adds path criteria", function() {
+        var qh = new utils.QueryHelper({vocabularyUrl: 'http://foobar.com/?foo=bar'});
+        qh.pattern.browsing = true;
+        expect(qh.getQueryData('foobar').query).to.contain('plone.app.querystring.operation.string.path');
+      });
+
+
+    });
+
+
   });
 
 });




-------------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CHANGES.log
Type: application/octet-stream
Size: 39094 bytes
Desc: not available
URL: <http://lists.plone.org/pipermail/plone-testbot/attachments/20140130/f28edb21/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: build.log
Type: application/octet-stream
Size: 130621 bytes
Desc: not available
URL: <http://lists.plone.org/pipermail/plone-testbot/attachments/20140130/f28edb21/attachment-0003.obj>


More information about the Testbot mailing list