Code Monkey home page Code Monkey logo

carpet.js's Introduction

Carpet.js Icon Carpet.js

Greenkeeper badge

project icon: Rug by Factorio.us collective from The Noun Project

Master: Build Status Develop: Build Status

Features

  • Lightweight: Minified: 2.2K, Minified & Gzipped: < 1K
  • Easy and Module based
  • Easily extendable by components

Used on big-scale projects as:

Browser Support (tested)

  • IE 9.0 (Windows 7)
  • IE 10.0 (Windows 7)
  • IE 11.0 (Windows 7)
  • Firefox 41.0 (Windows 8)
  • Firefox 41.0 (Mac OS X 10.11)
  • Safari 8.1 (Mac OS X 10.11)
  • Chrome 45.0 (Windows 7)
  • Chrome 45.0 (Mac OS X 10.10.0)

Framework Usage

Installation

bower install carpet

or download the compressed file from here

Minimal application

Minimal application will not do anything until you define some modules.

Example:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Carpet.js - Sample usage</title>
  </head>
  <body>
    <!-- your code here -->
    <script src="bower_components/carpet/dist/carpet.min.js"></script>

    <script>
      Carpet.init(); // Initialises the framework
    </script>
  </body>
</html>

Modules

Carpet.js is about modules so you have to define one before you start. Modules are defined very easily:

In html file:

<div data-module="myFirstModule"></div>

In JS file (before Carpet.init())

Carpet.module('myFirstModule', function (exports, settings, context) {
  'use strict';

  exports.init = function () {
    console.log('I will show when a data-module="myFirstModule" will be found in DOM');
  };
});

Carpet.module() takes two parameters:

  • name of the module (string, it should fit the name put in data-module attribute)
  • module body (function which will contain all the module body)

Your module body will be called with three parameters: exports, settings and context.

  • exports - object which will be 'public', you can extend it with a function or properties. exports.init is reserved for the initial function which will be called immediately after module init. If you do not provide exports.init the module will stay in the memory but have to be initialized in another way (e.g. PubSub)
  • settings - object with module settings. Settings can be passed by the DOM attribute as following:
data-settings="{'key' : 'value', sample: [1, 3, 5]}"
  • context - is a HTML DOM context which should be used as scope for all DOM finding actions, e.g. using jQuery:
$('a', context).anything(); // or
$(context).find('.my-pro-element').on('click', anotherFunction);

Additional informations

In module body you can also access its this context where you can find some additional data:

this.name       // - (string) containing the name of the module
this.moduleBody // - (function) body of the module
this.settings   // - (object) settings object
this.methods    // - (object) all public methods - same as 'exports'

Logging to console

Carpet.js comes with bunch of console.log extensions which will help you to organise your console output.

Sample logs:

Carpet:log ["Module: navigation has been loaded to memory"]
Carpet:info ["Module: navigation has been autoinited"]

Enable logging

Logging is disabled by default. You can easily enable it by just setting the Carpet.loggingEnabled to true. Remember the property must be set before Carpet.init().

Carpet.loggingEnabled = true;

Use available logging methods

Carpet.log('log me'); // with log level
Carpet.warn('something wrong!'); // with warn level
Carpet.error('Application abort!!'); // with error level
Carpet.info('I am awesome'); // with info level

Logs can take any type and any amount of parameters

Carpet.warn('log me', {}, [], 1, -1, Infinity, function(){} /* any other type here */); // with log level

Clearing the output

To clear the output just call Carpet.clearConsole()

For more information you can check the API documentation

Writing your own components

Components are separated logic in Carpet.js. They can be loaded inside the modules. Before first load they stay in the memory - not executed.

You can use them as helpers or any other kind of separated logic.

Writing a component is very easy:

Carpet.registerComponent('componentName', function () {

  Carpet.log(this); // => { name: 'componentName', componentBody: function...}

  return {
    componentMethod : function () {
      return 1;
    },

    componentProperty : [1, 5, 7],

    anythingYouWant : {}
  };
});

Component can return any type. Object with methods or a simple function are the preferred pattern.

Component is inactive until the first 'require'. You can get the component in two ways:

// Globally (from anywhere)
Carpet.getComponent('componentName'); // => { componentMethod: function...}

// Locally in the module (preferred)
Carpet.module('myModule', function (exports, settings, context) {
  var mySweetComponent = this.component('componentName');

  Carpet.log(mySweetComponent);
  // =>
  // {
  //  componentMethod : function () { return 1; },
  //  componentProperty : [1, 5, 7],
  //  anythingYouWant : {}
  // }
});

Components have to be loaded before modules, so the structure looks like this:

  <!-- Carpet.js -->
  <script src="carpet.min.js"></script>

  <!-- Components -->
  <script src="components/myComponent.js"></script> <!-- HERE -->

  <!-- Modules -->
  <script src="modules/myModule.js"></script>

  <!-- Application init -->
  <script>Carpet.init();</script>

Carpet.js comes with some components included, but they are not added in the core library by default. You must add them to your html file with a script tag.

Contribution

Setup for development

npm install
grunt dist      # Generates dist files with full testing and linting
# or
grunt dist-dev  # Generates dist files without checking the code

Generating JSDoc

grunt docs

Running unit tests

npm test

Branching model and submitting changes

Carpet.js is using the great Git Flow. Please follow installation instruction for your distribution clicking here.

After you install GitFlow on your machine run this command in the forked Carpet.js repository:

git flow init

Now you should be ready to start working on a feature. Feature naming pattern should fit the following:

mg-short-description
^      ^-- Short description separated by dashes (-)
^--- Your initials

Examples:

mg-component-pubsub
ms-bug-21-wrong-casting # bug or others can be followed by GitHub issue number

To start working on a feature you will have to create a feature branch.

git flow feature start mg-component-pubsub

Then you can easily work on your branch, commit and git push your changes. After you finish your functionality and all tests are passing correctly (locally and by Travis CI) you can submit a Pull Request.

If the Pull request has been merged correctly you can just finish the branch by:

git flow feature finish mg-component-pubsub

Thats all, your feature will be released in next version!

Writing build-in components

Writing Carpet.js components don't differ from normal component registration pattern but requires documentation and full test coverage.

As a sample you can take a look at the advice component and its tests:

/**
 * @module advice
 */
Carpet.registerComponent('advice', function () {
  'use strict';

  return {
    /**
     * Wrapes function around
     *
     * @param  {Function} base    Function to wrap on
     * @param  {Function} wrapped Wrapping function
     * @return {Function}         Wrapped function body
     */
    around: function (base, wrapped) {
      return function composedAround() {
        // unpacking arguments by hand is faster
        var i = 0;
        var l = arguments.length;
        var args = new Array(l + 1);

        args[0] = base.bind(this);
        for (; i < l; i++) {
          args[i + 1] = arguments[i];
        }
        return wrapped.apply(this, args);
      };
    },
    // etc..
    // [...]
  };
});

Feel free to contribute or add issues and questions

carpet.js's People

Contributors

mateuszgachowski avatar greenkeeper[bot] avatar

Stargazers

 avatar Jason Davis avatar Hector Romo avatar Arif Çakıroğlu avatar Rey F. Diaz avatar Abdussamet Koçak avatar  avatar Stepan Perlov avatar Peter Craig avatar Adam Cupiał avatar  avatar Lauren Ancona avatar peengle avatar Wojciech Wójcik avatar Slawomir Dolzycki-Uchto avatar Krzysztof Magosa avatar Jan avatar Maciej Gierok avatar  avatar Maciej Smolinski avatar

Watchers

 avatar James Cloos avatar Artur Omazda avatar

carpet.js's Issues

Prepare some examples of use

Examples of use of Carpet.js:

  • Minimal application
  • Form Module (with validation)
  • Passing settings to the module
  • Registering custom components
  • Carpet.js + jQuery: how to use the jQuery context
  • Carpet.js + Bootstrap: samples
  • Modules with no 'auto init' - how to make use of PubSub or event-triggering
  • Connecting modules and their behaviour with each other
  • Using advice to extend framework functionality

For now examples can be stored in the repo, in the future we can create a separate repo for them

Prepare more external components

There is a need to prepare more external components for Carpet.js

To name some:

  • Own lightweight PubSub (current one requires external library)
  • Local storage / session storage management
  • and others

An in-range update of jasmine-core is breaking the build 🚨

Version 2.6.4 of jasmine-core just got published.

Branch Build failing 🚨
Dependency jasmine-core
Current Version 2.6.3
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As jasmine-core is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details

Release Notes 2.6.4

Please see the release notes

Commits

The new version differs by 4 commits.

  • 02c18a3 Bump version to 2.6.4
  • 0c6397d Don't setTimeout() every time the stack is cleared via MessageChannel()
  • eb46714 Don't use window
  • b38decf Break into a setTimeout every once in a while

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of grunt-jsdoc is breaking the build 🚨

The devDependency grunt-jsdoc was updated from 2.3.1 to 2.4.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

grunt-jsdoc is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for Version 2.4.0
  • Update to jsdoc 3.6.0 (security update)
  • Increase supported node versions
Commits

The new version differs by 6 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of grunt is breaking the build 🚨

The devDependency grunt was updated from 1.0.3 to 1.0.4.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

grunt is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 5 commits.

  • 8fcbed1 v1.0.4 (#1668)
  • 0b13970 Update js-yaml to address https://npmjs.com/advisories/788 (#1667)
  • 7db6cf7 Use SOURCE_DATE_EPOCH to render dates in template. (#1596)
  • a2d6d80 Revert "Indicate in package.json that Node.js 4 or newer is required (#1643)" (#1644)
  • 773b7e7 Indicate in package.json that Node.js 4 or newer is required (#1643)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Multiple modules per element

Why don't you allow multiple modules per element: data-module="module1 module2". Implementation should be easy and sometimes it's better to categorize modules by some common shared logic, not by element itself (eg. modules having tab navigation, modules having "load more" pagination, etc).

Pseudo-code:

 moduleNames = domModule.getAttribute('data-module').split(" ");
 for each (moduleNames....

An in-range update of karma-browserstack-launcher is breaking the build 🚨

The devDependency karma-browserstack-launcher was updated from 1.3.0 to 1.4.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

karma-browserstack-launcher is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 15 commits.

  • 26496c0 Merge pull request #134 from LinusU/patch-1
  • 6f5588b Merge pull request #138 from XhmikosR/patch-1
  • f184ba4 chore: release v1.4.0
  • 3ab9494 chore: update contributors
  • 99eafa0 Merge pull request #137 from johnjbarton/revert_1_4_0
  • 72d5115 Update package.json
  • e886587 Revert "chore: update contributors"
  • 921c0e4 Revert "chore: release v1.4.0"
  • 0f69bec chore: release v1.4.0
  • c5bbad1 chore: update contributors
  • 50556b6 Catch errors thrown by client.createWorker
  • 310c228 fix(deps: upgrade browserstack to v1.5.1 which patches a high risk vulnerability of https-proxy-agent dependency
  • 62fc997 feat: allow additional browserstack capabitilies to be configured verbatim
  • b6776f4 feat: add support for the enableLoggingForApi property
  • bef7167 chore: add a Git .mailmap with my new name (#124)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Error when importing Carpert.js from npm

Hi Mateusz,
Installing carpet.js from npm (not using bower) and including it using ES2015 import statement:

import Carpet from 'carpet.js';

Carpet.registerComponent('componentName', function () {
  // ...
});

produces error:

ERROR in ./~/carpet.js/Gruntfile.js
Module not found: Error: Cannot resolve module 'load-grunt-tasks' in /node_modules/carpet.js
 @ ./~/carpet.js/Gruntfile.js 110:2-29

It seems that load-grunt-tasks is not installed during installing library from npm.

In package.json
changing "main": "Gruntfile.js"
to "main": "dist/carpet.js"
seemed to solve the issue.

Thanks,
Darek.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.