jPack
jPack is a library of components, objects, plugin wrappers, and utilities designed to make building custom websites simpler.
With jPack, you can easily upgrade your server-side rendered application to a pseudo-SPA using XHR requests for page-loads, get values from the querystring, store and interact with user/multi-tenant website data, and more.
Installation
With Yarn or NPM:
yarn add htmlguyllc-jpack;
//or
npm i htmlguyllc-jpack;
and then use what you need, where you need it (requires ES6):
import {navigation} from 'htmlguyllc-jpack/components/navigation';
navigation.load('/my-page');
import 'htmlguyllc-jpack/components';
components.navigation.load('/my-page');
import 'htmlguyllc-jpack';
jpack.components.navigation.load('/my-page');
import {user, site} from 'htmlguyllc-jpack/objects';
user.getId();
site.getId();
Or you can download the latest release, unzip it, put it in your public folder then include the whole library:
<script href="/vendor/htmlguyllc-jpack/dist/jpack.min.js">
<script>
window.addEventListener('load', function() {
jpack.components.navigation.load('/my-page');
};
</script>
What's Included:
Four categories of functionality are provided in this library.
Each has it's own namespace in parenthesis below.
Components (components):
None yet.
Objects (objects):
request, site, user
Plugin Wrappers (plugin_wrappers):
None yet.
Utilities (utilities):
strings, data_types, dom, events
Documentation
- Components -
None yet.
- Objects -
-Request
Provides a wrapper for window.location and query string access
Method/Property | Params | Return | Notes |
---|
query | n/a | URLSearchParams | see https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Methods |
isHttps | | bool | |
getDomain | | string | |
getDomainWithProtocol | | string | |
getURI | | string | also known as the path - does not include querystring |
getURIWithQueryString | | string | full URL after the domain |
getFullURL | | string | |
appendSlash | string | string | adds a slash (if there isn't already one) to the end of a string. |
To use:
import {request} from 'htmlguyllc-jpack/objects';
var product_id = request.query.get('product_id');
var current_full_url = request.getFullURL();
var full_blog_url = request.appendSlash(request.getDomainWithProtocol())+'blog';
-Site
Designed for multi-tenant applications, this object stores a site's id, name, and config object.
Method/Property | Params | Return | Notes |
---|
getId | | string/int | |
setId | string/int | this | |
getName | | string | this |
setName | string | this | |
getConfig | | object/array | this |
setConfig | object/array | this | overwrites all config |
getConfigItem | | mixed | returns an individual item from config |
setConfigItem | string,mixed | this | sets the value of an individual item in config |
populate | object | this | sets provided values all at once (id,name,config) |
To populate with data:
The easy way: Create an object named $site with values from your server (prior to including jpack)
<script>
const $site = {
id: <?php echo $site['id']; ?>,
name: "<?php echo $site['name']; ?>",
config: JSON.parse("<?php echo json_encode($site['config']); ?>"),
};
</script>
The harder way: Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.
import {site} from 'htmlguyllc-jpack/objects';
$.get('/my-site-info-endpoint.php', function(data){
site.populate(JSON.parse(data));
});
-User
Designed for sites with user accounts/guest accounts. This object stores a user's details and allows for front-end permission checks.
Method/Property | Params | Return | Notes |
---|
getId | | string/int | |
setId | string/int | this | |
getIsGuest | | bool | if your site has users who don't login but still interact and have a user record (like guest checkout) |
setIsGuest | bool | this | |
setIsAdmin | | bool | if your site has users who have ultimate permissions and you want to do something based on that |
setIsGuest | bool | this | |
getUsername | | string | |
setUsername | string | this | |
getFname | | string | |
setFname | string | this | |
getLname | | string | |
setLname | string | this | |
getName | | string | returns fname and lname concatenated with a space |
getEmail | | string | |
setEmail | string | this | |
getPhone | | string | |
setPhone | string | this | |
getPermissions | | array | |
setPermissions | array | this | |
addPermission | string/int | this | |
removePermission | string/int | this | |
hasPermission | string/int | bool | |
getAdditionalData | | object/array | set any additional data about this user that doesn't fit the default getters and setters here (a better idea would be to extend this object with your custom properties/methods) |
setAdditionalData | object/array | this | |
populate | object | this | sets provided values all at once (id, isGuest, isAdmin, etc) |
To populate with data:
The easy way: Create an object named $user with values from your server (prior to including jpack)
<script>
const $user = {
id: <?php echo $user['id']; ?>,
fname: "<?php echo $user['fname']; ?>",
//..
permissions: JSON.parse("<?php echo json_encode($user['permissions']); ?>"),
additionalData: JSON.parse("<?php echo json_encode([
'user_type'=>$user['user_type'],
//whatever else you might want to pass
]); ?>"),
//..
};
</script>
The harder way: Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.
import {user} from 'htmlguyllc-jpack/objects';
$.get('/my-user-info-endpoint.php', function(data){
user.populate(JSON.parse(data));
});
To use:
import {site} from 'htmlguyllc-jpack/objects';
var site_id = site.getId();
- Plugin Wrappers -
None yet.
- Utilities -
-Strings
Common string manipulations
Method/Property | Params | Return | Notes |
---|
ucfirst | string | string | capitalizes the first letter of a string like ucfirst in PHP |
getter | string | string | creates a getter method name from a string |
setter | string | string | creates a setter method name from a string |
To Use:
import {strings} from 'htmlguyllc-jpack/utilities';
strings.ucfirst('bob');
strings.getter('name');
strings.setter('name');
-DOM
HTML DOM helpers
Method/Property | Params | Return | Notes |
---|
getDomElement | mixed, bool, bool | Element/HTMLDocument/null | returns a native DOM element for whatever you provide (selector string, array of elements, single element, jQuery wrapped DOM element, etc) |
getDomElements | mixed, bool | array | same as getDomElement, except it returns all matches |
exists | mixed | bool | checks to see if it exists in the DOM |
multipleExist | mixed | bool | checks to see if more than 1 instance exists in the DOM |
To Use:
import {dom} from 'htmlguyllc-jpack/utilities';
dom.getDomElement('.my-fav-button', true, true);
dom.getDomElements('.links', true);
dom.getDomElement('.my-button');
dom.getDomElements('.links');
dom.getDomElement($('a'));
dom.getDomElement(document.querySelectorAll('a'));
dom.exists('a');
dom.multipleExist('a');
-Type Checks
Check the data type of a value with more specificity than typeof or vanilla JS functions
Method/Property | Params | Return | Notes |
---|
isDataObject | object, array, bool, bool, bool | bool | validates that an object contains data and not a dom element, array, null or anything else that would normally return true when you call typeof |
To Use:
import {type_checks} from 'htmlguyllc-jpack/utilities';
var my_obj = {id:null, name:'John Doe', email:'john@doe.com'};
type_checks.isDataObject(my_obj, ['id', 'name', 'email'], true, true, true);
-Events
Shorthand event handlers
Method/Property | Params | Return | Notes |
---|
onClick | mixed, function | array | prevents the browser's default so you can handle link clicks and form submissions with less code |
offClick | mixed, function | array | removes the handler you added using onClick |
onSubmit | mixed, function | array | same as .onClick() but for submit |
offSubmit | mixed, function | array | same as .offClick() but for submit |
onChange | mixed, function | array | adds an on change handler but does NOT preventDefault - mostly exists for consistency |
offChange | mixed, function | array | removes the handler you added using .onChange() |
onEventPreventDefault | mixed, string, function | array | attaches an event handler and prevents the default browser action |
offEventPreventDefault | mixed, string, function | array | removes the handler you attached with .onEventPreventDefault() |
To Use:
import {events} from 'htmlguyllc-jpack/utilities';
events.onClick('a.my-link', function(){
});
events.onSubmit('form.my-form', function(){
});