Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@htmlguyllc/jpack

Package Overview
Dependencies
Maintainers
1
Versions
191
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@htmlguyllc/jpack

Core Javascript Library of Everyday Objects, Events, and Utilities

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-47.37%
Maintainers
1
Weekly downloads
 
Created
Source

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

Standard Global

Download the latest release, unzip and move it in your website's public folder, then include it in your HTML. Don't forget to also include dependencies (listed in package.json).

<script href="/@htmlguyllc/jpack/dist/jpack.min.js">

<script>
//wait for the page to finish loading so we know jpack is ready
window.addEventListener('load', function() {
    
    //now you can take advantage of the jpack library
    var user_id = jpack.objects.user.getId();
    
    //or if you're feeling lazy, you can tie all jpack components to window for shorter use.
    jpack.goGlobal("jp");
    
    var user_id = jp.user.getId();
    
    //or if you're insanely lazy and want to cross your fingers that nothing conflicts, you can tie 
    // everything to window WITHOUT a namespace
    jpack.goGlobal();
    
    var user_id = user.getId();
};
</script>
With NPM or Yarn:
npm i @htmlguyllc/jpack;
//or
yarn add @htmlguyllc/jpack;
ES6 (Babel)
//a single component from it's own file
import {strings} from '@htmlguyllc/jpack/es/utilities/strings';
strings.ucfirst('bob');

//or a single component from the collection file
import {strings} from '@htmlguyllc/jpack/es/utilities';
strings.ucfirst('bob');

//or multiple components from the collection file
import {strings, dom} from '@htmlguyllc/jpack/es/utilities';
strings.ucfirst('bob');
dom.exists('a.my-link');

//or a namespaced object containing all components
import * as utilities from '@htmlguyllc/jpack/es/utilities';
utilities.strings.ucfirst('bob');

//or a namespaced object containing all
import {jpack} from '@htmlguyllc/jpack';
jpack.objects.user.getId();
CommonJS (Browserify)
var jpack = require('@htmlguyllc/jpack');

jpack.objects.user.getId();

Dependencies

NameRequired byLink
url-search-params-polyfillrequesthttps://www.npmjs.com/package/url-search-params-polyfill
axiosnavigationhttps://www.npmjs.com/package/axios
formdata-polyfillformhttps://www.npmjs.com/package/formdata-polyfill

What's Included

Four categories of functionality are provided in this library. Each one is detailed further in the documentation below.

TypeNamespaceComponents
Componentscomponentsnavigation, form
Objectsobjectsrequest, site, user
Plugin Wrappersplugin_wrappersNone Yet.
Utilitiesutilitiesstrings, data_types, dom, events

Documentation

- Components -

-Navigation

Grabs content from a URL and replaces it on the current page (along with browser history button handling, onload/unload handlers, and much more

Method/PropertyParams (name:type)ReturnNotes
setPassthroughDatadata:mixedselfset data you want provided in the onload method for the next page
clearPassthroughDataselfmust be called manually or the data will persist infinitely. you can set an onload callback to clear this every time
getPassthroughDatamixedreturns the data you set
setIncomingElementel:stringselfa selector string for the element being retrieved from another page which contains the HTML you want put on the current page
getIncomingElementstring
setReplaceElementel:stringselfa selector string for the element on the current page you want the new HTML to replace
getReplaceElementstring
loadurl:string, callback:function/null, incoming_el:string/null, replace_el:string/null, push_state:boolvoidpulls content from the provided URL and puts it on the current page - also swaps out the page title, metas, and much more
loaderEnabledn/aboolproperty to toggle the slow request loader on/off
setLoaderDelaydelay:intselfset how long a request should take in ms before the loader displays
getLoaderDelayself
getLoaderElElement
showLoaderselfshows the loader after the delay
hideLoaderselfclears the loader timeout and hides it
parseHTMLhtml:string, parent_el:stringobjectparses HTML from the request to get key components like metas and the HTML to be displayed
getRouteFromMetahtml:stringstringretrieves the value of a meta tag named "current_route" to be passed in the onload event to help trigger page-specific JS
replacePageContenthtml:string, url:string, incoming_el:string, replace_el:string, push_state:boolselfreplaces HTML on the page with the new content, updates metas, runs the unload and load callbacks and more
reloadcallback:functionselfreloads the current page using .load()
fullReloadvoidperforms a full browser refresh of the current page
redirecturl:stringvoidredirects the user to a new page (no XHR request)
setTitletitle:stringselfsets the page title
onLoadcallback:functionselfadd an onload callback (runs 100ms after unload)
onUnloadcallback:functionselfadd an unload callback
onNavigationFailurecallback:functionselfadd a callback when the load() request fails - the error message is provided in event.detail.error
triggerOnLoadel:mixed, el_selector:string, route:stringselftriggers all onload callbacks
triggerUnloadel:mixed, el_selector:string, route:stringselftriggers all unload callbacks
triggerNavigationFailureerror:stringselftriggers the nav failure and provides an error message
initHistoryHandlersselfsets event listeners to handle back/forward navigation in the user's browser
To use:
import {navigation} from '@htmlguyllc/jpack/es/components'; 

//handles browser back/forward buttons
navigation.initHistoryHandlers();

//a selector that contains the HTML you'd like to pull from the response
navigation.setIncomingElement('#main-content');

//a selector that will be replaced with the incoming HTML
navigation.setReplaceElement('#main-content');

//enables a loader to show if a request takes more than 300ms
//WARNING: No styling is provided at this time. It uses Bootstrap 4's progress-bar classes.
navigation.loaderEnabled = true;
navigation.setLoaderDelay(300);

//things to do when a page loads
navigation.onLoad(function(e){
    var params = e.detail; //get info from the event
    //if a current_route meta was set in the incoming HTML, it'll be provided to you here
    //you can use this to kick off your page-specific JS
    var route = params.route; 
    //the data you set prior to loading the page, if any
    var data = params.data; //or navigation.getPassthroughData()
    //the DOM element that was added to the page replacing the previous
    var el = params.el;
    
    //el_selector is the incoming selector that was used for this request
    //WARNING: If you change the incomingElement for a single request
    //and the replaceElement is not the same, future requests will not work
    //because the replaceElement no longer exists in the DOM
    //in this case you'll want to run navigation.setReplaceElement(params.el_selector);
    //now future requests will replace the new element on the page
    var el_selector = params.el_selector;
   
   //if gtag is set (google analytics), push a page view
   if( typeof gtag !== 'undefined' ) {
       gtag('config', 'GA_MEASUREMENT_ID', {
           page_path: request.getURIWithQueryString()
       });
   }
   
   //scroll to the top of the page
   window.scrollTo(0, 0);
   
   //.. do something...like init tooltip plugins
});

//things to do when leaving a page
navigation.onUnload(function(){
   //.. do something...like remove generic event handlers or destroy plugins 
});

//things to do when a page fails to load
navigation.onNavigationFailure(function(e){
    var error = e.detail.error;
    //.. do something...like show an error popup for the user or log the issue
});

//to prevent duplicate code, you can run your onload callbacks immediately
navigation.triggerOnLoad(dom.getElement('body'), 'body', navigation.getRouteFromMeta());

//now use the plugin to load pages
//if you're lazy, the fastest way to integrate is to just add data-href to all internal links 
//and then attach a handler like this:
import {events} from '@htmlguyllc/jpack/es/utilities';
events.onClick('[data-href]', function(){
   navigation.load(this.href); 
});

//set something that will be received onload of the next page
navigation.setPassthroughData({
    product_id:1
});

//you can use the load method at any time to load a new page 
// the second param is an optional callback that only runs for that page
navigation.load('/my-page', function(new_el, new_el_selector, pass_through_params){
    //my page is now loaded
    
    //new_el is the new element on the page
    //new_el_selector is the incomingSelector used for this request
    
    //pass_through_data is any data that was set on navigation prior to this request
    //so in this instance, it will be {product_id:1} (set above)
    
    //now clear that data so it's gone for the next page load
    navigation.clearPassthroughData();
});

//in some cases, you'll want to grab a different element from the URL
//this example grabs .popup-content from /my-popup and replaces .current-popup
navigation.load('/my-popup', function(new_el, el_sel, data){
   //now the new element is on the page
}, '.popup-content', '.current-popup');

-XHRForm

Submits a form using XHR

Method/PropertyParams (name:type)ReturnNotes
constructorform:Element,options:objectself
setXHRSubmitenabled:boolselfenable/disable the XHR submission of the form
setSubmitMethodmethod:stringselfoverride the form and provide a method (GET, POST, PATCH)
getSubmitMethodmethod:string
setSubmitURLurl:mixedselfpass null to use the form's action, function to dynamically generate the URL (receives the form as a param), or string
getSubmitURLurl:stringreturns whatever was set in the constructor or using setSubmitURL, not the final URL
attachSubmitHandlerform:mixedselfattaches the event listener to on submit of the passed form
onSuccesscallback:functionselfadds an onSuccess callback (you can add as many as you'd like)
clearOnSuccessCallbacksself
triggerOnSuccessresponse:mixed, form:Elementselfruns all onSuccess callbacks and passes the server's response and the form element
onErrorcallback:functionselfadds an onError callback (you can add as many as you'd like)
clearOnErrorCallbacksself
triggerOnErrorerror:string, response:mixed, form:Elementselftriggers all onError callbacks and passes the error string, server response, and form Element
submitFormform:Elementselfgets URL and method, checks form validity using .validate(), gets values, submits, and kicks off callbacks
getFormValuesform:Elementselfreturns data from the form to be submitted - override this if you want to manipulate it first
setValidateCallbackcallback:functionis_valid:boolpass a function to validate the form and return true if it's valid, false if it's not. False prevents form submission so you must display errors for the user within here. The default callback uses Bootstrap 4's "was-validated" class to show errors and HTML5's :invalid attribute to validate
validateform:Elementboolpasses the form to the validate callback and returns the response
To use:
import {XHRForm} from '@htmlguyllc/jpack/es/components'; 

//shown with defaults
var remote_form = new XHRForm(document.getElementById('my-form'), {
        xhrSubmit: true, //wouldn't make a whole lotta sent to use this if this were false lol, but it's here for extending classes and incase you want to toggle it for whatever reason
        submitURL:null, //when null, the form's action will be used (if explicitly defined), otherwise it falls back to the URL the form was retrieved from
        submitMethod:null, //when null, the form's method will be used (if explicitly defined), otherwise it falls back to POST
        onError: function(error, response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
        onSuccess: function(response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
        //validate the form, display any errors and return false to block submission
        validateForm: function(form){
            //add .was-validated for bootstrap to show errors
            form.classList.add('was-validated');
    
            //if there are any :invalid elements, the form is not valid
            const is_valid = !form.querySelector(':invalid');
    
            //if it's valid, clear the validation indicators
            if( is_valid ) form.classList.remove('was-validated');
    
            return is_valid;
        }
});

//attach the submission handler
remote_form.attachSubmitHandler();

-FormFromURL

Allows you to pull a form from a URL and insert it into the current page very easily including optional XHR form submission!

Method/PropertyParams (name:type)ReturnNotes
constructorurl:string, options:objectself
setURLurl:stringselfset the URL to pull the form from
getURLurl:string
setIncomingElementSelectorselector:stringselfset a selector for the form element or it's parent that is returned by the URL
getIncomingElementSelectorselector:string
setInsertIntoElementelement:mixedselfset the element that the form should be inserted into
getInsertIntoElementelement:mixed
getFormvoidpulls the form from the URL and runs the insertForm method
insertFormparsed_content:object, response:mixed, form:Element/nullel:Elementinserts the form into the parent element, attaches the submit handler, triggers onload, and returns the parent element
onloadcallback:functionselfadds a callback function to be run when the form is loaded on the page
clearOnloadCallbacksselfremoves all onload callbacks
triggerOnloadform:Elementselfruns all onload callbacks and passes the form to them

There are several methods and properties inherited from XHRForm that are not listed here. Please see XHRForm above for those details

To use:
import {FormFromURL} from '@htmlguyllc/jpack/es/components'; 

//shown with defaults
var remote_form = new FormFromURL('/my-form', {
        incomingElementSelector: null, //when null, it assumes the entire response is the form's HTML
        insertIntoElement: null, //error on null, must provide this
        onload: function(form){ return this; }, //although you can add more, you can only pass 1 to start with in the constructor
        xhrSubmit: true, 
        submitURL:null, //when null, the form's action will be used (if explicitly defined), otherwise it falls back to the URL the form was retrieved from
        submitMethod:null, //when null, the form's method will be used (if explicitly defined), otherwise it falls back to POST
        onError: function(error, response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
        onSuccess: function(response, form){ }, //although you can add more, you can only pass 1 to start with in the constructor
        //validate the form, display any errors and return false to block submission
        validateForm: function(form){
            //add .was-validated for bootstrap to show errors
            form.classList.add('was-validated');
    
            //if there are any :invalid elements, the form is not valid
            const is_valid = !form.querySelector(':invalid');
    
            //if it's valid, clear the validation indicators
            if( is_valid ) form.classList.remove('was-validated');
    
            return is_valid;
        }
});

//grab the form and insert in into the "insertIntoElement"
remote_form.getForm();
Extending:

FormFromURL extends XHRForm and either can be extended as you need.

See examples/FormModalFromURL for an example

- Objects -

-Request

Provides a wrapper for window.location and query string access

Method/PropertyParams (name:type)ReturnNotes
queryn/aURLSearchParamssee https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Methods
isHttpsbool
getDomainstring
getDomainWithProtocolstring
getURIstringalso known as the path - does not include querystring
getURIWithQueryStringstringfull URL after the domain
getFullURLstring
appendSlashstring:stringstringadds a slash (if there isn't already one) to the end of a string.
To use:
import {request} from '@htmlguyllc/jpack/es/objects'; 

//get product_id from the querystring
var product_id = request.query.get('product_id');

var current_full_url = request.getFullURL();

var full_blog_url = request.appendSlash(request.getDomainWithProtocol())+'blog'; //https://my-domain.com/blog

-Site

Designed for multi-tenant applications, this object stores a site's id, name, and config object.

Method/PropertyParams (name:type)ReturnNotes
getIdstring/int
setIdid:string/intthis
getNamestringthis
setNamename:stringthis
getConfigobjectthis
setConfigconfig:objectthisoverwrites all config
getConfigItemmixedreturns an individual item from config
setConfigItemkey:string,val:mixedthissets the value of an individual item in config
populatedata:objectthissets 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']; ?>",
    //$site['config'] is a key/val array of configuration options
    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/es/objects';
 
//this example uses jQuery's shorthand AJAX call, you can use axios or any request you want
$.get('/my-site-info-endpoint.php', function(data){
    //don't forget error handling!
    site.populate(JSON.parse(data));
});
To Use:
import {site} from '@htmlguyllc/jpack/es/objects';

var site_id = site.getId();

-User

Designed for sites with user accounts/guest accounts. This object stores a user's details and allows for front-end permission checks.

Method/PropertyParamsReturnNotes
getIdstring/int
setIdid:string/intthis
getIsGuestboolif your site has users who don't login but still interact and have a user record (like guest checkout)
setIsGuestis_guest:boolthis
setIsAdminboolif your site has users who have ultimate permissions and you want to do something based on that
setIsGuestis_admin:boolthis
getUsernamestring
setUsernameusername:stringthis
getFnamestring
setFnamefname:stringthis
getLnamestring
setLnamelname:stringthis
getNamestringreturns fname and lname concatenated with a space
getEmailstring
setEmailemail:stringthis
getPhonestring
setPhonephone:stringthis
getPermissionsarray
setPermissionspermissions:arraythis
addPermissionpermission:string/intthis
removePermissionpermission:string/intthis
hasPermissionpermission:string/intbool
getAdditionalDataobjectset 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)
setAdditionalDatadata:objectthis
getDataItemkey:stringmixedreturns a single value from the additional data object/array
setDataItemkey:string, val:mixedsets a single value in the additional data array/object
populatedata:objectthissets 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/es/objects';
 
$.get('/my-user-info-endpoint.php', function(data){
    //don't forget error handling!
    user.populate(JSON.parse(data));
});
To use:
import {site} from '@htmlguyllc/jpack/es/objects';

var site_id = site.getId();

- Plugin Wrappers -

None yet.

- Utilities -

-Strings

Common string manipulations

Method/PropertyParams (name:type)ReturnNotes
ucfirststring:stringstringcapitalizes the first letter of a string like ucfirst in PHP
getterstring:stringstringcreates a getter method name from a string
setterstring:stringstringcreates a setter method name from a string
To Use:
import {strings} from '@htmlguyllc/jpack/es/utilities';

strings.ucfirst('bob'); //returns 'Bob'
strings.getter('name'); //returns 'getName';
strings.setter('name'); //returns 'setName';

-DOM

HTML DOM helpers

Method/PropertyParams (name:type)ReturnNotes
getElementel:mixed, error_on_none:bool, error_on_multiple:boolElement/HTMLDocument/nullreturns a native DOM element for whatever you provide (selector string, array of elements, single element, jQuery wrapped DOM element, etc)
getElementsel:mixed, error_on_none:boolarraysame as getElement, except it returns all matches
removeel:mixedthisremoves elements from the DOM - uses .getElements()
replaceElWithHTMLel:mixed, html:stringElementreplaces an element in the DOM with HTML and returns a reference to the new Element
existsel:mixedboolchecks to see if it exists in the DOM
multipleExistel:mixedboolchecks to see if more than 1 instance exists in the DOM
To Use:
import {dom} from '@htmlguyllc/jpack/es/utilities';

//Dont do this. Most of these are dumb examples.
dom.getElement('.my-fav-button', true, true); //will throw an error if it doesn't find it, or if it finds more than 1
dom.getElements('.links', true); //will throw an error if none are found
dom.getElement('.my-button'); //returns the button, or null (if multiple, it returns the first)
dom.getElements('.links'); //returns an array of any matches for .links
dom.getElement($('a')); //returns the native DOM element for the link and removes the jQuery wrapper
dom.getElement(document.querySelectorAll('a')); //returns the first anchor

dom.exists('a'); //returns true if there is an anchor on the page
dom.multipleExist('a'); //returns true if more than 1 anchor on the page

-Type Checks

Check the data type of a value with more specificity than typeof or vanilla JS functions

Method/PropertyParams (name:type)ReturnNotes
isDataObjectvalue:object, keys:array, require_all_keys:bool, block_other_keys:bool, throw_error:boolboolvalidates 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/es/utilities';

var my_obj = {id:null, name:'John Doe', email:'john@doe.com'};

//make sure my_obj contains the keys: 'id', 'name', 'email'
//force all keys to exist
//block any keys that aren't in that list
//throw an error if the object fails any checks
type_checks.isDataObject(my_obj, ['id', 'name', 'email'], true, true, true);

-Events

Shorthand event handlers

Method/PropertyParams (name:type)ReturnNotes
onClickel:mixed, callback:functionarrayprevents the browser's default so you can handle link clicks and form submissions with less code
offClickel:mixed, callback:functionarrayremoves the handler you added using onClick
onSubmitel:mixed, callback:functionarraysame as .onClick() but for submit
offSubmitel:mixed, callback:functionarraysame as .offClick() but for submit
onChangeel:mixed, callback:functionarrayadds an on change handler but does NOT preventDefault - mostly exists for consistency
offChangeel:mixed, callback:functionarrayremoves the handler you added using .onChange()
onEventPreventDefaultel:mixed, event:string, callback:functionarrayattaches an event handler and prevents the default browser action
offEventPreventDefaultel:mixed, event:string, callback:functionarrayremoves the handler you attached with .onEventPreventDefault()
onel:mixed, event:string, callback:functionarrayattaches an event listener
offel:mixed, event:string, callback:functionarrayremoves an event listener
triggerel:mixed, event:string, event_options:mixedarraytriggers an event on an element/elements - uses .getElements()
To Use:
import {events} from '@htmlguyllc/jpack/es/utilities';

events.onClick('a.my-link', function(){
   //do something without the page redirecting to the href 
});

events.onSubmit('form.my-form', function(){
   //do something and submit the form using XHR 
});

//trigger submit on a form and pass an object as additional data in the event
events.trigger('.my-form', 'submit', {id:1});

Keywords

FAQs

Package last updated on 14 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc