import Core from 'spotlight/core'
import SpotlightNestable from 'spotlight/admin/spotlight_nestable'
Core.Block.Resources = (function(){
return Core.Block.extend({
type: "resources",
formable: true,
autocompleteable: true,
show_heading: true,
show_alt_text: true,
title: function() { return i18n.t("blocks:" + this.type + ":title"); },
description: function() { return i18n.t("blocks:" + this.type + ":description"); },
alt_text_guidelines: function() {
if (this.show_alt_text) {
return i18n.t("blocks:alt_text_guidelines:intro");
}
return "";
},
alt_text_guidelines_link: function() {
if (this.show_alt_text) {
var link_url = i18n.t("blocks:alt_text_guidelines:link_url");
var link_label = i18n.t("blocks:alt_text_guidelines:link_label");
return '' + link_label + '';
}
return "";
},
icon_name: "resources",
blockGroup: function() { return i18n.t("blocks:group:items") },
primary_field_key: "primary-caption-field",
show_primary_field_key: "show-primary-caption",
secondary_field_key: "secondary-caption-field",
show_secondary_field_key: "show-secondary-caption",
display_checkbox: "display-checkbox",
decorative_checkbox: "decorative-checkbox",
alt_text_textarea: "alt-text-textarea",
globalIndex: 0,
_itemPanelIiifFields: function(index, data) {
return [];
},
_altTextFieldsHTML: function(index, data) {
if (this.show_alt_text) {
return this.altTextHTML(index, data);
}
return "";
},
_itemPanel: function(data) {
var index = "item_" + this.globalIndex++;
var checked;
if (data.display == "true") {
checked = "checked='checked'"
} else {
checked = "";
}
var resource_id = data.slug || data.id;
var markup = `
${this._itemPanelIiifFields(index, data)}
${i18n.t("blocks:resources:panel:drag")}
`
const panel = $(markup);
var context = this;
$('.remove a', panel).on('click', function(e) {
e.preventDefault();
$(this).closest('.field').remove();
context.afterPanelDelete();
});
this.afterPanelRender(data, panel);
return panel;
},
afterPanelRender: function(data, panel) {
},
afterPanelDelete: function() {
},
createItemPanel: function(data) {
var panel = this._itemPanel(data);
this.attachAltTextHandlers(panel);
$(panel).appendTo($('.panels > ol', this.inner));
$('[data-behavior="nestable"]', this.inner).trigger('change');
},
item_options: function() { return ""; },
content: function() {
var templates = [this.items_selector()];
if (this.plustextable) {
templates.push(this.text_area());
}
return templates.join("
\n");
},
items_selector: function() { return [
'',
'
',
'
',
this.item_options(),
'
',
'
'].join("\n")
},
editorHTML: function() {
return `
${this.content()}
`
},
_altTextData: function(data) {
const isDecorative = data.decorative;
const altText = isDecorative ? '' : (data.alt_text || '');
const altTextBackup = data.alt_text_backup || '';
const placeholderAttr = isDecorative ? '' : `placeholder="${i18n.t("blocks:resources:alt_text:placeholder")}"`;
const disabledAttr = isDecorative ? 'disabled' : '';
return { isDecorative, altText, altTextBackup, placeholderAttr, disabledAttr };
},
altTextHTML: function(index, data) {
const { isDecorative, altText, altTextBackup, placeholderAttr, disabledAttr } = this._altTextData(data);
return ``
},
attachAltTextHandlers: function(panel) {
if (this.show_alt_text) {
const decorativeCheckbox = $('input[name$="[decorative]"]', panel);
const altTextInput = $('textarea[name$="[alt_text]"]', panel);
const altTextBackupInput = $('input[name$="[alt_text_backup]"]', panel);
decorativeCheckbox.on('change', function() {
const isDecorative = this.checked;
if (isDecorative) {
altTextBackupInput.val(altTextInput.val());
altTextInput.val('');
} else {
altTextInput.val(altTextBackupInput.val());
}
altTextInput
.prop('disabled', isDecorative)
.attr('placeholder', isDecorative ? '' : i18n.t("blocks:resources:alt_text:placeholder"));
});
altTextInput.on('input', function() {
$(this).data('lastValue', $(this).val());
});
}
},
onBlockRender: function() {
SpotlightNestable.init($('[data-behavior="nestable"]', this.inner));
$('[data-input-select-target]', this.inner).selectRelatedInput();
},
afterLoadData: function(data) {
var context = this;
$.each(Object.keys(data.item || {}).map(function(k) { return data.item[k]}).sort(function(a,b) { return a.weight - b.weight; }), function(index, item) {
context.createItemPanel(item);
});
},
});
})();