Skip to content

Commit fdbf216

Browse files
committed
Restructured and removed namespaces, detailed each component in the README at the top, separated forms into their own files, changed goGlobal to setGlobal, and more
1 parent 4529510 commit fdbf216

File tree

26 files changed

+1730
-1790
lines changed

26 files changed

+1730
-1790
lines changed

README.md

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# jPack
2-
jPack is a library of components, objects, plugin wrappers, and utilities designed to make building custom websites simpler.
3-
4-
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.
2+
jPack is a library of components, classes, plugin wrappers, and utilities designed to make building custom websites simpler.
53

64
# What's Included
75

8-
Four categories of functionality are provided in this library.
9-
Each one is detailed further in the documentation below.
10-
11-
Type | Namespace | Object/Function/Class
12-
--- | --- | ---
13-
Components | components | navigation, XHRForm, FormFromURL
14-
Objects | objects | request, Site, User
15-
Plugin Wrappers | plugin_wrappers | None Yet.
16-
Utilities | utilities | strings, data_types, dom, events
6+
Component | Data Type | Singleton? | What it does
7+
--- | --- | --- | ---
8+
navigation | object | yes | Grabs HTML from a URL and replaces content on the current page. Handles browser history, meta title swaps, and offers several callbacks
9+
XHRForm | class | no | Adds an on-submit listener and sends the form values using XHR with callbacks for success/failure
10+
FormFromURL (extends XHRForm) | class | no | Grabs a form from a URL and places it on the current page (examples/FormModalFromURL shows how to put the form in a modal) and then uses an XHR request to submit the form
11+
request | object | yes | Provides a wrapper for window.location and easy querystring interaction
12+
Site | class | no | A generic website class with properties for id, name, and config - useful for multi-tenant applications where you need to know which site is being viewed
13+
User | class | no | A generic user class with properties for id, name, email, phone, etc - also allows for front-end permission checks
14+
strings | object | yes | Contains methods for semi-common string manipulation like creating a getter from a string ('hi' = 'getHi')
15+
data_types | object | yes | Validate the value of a variable with higher specificity than built-in functions. For instance, you can validate an object contains specific keys and throw errors if not, or if it contains keys that you didn't define
16+
dom | object | yes | Has methods for converting just about anything into a native DOM Element or array of them (you can provide a string selector, jQuery object, native DOM object, etc). Also has some shortcuts for common DOM checks/manipulation (like removing an element, verifying an element exists in the DOM, or replacing an element with HTML)
17+
events | object | yes | Includes shorthand methods for preventing the browser's default action onsubmit, onclick. Other methods are included for consistency like onchange (which does not prevent default since that is generally not preferred)
1718

1819
# Installation
1920

@@ -30,16 +31,15 @@ Don't forget to also include dependencies (listed in package.json).
3031
window.addEventListener('load', function() {
3132
3233
//now you can take advantage of the jpack library
33-
var fname = jpack.utilities.strings.ucfirst('bob'); //Bob
34+
jpack.strings.ucfirst('bob'); //Bob
3435
35-
//or if you're feeling lazy, you can tie all jpack components to window for shorter use.
36-
jpack.goGlobal("jp");
36+
//if you want to change the namespace
37+
jpack.setGlobal("$");
3738
38-
jp.strings.ucfirst('bob');
39+
$.strings.ucfirst('bob');
3940
40-
//or if you're insanely lazy and want to cross your fingers that nothing conflicts, you can tie
41-
// everything to window WITHOUT a namespace
42-
jpack.goGlobal();
41+
//if you want to make all global without a namespace - do so at your own risk! Things may conflict!
42+
jpack.setGlobal();
4343
4444
strings.ucfirst('bob');
4545
};
@@ -56,33 +56,25 @@ yarn add @htmlguyllc/jpack;
5656
#### ES6 (Babel)
5757
```javascript
5858
//a single component from it's own file
59-
import {strings} from '@htmlguyllc/jpack/es/utilities/strings';
60-
strings.ucfirst('bob');
61-
62-
//or a single component from the collection file
63-
import {strings} from '@htmlguyllc/jpack/es/utilities';
59+
import {strings} from '@htmlguyllc/jpack/es/strings';
6460
strings.ucfirst('bob');
6561
66-
//or multiple components from the collection file
67-
import {strings, dom} from '@htmlguyllc/jpack/es/utilities';
62+
//or multiple components from jpack
63+
import {strings, dom} from '@htmlguyllc/jpack';
6864
strings.ucfirst('bob');
6965
dom.exists('a.my-link');
7066
7167
//or a namespaced object containing all components
72-
import * as utilities from '@htmlguyllc/jpack/es/utilities';
73-
utilities.strings.ucfirst('bob');
74-
75-
//or a namespaced object containing all
76-
import {jpack} from '@htmlguyllc/jpack';
77-
jpack.utilities.strings.ucfirst('bob');
68+
import * as j from '@htmlguyllc/jpack';
69+
j.strings.ucfirst('bob');
7870
```
7971
8072
#### CommonJS (Browserify)
8173
```javascript
8274
var jpack = require('@htmlguyllc/jpack');
8375
8476
//now use it
85-
jpack.utilities.strings.ucfirst('bob');
77+
jpack.strings.ucfirst('bob');
8678
```
8779
8880
# Dependencies
@@ -95,8 +87,6 @@ formdata-polyfill | XHRForm (and anything that extends it) | https://www.npmjs.c
9587
9688
# Documentation
9789
98-
## - Components -
99-
10090
### -Navigation
10191
_Grabs content from a URL and replaces it on the current page (along with browser history button handling, onload/unload handlers, and much more_
10292
@@ -133,7 +123,7 @@ initHistoryHandlers| |self|sets event listeners to handle back/forward navigatio
133123
134124
##### To use:
135125
```javascript
136-
import {navigation} from '@htmlguyllc/jpack/es/components';
126+
import {navigation} from '@htmlguyllc/jpack/es/navigation';
137127
138128
//handles browser back/forward buttons
139129
navigation.initHistoryHandlers();
@@ -198,7 +188,7 @@ navigation.triggerOnLoad(dom.getElement('body'), 'body', navigation.getRouteFrom
198188
//now use the plugin to load pages
199189
//if you're lazy, the fastest way to integrate is to just add data-href to all internal links
200190
//and then attach a handler like this:
201-
import {events} from '@htmlguyllc/jpack/es/utilities';
191+
import {events} from '@htmlguyllc/jpack/es/events';
202192
events.onClick('[data-href]', function(){
203193
navigation.load(this.href);
204194
});
@@ -255,7 +245,7 @@ validate|form:Element|bool|passes the form to the validate callback and returns
255245
256246
##### To use:
257247
```javascript
258-
import {XHRForm} from '@htmlguyllc/jpack/es/components';
248+
import {XHRForm} from '@htmlguyllc/jpack/es/forms';
259249
260250
//shown with defaults
261251
var remote_form = new XHRForm(document.getElementById('my-form'), {
@@ -305,7 +295,7 @@ __There are several methods and properties inherited from XHRForm that are not l
305295
306296
##### To use:
307297
```javascript
308-
import {FormFromURL} from '@htmlguyllc/jpack/es/components';
298+
import {FormFromURL} from '@htmlguyllc/jpack/es/forms';
309299
310300
//shown with defaults
311301
var remote_form = new FormFromURL('/my-form', {
@@ -342,8 +332,6 @@ FormFromURL extends XHRForm and either can be extended as you need.
342332
343333
See examples/FormModalFromURL for an example
344334
345-
## - Objects -
346-
347335
### -Request
348336
_Provides a wrapper for window.location and query string access_
349337
@@ -360,7 +348,7 @@ appendSlash|string:string|string|adds a slash (if there isn't already one) to th
360348
361349
##### To use:
362350
```javascript
363-
import {request} from '@htmlguyllc/jpack/es/objects';
351+
import {request} from '@htmlguyllc/jpack/es/request';
364352
365353
//get product_id from the querystring
366354
var product_id = request.query.get('product_id');
@@ -413,7 +401,7 @@ The harder way:
413401
414402
Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.
415403
```javascript
416-
import {Site} from '@htmlguyllc/jpack/es/objects';
404+
import {Site} from '@htmlguyllc/jpack/es/site';
417405
418406
//this example uses jQuery's shorthand AJAX call, you can use axios or any request you want
419407
$.get('/my-site-info-endpoint.php', function(data){
@@ -490,7 +478,7 @@ The harder way:
490478
491479
Perform an XHR request to grab site details via a JSON API, then run the populate method on the site object.
492480
```javascript
493-
import {User} from '@htmlguyllc/jpack/es/objects';
481+
import {User} from '@htmlguyllc/jpack/es/user';
494482
495483
$.get('/my-user-info-endpoint.php', function(data){
496484
//don't forget error handling!
@@ -500,11 +488,6 @@ $.get('/my-user-info-endpoint.php', function(data){
500488
501489
Of course you can use this class for any User not just the current one, but that's the intended usage.
502490
503-
## - Plugin Wrappers -
504-
None yet.
505-
506-
## - Utilities -
507-
508491
### -Strings
509492
_Common string manipulations_
510493
@@ -517,7 +500,7 @@ setter|string:string|string|creates a setter method name from a string
517500
##### To Use:
518501
519502
```javascript
520-
import {strings} from '@htmlguyllc/jpack/es/utilities';
503+
import {strings} from '@htmlguyllc/jpack/es/strings';
521504
522505
strings.ucfirst('bob'); //returns 'Bob'
523506
strings.getter('name'); //returns 'getName';
@@ -540,7 +523,7 @@ multipleExist|el:mixed|bool|checks to see if more than 1 instance exists in the
540523
##### To Use:
541524
542525
```javascript
543-
import {dom} from '@htmlguyllc/jpack/es/utilities';
526+
import {dom} from '@htmlguyllc/jpack/es/dom';
544527
545528
//Dont do this. Most of these are dumb examples.
546529
dom.getElement('.my-fav-button', true, true); //will throw an error if it doesn't find it, or if it finds more than 1
@@ -565,7 +548,7 @@ isDataObject|value:object, keys:array, require_all_keys:bool, block_other_keys:b
565548
##### To Use:
566549
567550
```javascript
568-
import {type_checks} from '@htmlguyllc/jpack/es/utilities';
551+
import {type_checks} from '@htmlguyllc/jpack/es/type_checks';
569552
570553
var my_obj = {id:null, name:'John Doe', email:'john@doe.com'};
571554
@@ -597,7 +580,7 @@ trigger|el:mixed, event:string, event_options:mixed|array|triggers an event on a
597580
##### To Use:
598581
599582
```javascript
600-
import {events} from '@htmlguyllc/jpack/es/utilities';
583+
import {events} from '@htmlguyllc/jpack/es/events';
601584
602585
events.onClick('a.my-link', function(){
603586
//do something without the page redirecting to the href

dist/jpack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {type_checks, strings} from "../utilities";
1+
import {type_checks} from "./type_checks";
2+
import {strings} from "./strings";
23

3-
export class AbstractObject{
4+
export class AbstractClass{
45
constructor(){
56
}
67

es/components/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/**
22
* HTML DOM helpers
3-
*
4-
* @type {{getElements: (function(*=, *=): Array), getElement: dom.getElement, exists: (function(*=): number), multipleExist: (function(*=): number)}}
53
*/
64
export const dom = {
75
/**
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import {dom} from "../dom";
22

33
/**
4-
* Shorthand/consistent event listener management
5-
*
6-
* @type {{onClick: (function(*=, *=): (*|Array|el)), onChange: (function(*=, *=): (*|*|*[])), offChange: (function(*=, *=): (*|*|*[])), offEventPreventDefault: events.offEventPreventDefault, offClick(*=, *=): (Array|el), onSubmit: (function(*=, *=): (*|Array|el)), offSubmit: (function(*=, *=): (*|Array|el)), onEventPreventDefault: events.onEventPreventDefault, trigger: events.trigger, off: events.off, on: events.on}}
4+
* Shorthand preventDefault events (and others for consistency)
75
*/
86
export const events = {
97

0 commit comments

Comments
 (0)