Source: base/plugin/Sorter.js

  1. // Copyright 2011 Tart. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview tart.base.plugin.Sorter model sorter plugin.
  16. */
  17. goog.provide('tart.base.plugin.Sorter');
  18. goog.require('tart.base.plugin.BasePlugin');
  19. /**
  20. * @param {tart.base.Model} model
  21. *
  22. * @extends {tart.base.plugin.BasePlugin}
  23. * @constructor
  24. */
  25. tart.base.plugin.Sorter = function (model) {
  26. goog.events.EventTarget.call(this);
  27. /** @protected */
  28. this.model = model;
  29. /** @protected **/
  30. this.sorts = [];
  31. this.model.params.set(this.key, this.sorts);
  32. };
  33. goog.inherits(tart.base.plugin.Sorter, tart.base.plugin.BasePlugin);
  34. /**
  35. * Set plugin's param
  36. */
  37. tart.base.plugin.Sorter.prototype.key = "sortParams";
  38. /**
  39. * @param {string} field field to be sorted.
  40. * @param {string} order order by directive, which is asc or desc.
  41. */
  42. tart.base.plugin.Sorter.prototype.addSort = function (field, order) {
  43. /**
  44. * There can be multiple condition-value pair for a field
  45. */
  46. var fieldSorter = goog.array.find(this.sorts, function(item){
  47. return goog.object.getAnyKey(item) == field;
  48. });
  49. //and if this field did not set before create a new object
  50. if (!fieldSorter) {
  51. fieldSorter = {};
  52. }
  53. fieldSorter[field] = order;
  54. this.model.params.get(this.key).push(fieldSorter);
  55. };
  56. /**
  57. * clear map for plugin
  58. */
  59. tart.base.plugin.Sorter.prototype.clear = function () {
  60. this.sorts = [];
  61. this.model.params.set(this.key, this.sorts);
  62. };