Github user benkeen commented on a diff in the pull request:

    https://github.com/apache/couchdb-fauxton/pull/78#discussion_r18415642
  
    --- Diff: app/addons/documents/views-queryoptions.js ---
    @@ -0,0 +1,522 @@
    +// Licensed under the Apache License, Version 2.0 (the "License"); you may 
not
    +// use this file except in compliance with the License. You may obtain a 
copy of
    +// the License at
    +//
    +//   http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT
    +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    +// License for the specific language governing permissions and limitations 
under
    +// the License.
    +
    +define([
    +  "app",
    +  "api",
    +
    +  // libs
    +  "addons/fauxton/resizeColumns"
    +],
    +
    +function(app, FauxtonAPI) {
    +
    +
    +  // our default settings for the Query Options tray
    +  var defaultOptions = {
    +    showStale: false,
    +    hasReduce: false,
    +
    +    // all the possible query search params. Ultimately these should 
probably be moved higher-up (route object?),
    +    // because they also apply to the actual search results. Seems better 
to place them there, then use them in
    +    // both places
    +    queryParams: {
    +      include_docs: "false",
    +      keys: "",
    +      limit: "",
    +      descending: "false",
    +      skip: "",
    +      update_seq: "false",
    +      startkey: "",
    +      endkey: "",
    +      inclusive_end: "true",
    +      reduce: "false",
    +      stale: "",
    +      group_level: "999" // TODO the doc says I can pass 
group_level=exact, but it doesn't work
    +    }
    +  };
    +
    +
    +  var Views = {};
    +
    +  // our main View. This is the only View exposed externally
    +  Views.QueryOptionsTray = FauxtonAPI.View.extend({
    +    template: "addons/documents/templates/query_options",
    +    className: "query-options",
    +
    +    initialize: function(options) {
    +
    +      // overlays whatever custom options were passed with defaultOptions 
above, so this.options
    +      // always contains all options. [This does a deep copy: we never 
overwrite defaultOptions!]
    +      this.options = $.extend(true, {}, defaultOptions, options);
    +
    +      // add any general events relating to the Query Options tray
    +      this.addEvents();
    +
    +      // add the sub-views
    +      this.mainFieldsView       = 
this.setView("#query-options-main-fields", new MainFieldsView(this.options));
    +      this.keySearchFieldsView  = 
this.setView("#query-options-key-search", new 
KeySearchFieldsView(this.options));
    +      this.additionalParamsView = 
this.setView("#query-options-additional-params", new 
AdditionalParamsView(this.options));
    +    },
    +
    +    addEvents: function() {
    +      var self = this;
    +
    +      FauxtonAPI.Events.on('QueryOptions:closeTray', self.closeTray);
    +
    +      // very bizarre. Need the anonymous function + "self" for this to 
work (?!)
    +      FauxtonAPI.Events.on('QueryOptions:openTray', function() { 
self.toggleQueryOptionsTray(); });
    +
    +      // if the user just clicked outside the tray, close it [TODO be nice 
to generalize for all trays]
    +      $("body").on("click.queryOptions", function(e) {
    +        if (!self.trayIsVisible()) { return; }
    +        if ($(e.target).closest("#query-options-tray").length === 0) {
    +          self.closeTray();
    +        }
    +      });
    +    },
    +
    +    cleanup: function() {
    +      FauxtonAPI.Events.unbind("QueryOptions:closeTray");
    +      FauxtonAPI.Events.unbind("QueryOptions:openTray");
    +    },
    +
    +    events: {
    +      "click #toggle-query": "toggleQueryOptionsTray", // hide/show the 
Query Options tray
    +      "submit form.js-view-query-update": "onSubmit",  // submits the form
    +      "click .btn-cancel": "onCancel"                  // closes the tray 
(doesn't reset the fields)
    +    },
    +
    +    toggleQueryOptionsTray: function() {
    +      if (!this.trayIsVisible()) {
    +        $("#query-options-tray").velocity("transition.slideDownIn", 250); 
// TODO constant
    +        FauxtonAPI.Events.trigger("APIbar:closeTray");
    +      }
    +    },
    +
    +    onCancel: function() {
    +      this.closeTray();
    +    },
    +
    +    // returns all applicable query parameters for the Query Options tray
    +    getQueryParams: function() {
    +      var mainFieldParams = this.mainFieldsView.getParams();
    +      var keySearchParams = this.keySearchFieldsView.getParams();
    +      var additionalParams = this.additionalParamsView.getParams();
    +
    +      // assumption: there aren't conflicting keys
    +      return $.extend({}, mainFieldParams, keySearchParams, 
additionalParams);
    +    },
    +
    +    onSubmit: function(e) {
    +      e.preventDefault();
    +
    +      // validate the user-inputted fields. If anything is invalid, the 
sub-view will display the appropriate
    +      // errors to the user
    +      if (!this.keySearchFieldsView.hasValidInputs() || 
!this.additionalParamsView.hasValidInputs()) {
    +        return;
    +      }
    +
    +      this.closeTray();
    +
    +      // this may be empty. That's ok! Perhaps the user just did a search 
with params, then removed them & wants the default
    +      var params = this.getQueryParams();
    +
    +      // all looks good! Close the tray and publish the message. This used 
to pass off the work to the parent View
    +      // (Views.RightAllDocsHeader) but it feels cleaner to do it right 
here
    +      var fragment = window.location.hash.replace(/\?.*$/, "");
    +      if (!_.isEmpty(params)) {
    +        fragment = fragment + "?" + $.param(params);
    +      }
    +      FauxtonAPI.navigate(fragment, { trigger: false });
    +      FauxtonAPI.triggerRouteEvent("updateAllDocs", { allDocs: true });
    +    },
    +
    +    /*
    +     * Updates specific query options, leaving any that have been set 
already intact.
    +     */
    +    updateQueryOptions: function(options) {
    +      this.options = $.extend(this.options, options);
    +      this.updateSubViews();
    +    },
    +
    +    /*
    +     * Reset the query options back to the defaults, then apply whatever 
new options are needed.
    +     */
    +    resetQueryOptions: function(options) {
    +      this.options = $.extend(true, {}, defaultOptions, options);
    +      this.updateSubViews();
    +    },
    +
    +    // helper
    +    updateSubViews: function() {
    +      this.mainFieldsView.update(this.options);
    +      this.keySearchFieldsView.update(this.options);
    +      this.additionalParamsView.update(this.options);
    +    },
    +
    +    trayIsVisible: function() {
    +      return $("#query-options-tray").is(":visible");
    +    },
    +
    +//    beforeRender: function () {
    --- End diff --
    
    haha agreed, will do :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to