All files / components/reorderer/js ImageReorderer.js

89.8% Statements 44/49
75% Branches 12/16
78.57% Functions 11/14
89.8% Lines 44/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              1x   1x     1x   1x 87x     1x                         1x 165x 165x 165x 6552x 6552x 6552x 2310x     165x     1x 23x 23x   23x   23x   101x       1x 23x 46x       1x 64x         1x 23x 23x 322x 322x 322x                         1x 23x 23x   23x   41x   41x 574x     41x 41x 41x                             1x                                             1x        
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010-2011 Lucendo Development Ltd.
Copyright 2011 OCAD University
Copyright 2013 Raising the Floor - US
 
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.reorderImages");
 
    fluid.reorderImages.deriveLightboxCellBase = function (namebase, index) {
        return namebase + "lightbox-cell:" + index + ":";
    };
 
    fluid.reorderImages.addThumbnailActivateHandler = function (container) {
        var enterKeyHandler = function (evt) {
            if (evt.which === fluid.reorderer.keys.ENTER) {
                var thumbnailAnchors = $("a", evt.target);
                document.location = thumbnailAnchors.attr("href");
            }
        };
 
        container.keypress(enterKeyHandler);
    };
 
    // Custom query method seeks all tags descended from a given root with a
    // particular tag name, whose id matches a regex.
    fluid.reorderImages.seekNodesById = function (rootnode, tagname, idmatch) {
        var inputs = rootnode.getElementsByTagName(tagname);
        var togo = [];
        for (var i = 0; i < inputs.length; i += 1) {
            var input = inputs[i];
            var id = input.id;
            if (id && id.match(idmatch)) {
                togo.push(input);
            }
        }
        return togo;
    };
 
    fluid.reorderImages.createImageCellFinder = function (parentNode, containerId) {
        containerId = containerId || parentNode.prop("id");
        parentNode = fluid.unwrap(parentNode);
 
        var lightboxCellNamePattern = "^" + fluid.reorderImages.deriveLightboxCellBase(containerId, "[0-9]+") + "$";
 
        return function () {
            // This orderable finder assumes that the lightbox thumbnails are 'div' elements
            return fluid.reorderImages.seekNodesById(parentNode, "div", lightboxCellNamePattern);
        };
    };
 
    fluid.reorderImages.seekForm = function (container) {
        return fluid.findAncestor(container, function (element) {
            return $(element).is("form");
        });
    };
 
    fluid.reorderImages.seekInputs = function (container, reorderform) {
        return fluid.reorderImages.seekNodesById(reorderform,
                             "input",
                             "^" + fluid.reorderImages.deriveLightboxCellBase(container.prop("id"), "[^:]*") + "reorder-index$");
    };
 
    fluid.reorderImages.mapIdsToNames = function (container, reorderform) {
        var inputs = fluid.reorderImages.seekInputs(container, reorderform);
        for (var i = 0; i < inputs.length; i++) {
            var input = inputs[i];
            var name = input.name;
            input.name = name || input.id;
        }
    };
 
    /**
     * Returns a default afterMove listener using the id-based, form-driven scheme for communicating with the server.
     * It is implemented by nesting hidden form fields inside each thumbnail container. The value of these form elements
     * represent the order for each image. This default listener submits the form's default
     * action via AJAX.
     *
     * @param {jQueryable} container - The Image Reorderer's container element.
     * @return {Function} - A function which can be used as a listener for the afterMove event.
     */
    fluid.reorderImages.createIDAfterMoveListener = function (container) {
        var reorderform = fluid.reorderImages.seekForm(container);
        fluid.reorderImages.mapIdsToNames(container, reorderform);
 
        return function () {
            var inputs, i;
            inputs = fluid.reorderImages.seekInputs(container, reorderform);
 
            for (i = 0; i < inputs.length; i += 1) {
                inputs[i].value = i;
            }
 
            Eif (reorderform && reorderform.action) {
                var order = $(reorderform).serialize();
                $.post(reorderform.action,
                       order,
                       function () { /* No-op response */ });
            }
        };
    };
 
    // Public Lightbox API
    /**
     * Creates a new Lightbox instance from the specified parameters, providing full control over how
     * the Lightbox is configured.
     *
     * @param container {Object}
     * @param options {Object}
     */
    fluid.defaults("fluid.reorderImages", {
        gradeNames: ["fluid.reorderer"],
        layoutHandler: "fluid.gridLayoutHandler",
        listeners: {
            "afterMove.postModel": {
                expander: {
                    funcName: "fluid.reorderImages.createIDAfterMoveListener",
                    args: "{that}.container"
                }
            }
        },
        selectors: {
            movables: {
                expander: {
                    funcName: "fluid.reorderImages.createImageCellFinder",
                    args: "{that}.container"
                }
            },
            labelSource: ".flc-reorderer-imageTitle"
        }
    });
 
    // This function now deprecated. Please use fluid.reorderImages() instead.
    fluid.lightbox = fluid.reorderImages;
 
 
})(jQuery, fluid_3_0_0);