Skip to content

Commit

Permalink
Better style handling:
Browse files Browse the repository at this point in the history
* inject the style as a stand alone element
* support the meta directive to not include the default style

fixes bigskysoftware#13
  • Loading branch information
carson committed May 16, 2020
1 parent d4b4d46 commit 6e06c0d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/kutty.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ var kutty = kutty || (function () {
return trigger.split(/\s+/);
}

function addRule(rule) {
var sheet = getDocument().styleSheets[0];
sheet.insertRule(rule, sheet.cssRules.length);
}

function mergeObjects(obj1, obj2) {
for (var key in obj2) {
if (obj2.hasOwnProperty(key)) {
Expand Down Expand Up @@ -1261,16 +1256,32 @@ var kutty = kutty || (function () {
}
}

// insert kutty-indicator css rules
addRule(".kutty-indicator{opacity:0;transition: opacity 200ms ease-in;}");
addRule(".kutty-request .kutty-indicator{opacity:1}");
addRule(".kutty-request.kutty-indicator{opacity:1}");
// insert kutty-indicator css rules immediate, if not configured otherwise
(function() {
var metaConfig = getMetaConfig();
if (metaConfig === null || metaConfig.includeIndicatorStyles !== false) {
getDocument().head.insertAdjacentHTML("beforeend",
"<style>\
.kutty-indicator{opacity:0;transition: opacity 200ms ease-in;}\
.kutty-request .kutty-indicator{opacity:1}\
.kutty-request.kutty-indicator{opacity:1}\
</style>");
}
})();

function mergeMetaConfig() {
function getMetaConfig() {
var element = getDocument().querySelector('meta[name="kutty-config"]');
if (element) {
var source = JSON.parse(element.content);
kutty.config = mergeObjects(kutty.config , source)
return JSON.parse(element.content);
} else {
return null;
}
}

function mergeMetaConfig() {
var metaConfig = getMetaConfig();
if (metaConfig) {
kutty.config = mergeObjects(kutty.config , metaConfig)
}
}

Expand Down Expand Up @@ -1307,7 +1318,8 @@ var kutty = kutty || (function () {
historyCacheSize:10,
defaultSwapStyle:'innerHTML',
defaultSwapDelay:0,
defaultSettleDelay:100
defaultSettleDelay:100,
includeIndicatorStyles:true
},
version: "0.0.2",
_:internalEval
Expand Down
52 changes: 52 additions & 0 deletions test/img/bars.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/no-indicator-css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="kutty-config" content='{"includeIndicatorStyles":false}'>
<title>Test if the includeIndicatorStyles meta option works</title>
<script src="../src/kutty.js"></script>
</head>
<body>
<h1>You should see bars here:</h1>
<p>
We are overriding the normal CSS inclusion with the meta directive <code>{"includeIndicatorStyles":false}</code>
so you should see the indicator because it is not being hidden by the default classes.
</p>
<img class="kutty-indicator" src="img/bars.svg" width="200">
</body>
</html>
11 changes: 11 additions & 0 deletions test/yes-indicator-css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Test if indicators are invisible by default</title>
<script src="../src/kutty.js"></script>
</head>
<body>
<h1>You should not see bars here:</h1>
<img class="kutty-indicator" src="img/bars.svg" width="200">
</body>
</html>
1 change: 1 addition & 0 deletions www/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ Kutty allows you to configure a few defaults:
* `kutty.config.defaultSwapStyle` - defaults to `innerHTML`
* `kutty.config.defaultSwapDelay` - defaults to 0
* `kutty.config.defaultSettleDelay` - defaults to 100
* `kutty.config.includeIndicatorStyles` - defaults to `true` (determines if the `kutty-indicator` default styles are loaded, must be set in a `meta` tag before the kutty js is included)
You can set them directly in javascript, or you can use a `meta` tag:
Expand Down

0 comments on commit 6e06c0d

Please sign in to comment.