3 * Defines the behaviors needed for cropper integration.
6 (function ($, Drupal, debounce) {
9 var $window = $(window);
12 * @class Drupal.ImageWidgetCrop
14 * @param {HTMLElement|jQuery} element
15 * The wrapper element.
17 Drupal.ImageWidgetCrop = function (element) {
20 * The wrapper element.
24 this.$wrapper = $(element);
27 * The summary element.
31 this.$summary = this.$wrapper.find(this.selectors.summary).first();
34 * Flag indicating whether the instance has been initialized.
38 this.initialized = false;
41 * The current summary text.
45 this.summary = Drupal.t('Crop image');
48 * The individual ImageWidgetCropType instances.
50 * @type {Array.<Drupal.ImageWidgetCropType>}
54 // Initialize the instance.
59 * The selectors used to identify elements for this module.
61 * @type {Object.<String, String>}
63 Drupal.ImageWidgetCrop.prototype.selectors = {
64 // Unfortunately, core does not provide a way to inject attributes into the
65 // wrapper element's "summary" in a stable way. So, we can only target
66 // the immediate children known to be the likely elements. Contrib can
67 // extend this selector if needed.
68 summary: '> summary, > legend',
69 types: '[data-drupal-iwc=type]',
70 wrapper: '[data-drupal-iwc=wrapper]'
74 * Destroys this instance.
76 Drupal.ImageWidgetCrop.prototype.destroy = function () {
77 this.types.forEach(function (type) {
83 * Initializes the instance.
85 Drupal.ImageWidgetCrop.prototype.init = function () {
86 if (this.initialized) {
90 // Find all the types.
92 this.$wrapper.find(this.selectors.types).each(function () {
93 _this.types.push(new Drupal.ImageWidgetCropType(_this, this));
96 // Debounce resize event to prevent any issues.
97 $window.on('resize.iwc', debounce(this.resize.bind(this), 250));
99 // Update the summary when triggered from vertical tabs underneath it.
100 this.$wrapper.on('summaryUpdated', this.updateSummary.bind(this));
102 // Trigger the initial summaryUpdate event.
103 this.$wrapper.trigger('summaryUpdated');
107 * The "resize" event callback.
109 * @see Drupal.ImageWidgetCropType.prototype.resize
111 Drupal.ImageWidgetCrop.prototype.resize = function () {
112 var args = arguments;
114 // Proxy the resize event to each ImageWidgetCropType instance.
115 this.types.forEach(function (type) {
116 type.resize.apply(type, args);
121 * Updates the summary of the wrapper.
123 Drupal.ImageWidgetCrop.prototype.updateSummary = function () {
124 var text = Drupal.t('Crop image');
126 // Determine if any ImageWidgetCropType has been applied.
127 for (var i = 0, l = this.types.length; i < l; i++) {
128 var type = this.types[i];
129 if (type.getValue('applied')) {
130 text = Drupal.t('Crop image (cropping applied)');
135 if (this.summary !== text) {
136 this.$summary.text(this.summary = text);
140 }(jQuery, Drupal, Drupal.debounce));