targets.js 8.09 KB
Newer Older
1
(function (requirejs, require, define) {
2
define([], function () {
3 4 5 6 7
    return {
        'initializeBaseTargets': initializeBaseTargets,
        'initializeTargetField': initializeTargetField,
        'destroyTargetField': destroyTargetField
    };
8

9
    function initializeBaseTargets(state) {
10 11 12
        (function (c1) {
            while (c1 < state.config.targets.length) {
                processTarget(state, state.config.targets[c1]);
13

14 15 16
                c1 += 1;
            }
        }(0));
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
    function initializeTargetField(draggableObj) {
        var iconElOffset;

        if (draggableObj.targetField.length === 0) {
            draggableObj.originalConfigObj.target_fields.every(function (targetObj) {
                processTarget(draggableObj.state, targetObj, true, draggableObj);

                return true;
            });
        } else {
            iconElOffset = draggableObj.iconEl.position();

            draggableObj.targetField.every(function (targetObj) {
                targetObj.offset.top = iconElOffset.top + targetObj.y;
                targetObj.offset.left = iconElOffset.left + targetObj.x;

                return true;
            });
        }
    }

    function destroyTargetField(draggableObj) {
        var indexOffset, lowestRemovedIndex;

        indexOffset = 0;
        lowestRemovedIndex = draggableObj.state.targets.length + 1;

        draggableObj.targetField.every(function (target) {
            target.el.remove();

            if (lowestRemovedIndex > target.indexInStateArray) {
                lowestRemovedIndex = target.indexInStateArray;
            }

            draggableObj.state.targets.splice(target.indexInStateArray - indexOffset, 1);
            indexOffset += 1;

            return true;
        });

        draggableObj.state.targets.every(function (target) {
            if (target.indexInStateArray > lowestRemovedIndex) {
                target.indexInStateArray -= indexOffset;
            }

            return true;
        });

        draggableObj.targetField = [];
    }

    function processTarget(state, obj, fromTargetField, draggableObj) {
71
        var targetEl, borderCss, numTextEl, targetObj;
72

73 74 75 76
        borderCss = '';
        if (state.config.targetOutline === true) {
            borderCss = 'border: 1px dashed gray; ';
        }
77

78 79 80 81 82 83 84 85 86 87 88
        targetEl = $(
            '<div ' +
                'style=" ' +
                    'display: block; ' +
                    'position: absolute; ' +
                    'width: ' + obj.w + 'px; ' +
                    'height: ' + obj.h + 'px; ' +
                    'top: ' + obj.y + 'px; ' +
                    'left: ' + obj.x + 'px; ' +
                    borderCss +
                '" ' +
89
            'aria-dropeffect=""></div>'
90
        );
91 92 93 94 95 96
        if (fromTargetField === true) {
            targetEl.appendTo(draggableObj.iconEl);
        } else {
            targetEl.appendTo(state.baseImageEl.parent());
        }

97 98 99 100 101 102
        targetEl.mousedown(function (event) {
            event.preventDefault();
        });

        if (state.config.onePerTarget === false) {
            numTextEl = $(
103 104 105 106
                '<div ' +
                    'style=" ' +
                        'display: block; ' +
                        'position: absolute; ' +
107 108
                        'width: 24px; ' +
                        'height: 24px; ' +
109
                        'top: ' + obj.y + 'px; ' +
110 111 112 113 114 115 116
                        'left: ' + (obj.x + obj.w - 24) + 'px; ' +
                        'border: 1px solid black; ' +
                        'text-align: center; ' +
                        'z-index: 500; ' +
                        'background-color: white; ' +
                        'font-size: 0.95em; ' +
                        'color: #009fe2; ' +
117
                    '" ' +
118
                '>0</div>'
119
            );
120 121 122
        } else {
            numTextEl = null;
        }
123

124
        targetObj = {
125 126
            'uniqueId': state.getUniqueId(),

127
            'id': obj.id,
128

129 130 131
            'x': obj.x,
            'y': obj.y,

132 133
            'w': obj.w,
            'h': obj.h,
134

135 136
            'el': targetEl,
            'offset': targetEl.position(),
137

138
            'draggableList': [],
139

140 141
            'state': state,

142
            'targetEl': targetEl,
143

144 145
            'numTextEl': numTextEl,
            'updateNumTextEl': updateNumTextEl,
146

147
            'removeDraggable': removeDraggable,
148 149 150 151
            'addDraggable': addDraggable,

            'type': 'base',
            'draggableObj': null
152
        };
153

154 155 156 157 158 159 160 161 162
        if (fromTargetField === true) {
            targetObj.offset = draggableObj.iconEl.position();
            targetObj.offset.top += obj.y;
            targetObj.offset.left += obj.x;

            targetObj.type = 'on_drag';
            targetObj.draggableObj = draggableObj;
        }

163 164 165 166 167 168 169 170
        if (state.config.onePerTarget === false) {
            numTextEl.appendTo(state.baseImageEl.parent());
            numTextEl.mousedown(function (event) {
                event.preventDefault();
            });
            numTextEl.mouseup(function () {
                cycleDraggableOrder.call(targetObj)
            });
171
        }
172

173 174 175 176 177
        targetObj.indexInStateArray = state.targets.push(targetObj) - 1;

        if (fromTargetField === true) {
            draggableObj.targetField.push(targetObj);
        }
178
    }
179

180
    function removeDraggable(draggable) {
181 182
        var c1;

183
        this.draggableList.splice(draggable.onTargetIndex, 1);
184

185 186 187 188 189 190 191 192 193 194 195
        // An item from the array was removed. We need to updated all indexes accordingly.
        // Shift all indexes down by one if they are higher than the index of the removed item.
        c1 = 0;
        while (c1 < this.draggableList.length) {
            if (this.draggableList[c1].onTargetIndex > draggable.onTargetIndex) {
                this.draggableList[c1].onTargetIndex -= 1;
            }

            c1 += 1;
        }

196 197
        draggable.onTarget = null;
        draggable.onTargetIndex = null;
198

199 200 201 202
        if (this.type === 'on_drag') {
            this.draggableObj.numDraggablesOnMe -= 1;
        }

203 204
        this.updateNumTextEl();
    }
205

206 207 208
    function addDraggable(draggable) {
        draggable.onTarget = this;
        draggable.onTargetIndex = this.draggableList.push(draggable) - 1;
209

210 211 212 213
        if (this.type === 'on_drag') {
            this.draggableObj.numDraggablesOnMe += 1;
        }

214 215
        this.updateNumTextEl();
    }
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    /*
     * function cycleDraggableOrder
     *
     * Parameters:
     *     none - This function does not expect any parameters.
     *
     * Returns:
     *     undefined - The return value of this function is not used.
     *
     * Description:
     *     Go through all draggables that are on the current target, and decrease their
     *     z-index by 1, making sure that the bottom-most draggable ends up on the top.
     */
    function cycleDraggableOrder() {
        var c1, lowestZIndex, highestZIndex;

233
        if (this.draggableList.length < 2) {
234 235 236 237 238 239 240 241 242
            return;
        }

        highestZIndex = -10000;
        lowestZIndex = 10000;

        for (c1 = 0; c1 < this.draggableList.length; c1 += 1) {
            if (this.draggableList[c1].zIndex < lowestZIndex) {
                lowestZIndex = this.draggableList[c1].zIndex;
243 244
            }

245 246
            if (this.draggableList[c1].zIndex > highestZIndex) {
                highestZIndex = this.draggableList[c1].zIndex;
247 248
            }
        }
249 250 251 252 253 254 255 256 257 258 259 260 261 262

        for (c1 = 0; c1 < this.draggableList.length; c1 += 1) {
            if (this.draggableList[c1].zIndex === lowestZIndex) {
                this.draggableList[c1].zIndex = highestZIndex;
            } else {
                this.draggableList[c1].zIndex -= 1;
            }

            this.draggableList[c1].iconEl.css('z-index', this.draggableList[c1].zIndex);
            if (this.draggableList[c1].labelEl !== null) {
                this.draggableList[c1].labelEl.css('z-index', this.draggableList[c1].zIndex);
            }
        }
    }
263 264

    function updateNumTextEl() {
265 266 267
        if (this.numTextEl !== null) {
            this.numTextEl.html(this.draggableList.length);
        }
268
    }
269
}); // End-of: define([], function () {
270
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {