Code Monkey home page Code Monkey logo

clickago.js's Introduction

Clickago.js

Clickago.js is a lightweight framework for implementing Undo/Redo functionality on a web page.

Basic Demo

API

.register(actionOptions, rollbackOptions)

Use this method to register events.

Example:

var message, tracker;

tracker = new Clickago();

message = function (text) {
    alert(text);
};

tracker.register({
    method: message,
    thisArg: window,
    arguments: ["You're fired!"]
}, {
    method: message,
    thisArg: window,
    arguments: ["My bad. You can keep your job."]
});

message("You're fired!"); // Alerts "You're fired!"

.undo()

This method will get the newest rollbackOptions and evaluate it.

Example:

tracker.undo(); // Alerts "My bad. You can keep your job."

.redo()

This method will get the newest actionOptions and evaluate it.

Example:

tracker.redo(); // Alerts "You're fired!"

.canUndo / .canRedo

Boolean values indicating whether you can use .undo() and/or .redo().


.disable()

Disables .undo() and .redo() methods (you can still register new actions).

Example:

tracker.disable();

tracker.undo(); // returns undefined;

trakcer.canUndo; // false
tracker.canRedo; // false

.enable()

Enables .undo() and .redo().

Example:

tracker.enable();

tracker.undo(); // Alerts "My bad. You can keep your job."

Example

(function () {
    "use strict";

    var tracker, comments;

    // Initialise Clickago class.
    tracker = new Clickago();

    comments = [];

    // Inserting comments to the collection.
    function insertComment (comment) {
        comments[comments.length] = comment;
    };

    // Removing comments from the collection.
    function removeComment (index) {
        comments.splice(index, 1);
    };

    // Wraper function around `insertComment` which registers the events.
    function addComment (comment) {
        tracker.register({
            method: insertComment,
            thisArg: null,
            arguments: [comment]
        }, {
            method: removeComment,
            thisArg: null,
            arguments: [comments.length]
        });

        insertComment(comment);
    }

    // Wraper function around `insertComment` which registers the events.
    function deleteComment (index) {
        tracker.register({
            method: removeComment,
            thisArg: null,
            arguments: [index]
        }, {
            method: insertComment,
            thisArg: null,
            arguments: [comments[index]]
        });

        removeComment(index);
    };

    addComment("Comment 1");
    console.log(comments); // Logs ["Comment 1"]

    addComment("Comment 2");
    console.log(comments); // Logs ["Comment 1", "Comment 2"]

    addComment("Comment 3");
    console.log(comments); // Logs ["Comment 1", "Comment 2", "Comment 3"]

    deleteComment(2);
    console.log(comments); // Logs ["Comment 1", "Comment 2"]

    tracker.undo();
    console.log(comments); // Logs ["Comment 1", "Comment 2", "Comment 3"]

    tracker.redo();
    console.log(comments); // Logs ["Comment 1", "Comment 2"]

    tracker.undo();
    console.log(comments); // Logs ["Comment 1", "Comment 2", "Comment 3"]

    tracker.undo();
    console.log(comments); // Logs ["Comment 1", "Comment 2"]

    tracker.undo();
    console.log(comments); // Logs ["Comment 1"]

    tracker.undo();
    console.log(comments); // Logs []
})();

clickago.js's People

Contributors

adampoczatek avatar digiguru avatar

Watchers

 avatar  avatar  avatar

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.