All files / components/undo/js Undo.js

100% Statements 51/51
71.43% Branches 10/14
100% Functions 8/8
100% Lines 51/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                              3x   3x     3x     3x 3x 3x   3x 21x 21x       21x         21x 21x     3x 48x 21x 21x 27x 18x 18x 9x 9x 9x       3x 9x 9x 9x 9x 9x 9x   9x     3x 3x 3x 3x 3x 3x   3x     3x 27x 15x 15x 15x       3x 21x 21x     3x 21x 21x                     3x                                                                                                                                       3x                     3x          
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2010 University of Toronto
Copyright 2010 Lucendo Development Ltd.
Copyright 2013 Raising the Floor - US
Copyright 2015 Raising the Floor - International
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    fluid.registerNamespace("fluid.undo");
 
    // The three states of the undo component
    fluid.undo.STATE_INITIAL = "state_initial";
    fluid.undo.STATE_CHANGED = "state_changed";
    fluid.undo.STATE_REVERTED = "state_reverted";
 
    fluid.undo.defaultRenderer = function (that, targetContainer) {
        var str = that.options.strings;
        var markup = "<span class='flc-undo'>" +
            "<a href='#' class='flc-undo-undoControl'>" + str.undo + "</a>" +
            "<a href='#' class='flc-undo-redoControl'>" + str.redo + "</a>" +
            "</span>";
        var markupNode = $(markup).attr({
            "role": "region",
            "aria-live": "polite",
            "aria-relevant": "all"
        });
        targetContainer.append(markupNode);
        return markupNode;
    };
 
    fluid.undo.refreshView = function (that) {
        if (that.state === fluid.undo.STATE_INITIAL) {
            that.locate("undoContainer").hide();
            that.locate("redoContainer").hide();
        } else if (that.state === fluid.undo.STATE_CHANGED) {
            that.locate("undoContainer").show();
            that.locate("redoContainer").hide();
        } else Eif (that.state === fluid.undo.STATE_REVERTED) {
            that.locate("undoContainer").hide();
            that.locate("redoContainer").show();
        }
    };
 
    fluid.undo.undoControlClick = function (that) {
        Eif (that.state !== fluid.undo.STATE_REVERTED) {
            fluid.model.copyModel(that.extremalModel, that.component.model);
            that.component.updateModel(that.initialModel, that);
            that.state = fluid.undo.STATE_REVERTED;
            fluid.undo.refreshView(that);
            that.locate("redoControl").focus();
        }
        return false;
    };
 
    fluid.undo.redoControlClick = function (that) {
        Eif (that.state !== fluid.undo.STATE_CHANGED) {
            that.component.updateModel(that.extremalModel, that);
            that.state = fluid.undo.STATE_CHANGED;
            fluid.undo.refreshView(that);
            that.locate("undoControl").focus();
        }
        return false;
    };
 
    fluid.undo.modelChanged = function (that, newModel, oldModel, source) {
        if (source !== that) {
            that.state = fluid.undo.STATE_CHANGED;
            fluid.model.copyModel(that.initialModel, oldModel);
            fluid.undo.refreshView(that);
        }
    };
 
    fluid.undo.copyInitialModel = function (that) {
        fluid.model.copyModel(that.initialModel, that.component.model);
        fluid.model.copyModel(that.extremalModel, that.component.model);
    };
 
    fluid.undo.setTabindex = function (that) {
        fluid.tabindex(that.locate("undoControl"), 0);
        fluid.tabindex(that.locate("redoControl"), 0);
    };
 
    /**
     * Decorates a target component with the function of "undoability". This component is intended to be attached as a
     * subcomponent to the target component, which will bear a grade of "fluid.undoable"
     *
     * @param component {Object} a "model-bearing" standard Fluid component to receive the "undo" functionality
     * @param options {Object} a collection of options settings
     */
 
    fluid.defaults("fluid.undo", {
        gradeNames: ["fluid.component"],
        members: {
            state: fluid.undo.STATE_INITIAL,
            initialModel: {},
            extremalModel: {},
            component: "{fluid.undoable}",
            container: {
                expander: {
                    func: "{that}.options.renderer",
                    args: ["{that}", "{that}.component.container"]
                }
            },
            dom: {
                expander: {
                    funcName: "fluid.initDomBinder",
                    args: ["{that}", "{that}.options.selectors"]
                }
            }
        },
        invokers: {
            undoControlClick: {
                funcName: "fluid.undo.undoControlClick",
                args: "{that}"
            },
            redoControlClick: {
                funcName: "fluid.undo.redoControlClick",
                args: "{that}"
            }
        },
        listeners: {
            "onCreate.copyInitialModel": {
                funcName: "fluid.undo.copyInitialModel",
                priority: "before:refreshView"
            },
            "onCreate.setTabindex": "fluid.undo.setTabindex",
            "onCreate.refreshView": "fluid.undo.refreshView",
            "onCreate.bindUndoClick": {
                "this": "{that}.dom.undoControl",
                method: "click",
                args: "{that}.undoControlClick"
            },
            "onCreate.bindRedoClick": {
                "this": "{that}.dom.redoControl",
                method: "click",
                args: "{that}.redoControlClick"
            },
            "{fluid.undoable}.events.modelChanged": {
                funcName: "fluid.undo.modelChanged",
                args: ["{that}", "{arguments}.0", "{arguments}.1", "{arguments}.2"]
            }
        },
        selectors: {
            undoContainer: ".flc-undo-undoControl",
            undoControl: ".flc-undo-undoControl",
            redoContainer: ".flc-undo-redoControl",
            redoControl: ".flc-undo-redoControl"
        },
 
        strings: {
            undo: "undo edit",
            redo: "redo edit"
        },
 
        renderer: fluid.undo.defaultRenderer
    });
 
    // An uninstantiable grade expressing the contract of the "fluid.undoable" grade
    fluid.defaults("fluid.undoable", {
        gradeNames: ["fluid.modelComponent"],
        invokers: {
            updateModel: {} // will be implemented by concrete grades
        },
        events: {
            modelChanged: null
        }
    });
 
    // Backward compatibility for users of Infusion 1.4.x API
    fluid.defaults("fluid.undoDecorator", {
        gradeNames: ["fluid.undo"]
    });
 
})(jQuery, fluid_3_0_0);