Skip to content

adding sri modal to codeorigin #391

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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 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
44 changes: 44 additions & 0 deletions themes/codeorigin.jquery.com/page.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
<?php

// attach custom header and footer tags
function header_append() {
echo '
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" integrity="sha384-mf72GmkUsTKkmt05bABaLGVDDWha9SeDJTZ0tTQv4t1Tgz3CRQX+PENxJR8nQ7Tk" crossorigin="anonymous">
<link rel="stylesheet" href="' . get_template_directory_uri() . '/css/sri-modal.css" />
';
}

function footer_append() {
echo '
<script src="https://code.jquery.com/jquery-2.2.0.min.js" integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk" crossorigin="anonymous"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery is already loaded.

<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" integrity="sha384-YWP9O4NjmcGo4oEJFXvvYSEzuHIvey+LbXkBNJ1Kd0yfugEZN9NCQNpRYBVC1RvA" crossorigin="anonymous"></script>
<script src="' . get_template_directory_uri() . '/js/clipboard.1.5.5.min.js"></script>
<script src="' . get_template_directory_uri() . '/js/sri-modal.js"></script>
';
}

?>

<?php get_header(); ?>

<?php the_post(); ?>

<div id="sri-modal-template" title="Code Integration">
<div class="sri-modal-link">
<code>&lt;script
&nbsp;&nbsp;<span>src="{{link}}"</span>
&nbsp;&nbsp;<span>integrity="{{hash}}"</span>
&nbsp;&nbsp;<span>crossorigin="anonymous"&gt;&lt;/script&gt;</span>
</code>

<button class="sri-modal-copy-btn"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This button should start with a title and have the tooltip initialized so that the feature is discoverable.

data-clipboard-text='&lt;script src="{{link}}" integrity="{{hash}}" crossorigin="anonymous"&gt;&lt;/script&gt;'>
<i class="sri-modal-icon-copy icon-copy"></i>
</button>
</div>

<div class="sri-modal-info">
The <code>integrity</code> and <code>crossorigin</code> attributes are used for
<a href="https://www.w3.org/TR/SRI/" target="_blank">Subresource Integrity (SRI) checking</a>.
This allows browsers to ensure that resources hosted on third-party servers have
not been tampered with. Use of SRI is recommended as a best-practice, whenever
libraries are loaded from a third-party source. Read more at <a href="https://www.srihash.org/" target="_blank">srihash.org</a>
</div>
</div>

<div class="content-full twelve columns">
<div id="content">
<?php if ( !count( get_post_meta( $post->ID, "hide_title" ) ) ) : ?>
Expand Down
88 changes: 88 additions & 0 deletions themes/jquery/css/sri-modal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Added for SRI Modal
*/
.sri-modal {
padding: 0;
border: none;
background: #fff;
border-radius: 0;
}

.sri-modal .ui-dialog-titlebar {
padding: 0;
border: none;
background: #fff;
color: #000;
cursor: auto;
}

.sri-modal .ui-dialog-title {
padding-top: 20px;
text-align: center;
font-family: "klavika-web", "Helvetica Neue", Helvetica, Arial, Geneva, sans-serif;
font-weight: 400;
width: 100%;
}

.sri-modal .ui-dialog-titlebar-close {
background: none;
border: none;
right: 28px;
}

.sri-modal .ui-icon-closethick {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAclBMVEUAAADMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMxg1ZvWAAAAJXRSTlMAAQIDBAUGCQsNDhQWGSg4g4uUoKOoq62vsLW8wMrO2tzk5ujxm/5xbwAAAGxJREFUGBmNwVcWgkAQRcFHg4FgzjomZu7+t+hpRv6p0iS7byPJTs+5BluIjewML5O7A3F1AdJMrgpkqVVWBVxqNVriNhrZDRdrZXYl62sNjkBaB6Cv5PaQOpUB3iZXHFInqXx8FvozDQpN8gNoXAtiMhPWmQAAAABJRU5ErkJggg==) 0 0;
}

.sri-modal .ui-widget-content {
padding: 30px 0 0 0;
}

.sri-modal .ui-widget-content a {
color: #b24926;
}

.sri-modal-link {
width: 810px;
word-wrap: break-word;
word-break: break-all;
display: inline-block;
background-color: #f8fbfb;
padding: 0 10px;
position: relative;
}

.sri-modal-link code {
width: 765px;
display: inline-block;
white-space: pre-line;
background-color: transparent;
padding: 0;
font-size: 13px;
line-height: 16px;
}

.sri-modal-copy-btn {
background-color: #1c6db3;
border: none;
font-size: 1rem;
width: 35px;
height: 35px;
cursor: pointer;
position: absolute;
top: 5px;
}

.sri-modal-copy-btn > i {
color: #fff;
}

.sri-modal-info {
padding: 15px;
font: 15px/22px "Helvetica Neue", HelveticaNeue, Helvetica, Arial, sans-serif;
}

#sri-modal-template {
display: none;
}

7 changes: 6 additions & 1 deletion themes/jquery/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
</div>
</footer>

<?php wp_footer(); ?>
<?php
wp_footer();

if ( function_exists( 'footer_append' ) )
footer_append();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving myself a note to come back to this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure about this approach, so I'd be interested in your feedback and possible alternative. It does allow other themes to follow suit, but feels a little hacky at the same time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what problem you were having enqueueing these files, but replacing the function definitions in page.php with the following code works just fine for me:

wp_enqueue_style('jquery-ui', 'https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css');
wp_enqueue_style('sri-modal', get_template_directory_uri() . '/css/sri-modal.css');
wp_enqueue_script('jquery-ui', 'https://code.jquery.com/ui/1.11.4/jquery-ui.min.js');
wp_enqueue_script('clipboard', get_template_directory_uri() . '/js/clipboard.1.5.5.min.js');
wp_enqueue_script('sri-modal', get_template_directory_uri() . '/js/sri-modal.js');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, yes that works. Apologies, I must have had a type or something in my tests.

?>

</body>
</html>
3 changes: 3 additions & 0 deletions themes/jquery/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
wp_enqueue_script( 'comment-reply' );

wp_head();

if ( function_exists( 'header_append' ) )
header_append();
?>

</head>
Expand Down
7 changes: 7 additions & 0 deletions themes/jquery/js/clipboard.1.5.5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions themes/jquery/js/sri-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(function() {
// Store modal templates
var modalTemplate = $( "#sri-modal-template" )[ 0 ].outerHTML,
clipboard;

// Show modal on click
$( "body" ).on( "click", ".open-sri-modal", function( event ) {
if ( event.ctrlKey || event.metaKey ) {
return;
}

$( replace( modalTemplate, {
link: document.location.origin + $( this ).attr( "href" ),
hash: $( this ).attr( "data-hash" )
} ) ).removeAttr( "id" ).appendTo( "body" ).dialog( {
modal: true,
resizable: false,
width: 830,
dialogClass: "sri-modal",
draggable: false,
close: function() {
$( this ).remove();
}
} );

event.preventDefault();
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the return statement.

} );

// Helper functions
function replace ( string, values ) {
return string.replace( /\{\{([^}]+)}}/g, function( _, key ) {
return values[key];
});
}

clipboard = new Clipboard( "[data-clipboard-text]" );

clipboard.on( "success", function( e ) {
$( e.trigger )
.attr( "title", "Copied!" )
.tooltip()
.tooltip( "open" )
.one( "mouseout", function() {
$( this )
.removeAttr( "title" )
.tooltip( "destroy" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert the order so the tooltip is destroyed first. The title attribute becomes managed by the tooltip, so you're not actually removing the title when you use this order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this would completely change once the button always has a tooltip (like GitHub); at that point you won't be destroying it.

} );
} );

clipboard.on( "error", function( e ) {
$( e.trigger )
.attr( "title", "Press Ctrl+C to copy!" )
.tooltip()
.tooltip( "open" )
.one( "mouseout", function() {
$( this )
.removeAttr( "title" )
.tooltip( "destroy" );
} );
} );
})();