Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / image_widget_crop / js / iwc.behaviors.js
1 /**
2  * @file
3  * Defines the Drupal behaviors needed for the Image Widget Crop module.
4  */
5
6 (function ($, Drupal) {
7   'use strict';
8
9   /**
10    * Drupal behavior for the Image Widget Crop module.
11    *
12    * @type {Drupal~behavior}
13    *
14    * @prop {Drupal~behaviorAttach} attach
15    *   Attaches the behavior and creates Cropper instances.
16    * @prop {Drupal~behaviorAttach} detach
17    *   Detaches the behavior and destroys Cropper instances.
18    */
19   Drupal.behaviors.imageWidgetCrop = {
20     attach: function (context) {
21       this.createInstances(context);
22     },
23     detach: function (context) {
24       this.destroyInstances(context);
25     },
26
27     /**
28      * Creates necessary instances of Drupal.ImageWidgetCrop.
29      *
30      * @param {HTMLElement|jQuery} [context=document]
31      *   The context which to find elements in.
32      */
33     createInstances: function (context) {
34       var $context = $(context || document);
35       $context.find(Drupal.ImageWidgetCrop.prototype.selectors.wrapper).each(function () {
36         var $element = $(this);
37         if (!$element.data('ImageWidgetCrop')) {
38           $element.data('ImageWidgetCrop', new Drupal.ImageWidgetCrop($element));
39         }
40       });
41     },
42
43     /**
44      * Destroys any instances of Drupal.ImageWidgetCrop.
45      *
46      * @param {HTMLElement|jQuery} [context=document]
47      *   The context which to find elements in.
48      */
49     destroyInstances: function (context) {
50       var $context = $(context || document);
51       $context.find(Drupal.ImageWidgetCrop.prototype.selectors.wrapper).each(function () {
52         var $element = $(this);
53         var instance = $element.data('ImageWidgetCrop');
54         if (instance) {
55           instance.destroy();
56           $element.removeData('ImageWidgetCrop');
57         }
58       });
59     }
60   };
61
62 }(jQuery, Drupal));