All files / web/bundles/pimui/js/view base.ts

94.83% Statements 110/116
80.77% Branches 21/26
96.88% Functions 31/32
94.44% Lines 102/108

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 3241183x 1183x 1183x   1183x                   1183x 105774x     105774x 105774x   105774x                   105774x   105774x 105774x 105774x 105774x               101061x 101061x 7151x     101061x 95092x     101061x 101051x                       1183x 95355x   95355x 95355x 95355x   95355x       95355x                 1183x 6474x 46134x   46134x     6474x               1183x 95865x               1183x 725669x               1183x 253250x 253250x   253250x 464837x 464837x     253250x                   12572x 7238x 5470x     7238x   7238x 5470x                 1183x 125492x               1183x 212x           1183x 27831x   27831x           1183x 27831x 27831x 27831x 27831x   27831x           1183x 148335x 4220x     144115x               163177x   163177x       163177x   163177x 83201x     163177x               1183x 84892x   84892x           84892x   84892x           1183x 164870x     70589x     164870x                   1183x 87637x       87637x       87637x           1183x 306x   306x 95x 95x                   15850x 15850x 74798x                 1183x                     3886x 3886x 15844x   19514x 9623x       1183x   1183x  
import * as _ from 'underscore';
import * as JQuery from 'jquery';
import * as Backbone from 'backbone';
import View from 'pimui/js/view/base-interface';
const mediator = require('oro/mediator');
 
/**
 * View base class
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class BaseView extends Backbone.View<any> implements View {
  private parent: View | null = null;
  private extensions: {[code: string]: View};
 
  readonly preUpdateEventName: string = 'pim_enrich:form:entity:pre_update';
  readonly postUpdateEventName: string = 'pim_enrich:form:entity:post_update';
 
  public code: string = 'form';
  public configured: boolean;
  public zones: {[code: string]: any};
  public targetZone: string;
  public position: number;
 
  /**
   * {@inheritdoc}
   */
  constructor(config: any) {
    super(config);
 
    this.extensions = {};
    this.zones = {};
    this.targetZone = '';
    this.configured = false;
  }
 
  /**
   * Configure the extension and its child extensions
   *
   * @return {JQueryPromise<any>}
   */
  configure(): JQueryPromise<any> {
    if (null === this.parent) {
      this.model = new Backbone.Model();
    }
 
    const extensionPromises = Object.values(this.extensions).map((extension: View) => {
      return extension.configure();
    });
 
    return JQuery.when(...extensionPromises).then(() => {
      this.configured = true;
    });
  }
 
  /**
   * Add a child extension to this extension
   *
   * @param {string} code      Extension's code
   * @param {View}   extension Backbone module of the extension
   * @param {string} zone      Targeted zone
   * @param {number} position  The position of the extension
   */
  addExtension(code: string, extension: View, zone: string, position: number) {
    extension.setParent(this);
 
    extension.code = code;
    extension.targetZone = zone;
    extension.position = position;
 
    Iif (undefined === this.extensions || null === this.extensions) {
      throw 'this.extensions have to be defined. Please ensure you called parent initialize() method.';
    }
 
    this.extensions[code] = extension;
  }
 
  /**
   * Get a child extension (the first extension matching the given code or ends with the given code)
   *
   * @param  {string} code
   * @return {View}
   */
  getExtension(code: string): View {
    const extensionKey = _.findKey(this.extensions, (extension: View) => {
      const expectedPosition = extension.code.length - code.length;
 
      return expectedPosition >= 0 && expectedPosition === extension.code.indexOf(code, expectedPosition);
    });
 
    return this.extensions[extensionKey];
  }
 
  /**
   * Set the parent of this extension
   *
   * @param {View} parent
   */
  setParent(parent: View) {
    this.parent = parent;
  }
 
  /**
   * Get the parent of the extension
   *
   * @return {View | null}
   */
  getParent(): View | null {
    return this.parent;
  }
 
  /**
   * Get the root extension
   *
   * @return {View}
   */
  getRoot(): View {
    let rootView = <View>this;
    let parent = this.getParent();
 
    while (null !== parent) {
      rootView = parent;
      parent = parent.getParent();
    }
 
    return rootView;
  }
 
  /**
   * Set data in the root model
   *
   * @param {any}                data
   * @param {{silent?: boolean}} options If silent is set to true, don't fire events
   *                                       pim_enrich:form:entity:pre_update and pim_enrich:form:entity:post_update
   */
  setData(data: any, options: {silent?: boolean} = {}) {
    if (!options.silent) {
      this.getRoot().trigger(this.preUpdateEventName, data);
    }
 
    this.getRoot().model.set(data, options);
 
    if (!options.silent) {
      this.getRoot().trigger(this.postUpdateEventName, data);
    }
  }
 
  /**
   * Get the form raw data (vanilla javascript object)
   *
   * @return {any}
   */
  getFormData(): any {
    return this.getRoot().model.toJSON();
  }
 
  /**
   * Get the form data (backbone model)
   *
   * @return {Backbone.Model}
   */
  getFormModel(): Backbone.Model {
    return this.getRoot().model;
  }
 
  /**
   * Called before removing the form from the view
   */
  shutdown() {
    this.doShutdown();
 
    Object.values(this.extensions).forEach((extension: View) => extension.shutdown());
  }
 
  /**
   * The actual shutdown method called on all extensions
   */
  doShutdown() {
    this.stopListening();
    this.undelegateEvents();
    this.$el.removeData().off();
    this.remove();
 
    Backbone.View.prototype.remove.call(this);
  }
 
  /**
   * {@inheritdoc}
   */
  render(): View {
    if (!this.configured) {
      return this;
    }
 
    return this.renderExtensions();
  }
 
  /**
   * Render the child extensions
   *
   * @return {View}
   */
  renderExtensions(): View {
    // If the view is no longer attached to the DOM, don't render the extensions
    Iif (undefined === this.el) {
      return this;
    }
 
    this.initializeDropZones();
 
    Object.values(this.extensions).forEach((extension: View) => {
      this.renderExtension(extension);
    });
 
    return this;
  }
 
  /**
   * Render a single extension
   *
   * @param {View} extension
   */
  renderExtension(extension: View) {
    var zone = this.getZone(extension.targetZone);
 
    Iif (null === zone) {
      throw new Error(
        `Can not render extension "${extension.code}" in "${this.code}": zone "${extension.targetZone}" does not exist`
      );
    }
 
    zone.appendChild(extension.el);
 
    extension.render();
  }
 
  /**
   * Initialize dropzone cache
   */
  initializeDropZones() {
    this.zones = this.$('[data-drop-zone]')
      .toArray()
      .reduce((zones: {[code: string]: HTMLElement}, zone: HTMLElement) => {
        return {...zones, [<string>zone.dataset.dropZone]: zone};
      }, {});
 
    this.zones['self'] = this.el;
  }
 
  /**
   * Get the drop zone for the given code
   *
   * @param {string} code
   *
   * @return {HTMLElement | null}
   */
  getZone(code: string): HTMLElement | null {
    Iif (!(code in this.zones)) {
      this.zones[code] = this.el.querySelector(`[data-drop-zone="${code}"]`);
    }
 
    Iif (!this.zones[code]) {
      return null;
    }
 
    return this.zones[code];
  }
 
  /**
   * Trigger event on each child extensions and their childs
   */
  triggerExtensions() {
    const options = Object.values(arguments);
 
    Object.values(this.extensions).forEach(extension => {
      extension.trigger.apply(extension, options);
      extension.triggerExtensions.apply(extension, options);
    });
  }
 
  /**
   * Listen on child extensions and their childs events
   *
   * @param {string}   code
   * @param {Function} callback
   */
  onExtensions(code: string, callback: any) {
    Object.values(this.extensions).forEach((extension: View) => {
      this.listenTo(extension, code, callback);
    });
  }
 
  /**
   * Get the root form code
   *
   * @return {string}
   */
  getFormCode(): string {
    return this.getRoot().code;
  }
 
  /**
   * Listen to given mediator events to trigger them locally (in the local root).
   * This way, extensions attached to this form don't have to listen "globally" on the mediator.
   *
   * @param {{[mediatorEvent: string]: string}} mediator events to forward:
   *                {'mediator:event:name': 'this:event:name', ...}
   */
  forwardMediatorEvents(events: {[mediatorEvent: string]: string}) {
    Object.keys(events).forEach((localEvent: string) => {
      const mediatorEvent = events[localEvent];
 
      this.listenTo(mediator, mediatorEvent, (...args: any[]) => {
        this.trigger(localEvent, ...args);
      });
    });
  }
}
 
export = BaseView;