Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCC-98: Support relatime doc preview. #21

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
371 changes: 371 additions & 0 deletions config/optional/views.view.pantheon_cloud_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,371 @@
status: true
dependencies:
module:
- pcc_site
- pcx_connect
id: pantheon_cloud_api
label: 'Pantheon Cloud API'
module: views
description: ''
tag: ''
base_table: pcc_drupal
base_field: id
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: ''
fields:
id:
id: id
table: pcc_drupal
field: id
relationship: none
group_type: group
admin_label: ''
plugin_id: standard
label: ''
exclude: true
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
previewActiveUntil:
id: previewActiveUntil
table: pcc_drupal
field: previewActiveUntil
relationship: none
group_type: group
admin_label: ''
plugin_id: date
label: ''
exclude: true
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
date_format: custom
custom_date_format: 'Y-m-d H:i:s'
timezone: Asia/Kolkata
title:
id: title
table: pcc_drupal
field: title
relationship: none
group_type: group
admin_label: ''
plugin_id: standard
label: ''
exclude: false
alter:
alter_text: true
text: "<div class=\"livew-preview-bar\">\r\n<div class=\"item\"><a href=\"https://docs.google.com/document/d/{{ id }}/edit\" targte=\"_blank\">{{ title }} </a></div>\r\n<div class=\"item\">Page preview: <span class=\"preview-status\">Off</span></div>\r\n</div>"
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
content:
id: content
table: pcc_drupal
field: content
relationship: none
group_type: group
admin_label: ''
plugin_id: standard
label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
pager:
type: mini
options:
offset: 0
items_per_page: 10
total_pages: null
id: 0
tags:
next: ››
previous: ‹‹
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
access:
type: none
options: { }
cache:
type: none
options: { }
empty: { }
sorts: { }
arguments:
id:
id: id
table: pcc_drupal
field: id
relationship: none
group_type: group
admin_label: ''
plugin_id: string
default_action: 'not found'
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
publishingLevel:
id: publishingLevel
table: pcc_drupal
field: publishingLevel
relationship: none
group_type: group
admin_label: ''
plugin_id: string
default_action: default
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: query_parameter
default_argument_options:
query_param: publishingLevel
fallback: PRODUCTION
multiple: and
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
glossary: false
limit: 0
case: none
path_case: none
transform_dash: false
break_phrase: false
filters: { }
style:
type: default
row:
type: fields
query:
type: views_query
options: { }
relationships: { }
css_class: views-pcc-live-preview
use_ajax: true
header: { }
footer: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_interface'
- url
- url.query_args
tags: { }
realtime_preview:
id: realtime_preview
display_title: Page
display_plugin: page
position: 1
display_options:
display_extenders: { }
path: api/pantheoncloud/document/%
cache_metadata:
max-age: -1
contexts:
- 'languages:language_interface'
- url
- url.query_args
tags: { }
gauravgoyal marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions css/live-preview.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.views-pcc-live-preview {
.views-field-title {
box-shadow: 0px 0px 1px rgba(0, 0, 0, 0.28), 0px 2px 0px -8px rgba(0, 0, 0, 0.04);
padding: 20px;
}

.livew-preview-bar {
display: flex;
gap: 20px;

& .item {
flex: 1;
}
}
}
36 changes: 36 additions & 0 deletions js/preview-refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.preview_refresh = {
attach: (context, settings) => {
let interval = 2000;
const previwView = '.views-pcc-live-preview';
const livePreviewText = document.querySelector('.preview-status');
const livePreviewTimeRemaining = settings.realtime_preview.previewActiveUntil - Date.now();
// Live preview is active for 600000 mili-seconds.
// Auto refresh the views for 200000 mili-seconds.
if ($(previwView).length > 0 && livePreviewTimeRemaining > 400000) {
if (settings.realtime_preview.previewActiveUntil) {
livePreviewText.innerText = "On"
clearTimeout(settings.realtime_preview.previewActiveUntil);
}
settings.realtime_preview.previewActiveUntil = setTimeout(
function () {
Drupal.behaviors.preview_refresh.refresh(previwView)
}, interval
);
}

// Hides the throbber on refresh for views that use autorefresh.
for(let i in Drupal.views.instances) {
let view_set = Drupal.views.instances[i].settings;
if(view_set.view_name === "pantheon_cloud_api" && view_set.view_display_id === "realtime_preview") {
if(Drupal.views.instances[i].hasOwnProperty('refreshViewAjax')) {
Drupal.views.instances[i].refreshViewAjax.progress = {type: 'none'};
}
}
}
},
refresh: (previwView) => {
$(previwView).trigger('RefreshView');
}
}
})(jQuery, Drupal, drupalSettings);
10 changes: 10 additions & 0 deletions pcx_connect.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
preview-refresh:
css:
theme:
css/live-preview.css: {}
js:
js/preview-refresh.js: {}
dependencies:
- core/jquery
- core/drupalSettings
- core/drupal
16 changes: 16 additions & 0 deletions pcx_connect.module
Original file line number Diff line number Diff line change
@@ -68,3 +68,19 @@ function pcx_connect_preprocess_views_pcc_pager(&$variables) {
$variables['items']['next']['attributes'] = new Attribute();
}
}

/**
* Implements hook_preprocess_views_view().
*/
function pcx_connect_preprocess_views_view(&$variables) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
$variables['#cache']['contexts'][] = 'route';
if ($view->current_display == 'realtime_preview') {
foreach ($view->result as $row) {
$previewActiveUntil = $view->field['previewActiveUntil']->getValue($row);
$variables['#attached']['drupalSettings']['realtime_preview']['previewActiveUntil'] = $previewActiveUntil;
}
$variables['#attached']['library'][] = 'pcx_connect/preview-refresh';
}
}
9 changes: 9 additions & 0 deletions pcx_connect.views.inc
Original file line number Diff line number Diff line change
@@ -132,6 +132,15 @@ function pcx_connect_views_data() {
],
];

$table['previewActiveUntil'] = [
'title' => $translation->translate('Preview Active Until'),
'help' => $translation->translate('PCC Doc Preview Active Until'),
'field' => [
'id' => 'standard',
'click sortable' => TRUE,
],
];

$table['slug'] = [
'title' => $translation->translate('Slug'),
'help' => $translation->translate('PCC Doc Slug'),
2 changes: 1 addition & 1 deletion src/Plugin/views/field/PccMetadata.php
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
if (!$form_state->getValue(['options', 'pcc_metadata_fields'])) {
$form_state->setError($form['pcc_metadata'], $this->t('Metadata fields are not available for this site.'));
$form_state->setError($form['pcc_metadata'], $this->t('Please select a metadata field.'));
}
}