Skip to content

Commit

Permalink
✨ Update
Browse files Browse the repository at this point in the history
📚 Change log.
🆕 index.html now in examples folder.
🆕 Theme files.
🆕 Update example with jquery 3.
🆕 Update code with new standars.
  • Loading branch information
MiguelFernandez008 committed Sep 15, 2024
1 parent 41fc92b commit cc5d26a
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 134 deletions.
6 changes: 6 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: JQueryHash
description: A Jquery plugin that allow users to add hooks when the url hash changes.
show_downloads: true
remote_theme: pages-themes/cayman@v0.2.0
plugins:
- jekyll-remote-theme # add this line to the plugins list if you already have one
41 changes: 41 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="{{ site.lang | default: "en-US" }}">
<head>
<meta charset="UTF-8">

{% seo %}
<link rel="preconnect" href="https://fonts.gstatic.com">
<link rel="preload" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700&display=swap" as="style" type="text/css" crossorigin>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#008080">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}">
{% include head-custom.html %}
</head>
<body>
<a id="skip-to-content" href="#content">Skip to the content.</a>

<header class="page-header" role="banner">
<h1 class="project-name">{{ page.title | default: site.title | default: site.github.repository_name }}</h1>
<h2 class="project-tagline">{{ page.description | default: site.description | default: site.github.project_tagline }}</h2>
{% if site.github.is_project_page %}
<a href="{{ site.github.repository_url }}" class="btn">View on GitHub</a>
{% endif %}
{% if site.show_downloads %}
<a href="{{ site.github.zip_url }}" class="btn">Download .zip</a>
<a href="{{ site.github.tar_url }}" class="btn">Download .tar.gz</a>
{% endif %}
</header>

<main id="content" class="main-content" role="main">
{{ content }}

<footer class="site-footer">
{% if site.github.is_project_page %}
<span class="site-footer-owner"><a href="{{ site.github.repository_url }}">{{ site.github.repository_name }}</a> is maintained by <a href="{{ site.github.owner_url }}">{{ site.github.owner_name }}</a>.</span>
{% endif %}
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a>.</span>
</footer>
</main>
</body>
</html>
4 changes: 4 additions & 0 deletions assets/css/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

@import "{{ site.theme }}";
68 changes: 68 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="es">
<head>
<title>Jquery Hash plugin</title>
<script
src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
crossorigin="anonymous"
></script>
<script src="../jquery.jqueryhash.js"></script>
</head>
<body>
<a href="#asdf" id="asdf">ASDF Hash</a>
<a href="#qwerty" id="qwerty">QWERTY Hash</a>
<a href="#new" id="new">NEW Hash</a>
<a href="#" id="updateNew">Update new handler</a>
<a href="#" id="addNew">Add new handler</a>
<a href="#" id="triggerNew">Trigger new handler</a>
<a href="#" id="removeQuerty">Remove querty handler</a>
<a href="#" id="removeAll">Remove all handlers</a>
<script>
$(function () {
$.onHash({
hashes: [
[
"asdf",
function () {
console.log(123);
},
],
[
"qwerty",
function () {
console.log(456);
},
],
],
onlyOnLoad: false,
});
$("#removeAll").on("click", function () {
$.onHash("onRemoveHashChange");
});
$("#removeQuerty").on("click", function () {
$.onHash("onRemoveOneHandler", "qwerty");
});
$("#addNew").on("click", function () {
$.onHash("onAddHandler", [
"new",
function () {
console.log(789);
},
]);
});
$("#updateNew").on("click", function () {
$.onHash("onUpdateOne", [
"new",
function () {
console.log(2222);
},
]);
});
$("#triggerNew").on("click", function () {
$.onHash("onTriggerOne", "new");
});
});
</script>
</body>
</html>
44 changes: 0 additions & 44 deletions index.html

This file was deleted.

188 changes: 98 additions & 90 deletions jquery.jqueryhash.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,120 @@
(function($){
(function ($) {
// Variables
let settings;
const _jqWindow = $(window);

// Variables
var settings;
var _jqWindow = $(window);
// Plugin methods
const methods = {
init: function (options) {
settings = $.extend(
{
hashes: [["", function () {}]],
onlyOnLoad: false,
},
options
);

// Plugin methods
var methods = {
init: function(options) {
settings = $.extend({
hashes: [['', function() {}]],
onlyOnLoad: false
}, options);

// We start a events listener
methods.onCheckHash();
if(!settings.onlyOnLoad)
_jqWindow.on('hashchange', methods.onCheckHash);
// We start a events listener
methods.onCheckHash();
if (!settings.onlyOnLoad) _jqWindow.on("hashchange", methods.onCheckHash);
},
// Add one handler
onAddHandler: function(handler) {
if( typeof handler[0] === 'string' && $.isFunction(handler[1]) ) {
settings.hashes.push([ handler[0], handler[1] ]);
}
else { throw "Incorrect format of the handler."; }
onAddHandler: function (handler) {
if (typeof handler[0] === "string" && typeof handler[1] === "function") {
settings.hashes.push([handler[0], handler[1]]);
} else {
throw "Incorrect format of the handler.";
}
},
// Update one handler
onUpdateOne: function(handler) {
// Variable used to error checking
var _boolExits = false;
if( typeof handler[0] === 'string' ) {
for(var i = 0; i < settings.hashes.length; i++) {
if( settings.hashes[i][0] === handler[0] ) {
if($.isFunction(handler[1])) {
settings.hashes[i][1] = handler[1];
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
onUpdateOne: function (handler) {
// Variable used to error checking
let _boolExits = false;
if (typeof handler[0] === "string") {
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler[0]) {
if (handler[1] && typeof handler[1] === "function") {
settings.hashes[i][1] = handler[1];
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
if(!_boolExits)
throw 'Trying to update an unexisting handler';
}
}
if (!_boolExits) throw "Trying to update an unexisting handler";
}
},
// Remove only one handler
onRemoveOneHandler: function(handler) {
for(var i = 0; i < settings.hashes.length; i++) {
if(settings.hashes[i][0] === handler) {
settings.hashes.splice(i, 1);
break;
}
onRemoveOneHandler: function (handler) {
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler) {
settings.hashes.splice(i, 1);
break;
}
}
},
// Remove all handlers
onRemoveHashChange: function() {
if(!settings.onlyOnLoad) {
_jqWindow.off('hashchange', methods.onCheckHash);
settings.hashes = [['', function() {}]];
}
onRemoveHashChange: function () {
if (!settings.onlyOnLoad) {
_jqWindow.off("hashchange", methods.onCheckHash);
settings.hashes = [["", function () {}]];
}
},
// Trigger one handler
onTriggerOne: function(handler) {
// Variable used to error checking
var _boolExits = false;
for(var i = 0; i < settings.hashes.length; i++) {
if(settings.hashes[i][0] === handler) {
if($.isFunction(settings.hashes[i][1])) {
settings.hashes[i][1]();
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
onTriggerOne: function (handler) {
// Variable used to error checking
let _boolExits = false;
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === handler) {
if (
settings.hashes[i][1] &&
typeof settings.hashes[i][1] === "function"
) {
settings.hashes[i][1]();
_boolExits = true;
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
if(!_boolExits)
throw 'Trying to trigger an unexisting handler';
}
if (!_boolExits) throw "Trying to trigger an unexisting handler";
},
// Function used to check the hash and fire the function associated with it
onCheckHash: function() {
if(window.location.hash) {
var _strHash = window.location.href.split('#')[1];
for(var i = 0; i < settings.hashes.length; i++) {
if( settings.hashes[i][0] === _strHash ) {
if($.isFunction(settings.hashes[i][1])) {
settings.hashes[i][1]();
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
onCheckHash: function () {
if (window.location.hash) {
let _strHash = window.location.href.split("#")[1];
for (let i = 0; i < settings.hashes.length; i++) {
if (settings.hashes[i][0] === _strHash) {
if (
settings.hashes[i][1] &&
typeof settings.hashes[i][1] === "function"
) {
settings.hashes[i][1]();
// No need to keep the for loop
break;
} else {
throw "Error trying to execute the callback function.";
}
}
}
}
}
}
}
},
};

$.onHash = function(method) {
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if(typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
$.onHash = function (method) {
if (methods[method]) {
return methods[method].apply(
this,
Array.prototype.slice.call(arguments, 1)
);
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
throw 'This method ' + method + ' does not belong to this plugin.';
throw "This method " + method + " does not belong to this plugin.";
}
};

})(jQuery);
};
})(jQuery);

0 comments on commit cc5d26a

Please sign in to comment.