Skip to content

Commit

Permalink
Detangle object relationships, formalize path api
Browse files Browse the repository at this point in the history
- This was done to clarify the scope and hierarchy of the affected
  objects.

- The AppView instance is where the path setting should be done, even
  though it ultimately pokes into the methods by the same name within
  ResultCollection.

  - Code that previously used queryHelper.currentPath now delegate this
    through the methods provided by AppView.

- Formalizing what needs to be implemented for custom ResultCollection
  models - it needs to provide getCurrentPath/setCurrentPath along with
  the relevant Backbone.Paginator.requestPager methods.  This should be
  documented.

  - The instance of AppView.collection now keeps track of the path that
    the AppView should be on - no longer requiring fetching this value
    from queryHelper

- A completely custom ResultCollection can be now provided.

- Default ResultCollection implementation now constructs queryParser and
  queryHelper inside itself to keep as much of the Plone catalog and
  content type specific information out of the pattern itself to aid in
  allowing reuse without the unwanted Plone bits (specifically with the
  queryHelper stuff, it really had no place on the top level option
  object).

- Default implementation of the queryParser and construction of
  queryHelper should remain unchanged except for its references to
  certain attributes that exist on the parent app objects (such as the
  AppView.toolbar).
  • Loading branch information
metatoaster committed Jan 26, 2016
1 parent ff74dab commit d35479f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 43 deletions.
46 changes: 42 additions & 4 deletions mockup/patterns/structure/js/collections/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,58 @@ define([
'underscore',
'backbone',
'mockup-patterns-structure-url/js/models/result',
'mockup-utils',
'backbone.paginator'
], function(_, Backbone, Result) {
], function(_, Backbone, Result, Utils) {
'use strict';

var ResultCollection = Backbone.Paginator.requestPager.extend({
model: Result,
queryHelper: null, // need to set
initialize: function(models, options) {
this.options = options;
this.view = options.view;
this.url = options.url;
this.queryParser = options.queryParser;
this.queryHelper = options.queryHelper;

this.queryHelper = Utils.QueryHelper(
$.extend(true, {}, this.view.options, {
attributes: this.view.options.attributes_}));

this.queryParser = function(options) {
var self = this;
if(options === undefined){
options = {};
}
var term = null;
if (self.view.toolbar) {
term = self.view.toolbar.get('filter').term;
}
var sortOn = self.view.sort_on; // jshint ignore:line
var sortOrder = self.view.sort_order; // jshint ignore:line
if (!sortOn) {
sortOn = 'getObjPositionInParent';
}
return JSON.stringify({
criteria: self.queryHelper.getCriterias(term, $.extend({}, options, {
additionalCriterias: self.view.additionalCriterias
})),
sort_on: sortOn,
sort_order: sortOrder
});
}

// check and see if a hash is provided for initial path
if (window.location.hash.substring(0, 2) === '#/') {
this.queryHelper.currentPath = window.location.hash.substring(1);
}

Backbone.Paginator.requestPager.prototype.initialize.apply(this, [models, options]);
},
getCurrentPath: function() {
return this.queryHelper.getCurrentPath();
},
setCurrentPath: function(path) {
this.queryHelper.currentPath = path;
},
pager: function() {
this.trigger('pager');
Backbone.Paginator.requestPager.prototype.pager.apply(this, []);
Expand Down
2 changes: 1 addition & 1 deletion mockup/patterns/structure/js/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ define([
e.preventDefault();
// handler for folder, go down path and show in contents window.
var self = this;
self.app.queryHelper.currentPath = self.model.attributes.path;
self.app.setCurrentPath(self.model.attributes.path);
// also switch to fix page in batch
self.app.collection.goTo(self.app.collection.information.firstPage);
},
Expand Down
43 changes: 16 additions & 27 deletions mockup/patterns/structure/js/views/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define([
TableView, SelectionWellView,
GenericPopover, RearrangeView, SelectionButtonView,
PagingView, ColumnsView, TextFilterView, UploadView,
ResultCollection, SelectedCollection, utils, _t, logger) {
_ResultCollection, SelectedCollection, utils, _t, logger) {
'use strict';

var log = logger.getLogger('pat-structure');
Expand Down Expand Up @@ -65,34 +65,17 @@ define([
}
});

var ResultCollection = require(options.collectionConstructor);

self.collection = new ResultCollection([], {
// Due to default implementation need to poke at things in here,
// view is passed.
view: self,
url: self.options.collectionUrl,
queryParser: function(options) {
if(options === undefined){
options = {};
}
var term = null;
if (self.toolbar) {
term = self.toolbar.get('filter').term;
}
var sortOn = self['sort_on']; // jshint ignore:line
if (!sortOn) {
sortOn = 'getObjPositionInParent';
}
return JSON.stringify({
criteria: self.queryHelper.getCriterias(term, $.extend({}, options, {
additionalCriterias: self.additionalCriterias
})),
sort_on: sortOn,
sort_order: self['sort_order'] // jshint ignore:line
});
},
queryHelper: self.options.queryHelper
});

self.setAllCookieSettings();

self.queryHelper = self.options.queryHelper;
self.selectedCollection = new SelectedCollection();
self.tableView = new TableView({app: self});

Expand Down Expand Up @@ -145,7 +128,7 @@ define([
/* maintain history here */
if(self.options.urlStructure && window.history && window.history.pushState){
if (!self.doNotPushState){
var path = self.queryHelper.getCurrentPath();
var path = self.getCurrentPath();
if(path === '/'){
path = '';
}
Expand Down Expand Up @@ -178,7 +161,7 @@ define([
if(!path){
path = '/';
}
self.queryHelper.currentPath = path;
self.collection.setCurrentPath(path);
$('body').trigger('structure-url-changed', path);
// since this next call causes state to be pushed...
self.doNotPushState = true;
Expand Down Expand Up @@ -232,8 +215,14 @@ define([
});
return uids;
},
getCurrentPath: function() {
return this.collection.getCurrentPath();
},
setCurrentPath: function(path) {
this.collection.setCurrentPath(path);
},
getAjaxUrl: function(url) {
return url.replace('{path}', this.options.queryHelper.getCurrentPath());
return url.replace('{path}', this.getCurrentPath());
},
buttonClickEvent: function(button) {
var self = this;
Expand Down Expand Up @@ -264,7 +253,7 @@ define([
}
data._authenticator = utils.getAuthenticator();
if (data.folder === undefined) {
data.folder = self.options.queryHelper.getCurrentPath();
data.folder = self.getCurrentPath();
}

var url = self.getAjaxUrl(button.url);
Expand Down
4 changes: 2 additions & 2 deletions mockup/patterns/structure/js/views/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ define([
self.$el.html(self.template({
_t: _t,
pathParts: _.filter(
self.app.queryHelper.getCurrentPath().split('/').slice(1),
self.app.getCurrentPath().split('/').slice(1),
function(val) {
return val.length > 0;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ define([
}
});
path += $el.attr('data-path');
this.app.queryHelper.currentPath = path;
this.app.setCurrentPath(path);
this.collection.pager();
},
selectFolder: function(e) {
Expand Down
4 changes: 2 additions & 2 deletions mockup/patterns/structure/js/views/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ define([
options.relatedItems = {
vocabularyUrl: self.app.options.vocabularyUrl
};
options.currentPath = self.app.options.queryHelper.getCurrentPath();
options.currentPath = self.app.getCurrentPath();
self.upload = new Upload(self.$('.uploadify-me').addClass('pat-upload'), options);
return this;
},
Expand All @@ -46,7 +46,7 @@ define([
if (!this.opened) {
return;
}
var currentPath = self.app.queryHelper.getCurrentPath();
var currentPath = self.app.getCurrentPath();
var relatedItems = self.upload.relatedItems;
if (self.currentPathData && relatedItems && currentPath !== self.upload.currentPath){
if (currentPath === '/'){
Expand Down
14 changes: 7 additions & 7 deletions mockup/patterns/structure/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ define([
'mockup-patterns-structure-url/js/navigation', 'openClicked']
},

collectionConstructor:
'mockup-patterns-structure-url/js/collections/result',

momentFormat: 'relative',
rearrange: {
properties: {
Expand Down Expand Up @@ -179,14 +182,11 @@ define([

self.browsing = true; // so all queries will be correct with QueryHelper
self.options.collectionUrl = self.options.vocabularyUrl;
self.options.queryHelper = new utils.QueryHelper(
$.extend(true, {}, self.options, {pattern: self}));
self.options.pattern = self;

// check and see if a hash is provided for initial path
if(window.location.hash.substring(0, 2) === '#/'){
self.options.queryHelper.currentPath = window.location.hash.substring(1);
}
delete self.options.attributes; // not compatible with backbone
// the ``attributes`` options key is not compatible with backbone
self.options.attributes_ = self.options.attributes;
delete self.options.attributes;

self.view = new AppView(self.options);
self.$el.append(self.view.render().$el);
Expand Down
2 changes: 2 additions & 0 deletions mockup/tests/pattern-structure-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ define([
'availableColumns': [],
'indexOptionsUrl': '',
'setDefaultPageUrl': '',
'collectionConstructor':
'mockup-patterns-structure-url/js/collections/result',
});
this.app.render();
});
Expand Down

0 comments on commit d35479f

Please sign in to comment.