Skip to content

Commit

Permalink
Use dataset rather than jquery data function
Browse files Browse the repository at this point in the history
Start decoupling from jquery
  • Loading branch information
jcoyne committed Oct 17, 2023
1 parent 6c98119 commit 4a8a4fd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
10 changes: 5 additions & 5 deletions app/assets/javascripts/modules/css_injection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Module injects css into head of embeded page

(function( global ) {
var Module = (function() {
const Module = (function() {

var linkHtml = '<link rel="stylesheet" href="{{stylesheetLink}}" type="text/css" />',
themeUrl = $("[data-sul-embed-theme]").data("sul-embed-theme"),
iconsUrl = $('[data-sul-icons]').data('sul-icons'),
pluginStylesheets = $('[data-plugin-styles]').data('plugin-styles') || '';
const linkHtml = '<link rel="stylesheet" href="{{stylesheetLink}}" type="text/css" />',
themeUrl = document.querySelector("[data-sul-embed-theme]").dataset.sulEmbedTheme,
iconsUrl = document.querySelector('[data-sul-icons]').dataset.sulIcons,
pluginStylesheets = document.querySelector('[data-plugin-styles]')?.dataset?.pluginStyles || '';

return {
appendToHead: function() {
Expand Down
11 changes: 6 additions & 5 deletions app/assets/javascripts/modules/embed_this.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
var _this = this,
textarea = $('textarea', formContainer);
$('input[type="checkbox"], input[type="text"]', formContainer).on('change', function(){
var checked = $(this).is(':checked'),
const checked = $(this).is(':checked'),
inputType = $(this).attr('type'),
src = textarea.text().match(/src="(\S+)"/)[1],
urlAttr = '&' + $(this).data('embed-attr') + '=true';
urlAttr = `&${this.dataset.embedAttr}=true`
if(inputType === 'checkbox'){
if(checked) {
textarea.text(textarea.text().replace(urlAttr, ''));
}else{
textarea.text(textarea.text().replace(src, src + urlAttr));
}
}else{
if(oldParam = textarea.text().match('&' + $(this).data('embed-attr') + "=\\w+")) {
textarea.text(textarea.text().replace(oldParam, '&' + $(this).data('embed-attr') + '=' + $(this).val()));
const value = $(this).val()
if(oldParam = textarea.text().match(`&${this.dataset.embedAttr}=\\w+`)) {
textarea.text(textarea.text().replace(oldParam, `&${this.dataset.embedAttr}=${value}`));
}else{
textarea.text(textarea.text().replace(src, src + '&' + $(this).data('embed-attr') + '=' + $(this).val()));
textarea.text(textarea.text().replace(src, `${src}&${this.dataset.embedAttr}=${value}`));
}
}
});
Expand Down
13 changes: 7 additions & 6 deletions app/assets/javascripts/modules/geo_viewer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
(function( global ) {
'use strict';
var Module = (function() {
var dataAttributes;
var map;
var $el;
const Module = (function() {
let dataAttributes;
let map;
let $el;

var isDefined = function(object) {
return typeof object !== 'undefined';
};

return {
init: function(options) {
$el = jQuery('#sul-embed-geo-map');
dataAttributes = $el.data();
const el = document.getElementById(sul-embed-geo-map)
dataAttributes = el.dataset;
$el = jQuery(el);

map = L.map('sul-embed-geo-map', options).fitBounds(dataAttributes.boundingBox);

Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/modules/m3_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import cdlAuthPlugin from '../plugins/cdlAuthPlugin';

export default {
init: function() {
var $el = jQuery('#sul-embed-m3');
var data = $el.data();
const el = document.getElementById('sul-embed-m3');
const data = el.dataset;

// Determine which panel should be open
var sideBarPanel = 'info';
let sideBarPanel = 'info';
if (data.search) {
sideBarPanel = 'search';
}
Expand Down
46 changes: 23 additions & 23 deletions app/assets/javascripts/modules/media_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,32 @@ export default (function() {
cssClass = 'sul-i-file-video-3';
}

var activeClass = '';
let activeClass = '';
if (index === 0) {
activeClass = 'active';
}

var thumbClass = 'sul-embed-slider-thumb sul-embed-media-slider-thumb ';
var labelClass = 'sul-embed-thumb-label';
var isStanfordRestricted = $(mediaDiv).data('stanford-only');
const thumbClass = 'sul-embed-slider-thumb sul-embed-media-slider-thumb ';
let labelClass = 'sul-embed-thumb-label';
const isStanfordRestricted = mediaDiv.dataset.stanfordOnly
if (isStanfordRestricted) {
labelClass += ' sul-embed-thumb-stanford-only';
}

var thumbnailIcon = '';
var thumbnailUrl = $(mediaDiv).data('thumbnail-url');
let thumbnailIcon = '';
const thumbnailUrl = mediaDiv.dataset.thumbnailUrl
if (thumbnailUrl !== '') {
thumbnailIcon = '<img class="sul-embed-media-square-icon" src="' + thumbnailUrl + '" />';
thumbnailIcon = `<img class="sul-embed-media-square-icon" src="${thumbnailUrl}" />`
} else {
thumbnailIcon = '<i class="' + cssClass + '"></i>';
thumbnailIcon = `<i class="${cssClass}"></i>`
}

var isLocationRestricted = $(mediaDiv).data('location-restricted');
var fileLabel = String($(mediaDiv).data('file-label') || '');
var duration = String($(mediaDiv).data('duration') || '');
const isLocationRestricted = mediaDiv.dataset.locationRestricted
var fileLabel = String(mediaDiv.dataset.fileLabel || '');
var duration = String(mediaDiv.dataset.duration || '');
thumbs.push(
'<li class="' + thumbClass + activeClass + '">' +
thumbnailIcon +
'<a class="' + labelClass + '" href="#">' +
`<li class="${thumbClass}${activeClass}">${thumbnailIcon}` +
`<a class="${labelClass}" href="#">` +
stanfordOnlyScreenreaderText(isStanfordRestricted) +
restrictedTextMarkup(isLocationRestricted) +
truncateWithEllipsis(fileLabel, maxFileLabelLength(isLocationRestricted)) +
Expand Down Expand Up @@ -146,32 +145,33 @@ export default (function() {
}

function authCheckForMediaObject(mediaObject, completeCallback) {
var authUrl = mediaObject.data('auth-url');
const authUrl = mediaObject.dataset.authUrl
const $mediaObject = jQuery(mediaObject)
jQuery.ajax({url: authUrl, dataType: 'jsonp'}).done(function(data) {
// present the auth link if it's stanford restricted and the user isn't logged in
if(jQuery.inArray('stanford_restricted', data.status) > -1) {
var wrapper = jQuery('<div data-auth-link="true" class="sul-embed-auth-link"></div>');
mediaObject.parents(sliderObjectSelector).append(
$mediaObject.parents(sliderObjectSelector).append(
wrapper.append(authLink(data.service, mediaObject))
);
}

// if the user authed successfully for the file, hide the restriction overlays
var sliderSelector = '.sul-embed-media ' + sliderObjectSelector;
var parentDiv = mediaObject.closest(sliderSelector);
var isRestricted = parentDiv.data('stanford-only') || parentDiv.data('location-restricted');
var isRestricted = parentDiv.dataset.stanfordOnly || parentDiv.dataset.locationRestricted
if(isRestricted && data.status === 'success') {
parentDiv.find(restrictedMessageSelector).hide();
parentDiv.find('[data-auth-link]').hide();
jQuery(parentDiv).find(restrictedMessageSelector).hide();
jQuery(parentDiv).find('[data-auth-link]').hide();
}

if(data.status === 'success') {
updateMediaSrcWithToken(mediaObject, data.token);
initializeVideoJSPlayer(mediaObject);
updateMediaSrcWithToken($mediaObject, data.token);
initializeVideoJSPlayer($mediaObject);
}

if(typeof(completeCallback) === 'function') {
completeCallback(mediaObject, data);
completeCallback($mediaObject, data);
}
});
}
Expand Down Expand Up @@ -201,7 +201,7 @@ export default (function() {

function authCheck() {
jQuery('.sul-embed-media [data-auth-url]').each(function(){
authCheckForMediaObject(jQuery(this));
authCheckForMediaObject(this);
});
}

Expand Down

0 comments on commit 4a8a4fd

Please sign in to comment.