Commit dd955abf authored by ephoto_dam's avatar ephoto_dam

Ephoto Dam Field in progress

parent 75608d37
...@@ -5,9 +5,30 @@ ...@@ -5,9 +5,30 @@
* Module ephoto_dam_field. * Module ephoto_dam_field.
*/ */
use Drupal\Component\Serialization\Json;
use \Drupal\Core\Render\Markup;
/** /**
* Hook : implements hook_field_widget_form_alter(). * Hook : implements hook_field_widget_form_alter().
*/ */
function ephoto_dam_field_field_widget_form_alter(&$element, &$form_state, $context) { function ephoto_dam_field_field_widget_form_alter(&$element, &$form_state, $context) {
$element['#attached']['library'][] = 'ephoto_dam_field/edit'; $field_definition = $context['items']->getFieldDefinition();
if ($field_definition->getType() === 'ephoto_dam_field') {
$settings = $field_definition->getSettings();
$settings_encoded = str_replace('"', "'", Json::encode($settings));
$element['#attached']['library'][] = 'ephoto_dam_field/edit';
// I do not like it
// It would be nice to put the settings AFTER the ".libraries", which would make it possible to call directly EphotoDamField.load($settings_encoded)
$element['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => Markup::create('EphotoDamFieldSettings=' . $settings_encoded . ';'),
'#weight' => 200
],
'ephoto_dam_field_js_settings'
];
}
} }
...@@ -7,26 +7,29 @@ ...@@ -7,26 +7,29 @@
(function ($) { (function ($) {
EphotoDamField = { EphotoDamField = {
/** /**
* EPHOTO ID of Drupal Addon * EPHOTO ID of Drupal Addon
*/ */
CLIENT_ID : 'CaCvqS5H', CLIENT_ID : 'CaCvqS5H',
/** /**
* Settings * Settings
*/ */
settings: null, settings: null,
/** /**
* Ephoto Dam instance * Ephoto Dam instance
*/ */
ephoto: null, ephoto: null,
/** /**
* Ephoto Dam Server connected * Ephoto Dam Server connected
*/ */
connected: false, connected: false,
/**
* Enable version support
*/
enableVersionSupport: function(el) { enableVersionSupport: function(el) {
if(el.checked) { if(el.checked) {
$('.ephoto-dam-field-version-field').removeClass('ephoto-dam-field-hidden'); $('.ephoto-dam-field-version-field').removeClass('ephoto-dam-field-hidden');
...@@ -43,49 +46,76 @@ ...@@ -43,49 +46,76 @@
} }
}, },
select: function(settings) { /**
this.settings = settings; * Load Ephoto Dam Api
*/
load: function() {
this.settings = EphotoDamFieldSettings;
$.getScript(settings.url + 'api/apiJS.js', this.initApi.bind(this)); $.getScript(settings.url + 'api/apiJS.js', this.initApi.bind(this));
}, },
/**
* Init Ephoto Dam Api
*/
initApi: function() { initApi: function() {
var self = this; var self = this;
if(!ePhoto) { if (!ePhoto) {
console.log(Drupal.t('Error loading the Ephoto Dam API')); console.log(Drupal.t('Error loading the Ephoto Dam API'));
return; return;
} }
console.log('loaded!'); console.log('loaded!');
console.log(this.settings.url); console.log(this.settings.url);
this.ephoto = new ePhoto({ this.ephoto = new ePhoto({
'server': this.settings.url, 'server': this.settings.url,
//'authID': CKEDITOR.tools.getCookie('drupal_ephoto_dam_authid'), //'authID': CKEDITOR.tools.getCookie('drupal_ephoto_dam_authid'),
'client': this.CLIENT_ID 'client': this.CLIENT_ID
}); });
this.ephoto.connect(); this.ephoto.connect();
this.ephoto.callOnConnect(function() { //this.ephoto.File.setMode('embed');
//var today = expires = new Date();
//expires.setDate(today.getDate() + 2);
//CKEDITOR.tools.setCookie( 'drupal_ephoto_dam_authid', self.ephoto.getAuthID()); //this.ephoto.File.enableDCore();
self.connected = true; this.ephoto.callOnConnect(function() {
}); //var today = expires = new Date();
//expires.setDate(today.getDate() + 2);
this.ephoto.File.callOnFileReceived(this.insert.bind(this)); //CKEDITOR.tools.setCookie( 'drupal_ephoto_dam_authid', self.ephoto.getAuthID());
this.ephoto.File.get(); self.connected = true;
});
this.ephoto.File.callOnFileReceived(this.insert.bind(this));
}, },
/**
*
*/
selectVersionUrls: function() {
this.ephoto.File.get();
},
/**
*
*/
selectUrl: function(delta) {
this.ephoto.File.get();
},
/**
*
*/
insert: function() { insert: function() {
} }
} }
EphotoDamField.load();
})(jQuery); })(jQuery);
...@@ -26,13 +26,8 @@ class EphotoDamFieldWidget extends WidgetBase { ...@@ -26,13 +26,8 @@ class EphotoDamFieldWidget extends WidgetBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$settings = $this->getFieldSettings();
$settings['delta'] = $delta;
$settings_encoded = str_replace('"', "'", Json::encode($settings));
$is_default_values_form = $this->isDefaultValueWidget($form_state); $is_default_values_form = $this->isDefaultValueWidget($form_state);
$version_support = $settings['version_support']; $version_support = $this->getFieldSetting('version_support');
if (self::$first) { if (self::$first) {
$classes = ['ephoto-dam-field-button', 'ephoto-dam-field-version-button']; $classes = ['ephoto-dam-field-button', 'ephoto-dam-field-version-button'];
...@@ -44,9 +39,9 @@ class EphotoDamFieldWidget extends WidgetBase { ...@@ -44,9 +39,9 @@ class EphotoDamFieldWidget extends WidgetBase {
'#disabled' => $is_default_values_form, '#disabled' => $is_default_values_form,
'#attributes' => [ '#attributes' => [
'class' => $classes, 'class' => $classes,
'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false', 'onclick' => 'EphotoDamField.selectVersionUrls();return false',
'class' => $classes, 'class' => $classes,
] ],
]; ];
self::$first = false; self::$first = false;
...@@ -89,7 +84,7 @@ class EphotoDamFieldWidget extends WidgetBase { ...@@ -89,7 +84,7 @@ class EphotoDamFieldWidget extends WidgetBase {
'#disabled' => $is_default_values_form, '#disabled' => $is_default_values_form,
'#attributes' => [ '#attributes' => [
'class' => $classes, 'class' => $classes,
'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false', 'onclick' => 'EphotoDamField.selectUrl(' . $delta . ');return false',
'class' => $classes 'class' => $classes
] ]
]; ];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment