API Docs for: 1.0.0
Show:

File: Resources/public/js/extensions/ez-processable.js

  1. /*
  2. * Copyright (C) eZ Systems AS. All rights reserved.
  3. * For full copyright and license information view LICENSE file distributed with this source code.
  4. */
  5. YUI.add('ez-processable', function (Y) {
  6. "use strict";
  7. /**
  8. * The processable extension
  9. *
  10. * @module ez-processable
  11. */
  12. Y.namespace('eZ');
  13.  
  14. /**
  15. * View extension providing the concept of processor. The processors are
  16. * run after the event indicated by the `_processEvent` property.
  17. *
  18. * @namespace eZ
  19. * @class Processable
  20. * @extensionfor Y.View
  21. */
  22. Y.eZ.Processable = Y.Base.create('processableExtension', Y.View, [], {
  23. initializer: function () {
  24. /**
  25. * Holds the event(s) after which the processing should be triggered.
  26. *
  27. * @property _processEvent
  28. * @type {String|Array}
  29. * @required
  30. */
  31. this.after(this._processEvent, this._process);
  32. },
  33.  
  34. /**
  35. * Adds a processor to the list with the given priority. A processor is
  36. * an object that should have a `process` method. When run, the
  37. * processor will receive the view being processed.
  38. *
  39. * @method addProcessor
  40. * @param {Object} processor
  41. * @param {Number} priority
  42. */
  43. addProcessor: function (processor, priority) {
  44. var processors = this.get('processors');
  45.  
  46. if ( typeof processor.process !== 'function' ) {
  47. throw new Error("Processor must have a process method");
  48. }
  49. processors.push({
  50. processor: processor,
  51. priority: priority,
  52. });
  53. processors.sort(function (a, b) {
  54. return (b.priority - a.priority);
  55. });
  56. },
  57.  
  58. /**
  59. * Removes one or several processors matching the given `matchingFn`
  60. * function.
  61. *
  62. * @method removeProcessor
  63. * @param {Function} matchingFn it receives the processor and if it
  64. * returns a truthy value, the corresponding processor is excluded from
  65. * the list.
  66. */
  67. removeProcessor: function (matchingFn) {
  68. var processors = this.get('processors');
  69.  
  70. this._set('processors', processors.filter(function () {
  71. return !matchingFn.apply(this, arguments);
  72. }, this));
  73. },
  74.  
  75. /**
  76. * Loops through the processors to run them on the view.
  77. *
  78. * @method _process
  79. * @protected
  80. * @param {EventFacade} [event] the event parameters of the event
  81. * triggering the process (if any)
  82. */
  83. _process: function (event) {
  84. this.get('processors').forEach(function (info) {
  85. info.processor.process(this, event);
  86. }, this);
  87. },
  88. }, {
  89. ATTRS: {
  90. /**
  91. * Holds the processors. Each entry is an object containing 2
  92. * properties:
  93. * - `processor` the processor
  94. * - `priority` its priority
  95. *
  96. * @attribute priority
  97. * @type {Array}
  98. * @readOnly
  99. */
  100. processors: {
  101. writeOnce: 'initOnly',
  102. value: [],
  103. },
  104. },
  105. });
  106. });
  107.