couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From benkeen <...@git.apache.org>
Subject [GitHub] couchdb-fauxton pull request: New Query Options tray added
Date Fri, 03 Oct 2014 19:31:01 GMT
Github user benkeen commented on a diff in the pull request:

    https://github.com/apache/couchdb-fauxton/pull/78#discussion_r18414930
  
    --- 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 () {
    +//      //if (this.viewName && this.ddocName) {
    +//      var buttonViews = FauxtonAPI.getExtensions('QueryOptions:ViewButton');
    +//        _.each(buttonViews, function (view) {
    +//          this.insertView('#button-options', view);
    +//          //view.update(this.database, this.ddocName, this.viewName);
    +//        }, this);
    +//      //}
    +//    },
    +
    +    closeTray: function() {
    +      $("#query-options-tray").velocity("reverse", 250, function() { // TODO constant
    +        $("#query-options-tray").hide();
    +      });
    +    }
    +  });
    +
    +
    +  // ------ "private" Views ------
    +
    +  var MainFieldsView = FauxtonAPI.View.extend({
    +    template: "addons/documents/templates/query_options_main_fields",
    +    events: {
    +      "change #qoReduce": "onToggleReduceCheckbox"
    +    },
    +
    +    initialize: function(options) {
    +      this.options = options;
    +    },
    +
    +    update: function(options) {
    +      this.options = options;
    +
    +      // if the View hasn't already rendered we can rely on afterRender() to pre-fill
the fields
    +      if (this.hasRendered) {
    +        this.render();
    +      }
    +    },
    +
    +    afterRender: function() {
    +      $("#qoIncludeDocs").prop("checked", this.options.queryParams.include_docs === "true");
    +      this.updateReduceSettings(this.options.queryParams.reduce === "true");
    +    },
    +
    +    /*
    +     * The "Reduce" option comes with baggage:
    +     *   - we can't include_docs for reduce = true
    +     *   - can't include group_level for reduce = false
    +     */
    +    onToggleReduceCheckbox: function(e) {
    +      e.preventDefault();
    +
    +      var isChecked = $(e.currentTarget).prop("checked");
    +
    +      // nah! This seems really overkill now it visually
    +//      if (isChecked && $("#qoIncludeDocs").is(":checked")) {
    +//        FauxtonAPI.addNotification({
    +//          msg: "include_docs has been disabled as you cannot include docs on a reduced
view",
    --- End diff --
    
    Originally it was helpful, I agree, but the new UI greys out the "Include as Docs" checkbox
label so it's visually very clear what's going on when you check the Reduce checkbox. Seemed
overkill to show the (rather intrusive) notification. I *think* I persuaded @seanbarclay that
this was okay. :) 


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message