draggable_logic.js 15.8 KB
Newer Older
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
(function(requirejs, require, define) {
    define(['js/capa/drag_and_drop/update_input', 'js/capa/drag_and_drop/targets'], function(updateInput, Targets) {
        return {
            'moveDraggableTo': function(moveType, target, funcCallback) {
                var self, offset;

                if (this.hasLoaded === false) {
                    self = this;

                    setTimeout(function() {
                        self.moveDraggableTo(moveType, target, funcCallback);
                    }, 50);

                    return;
                }

                if ((this.isReusable === true) && (this.isOriginal === true)) {
                    this.makeDraggableCopy(function(draggableCopy) {
                        draggableCopy.moveDraggableTo(moveType, target, funcCallback);
                    });

                    return;
                }

                offset = 0;
                if (this.state.config.targetOutline === true) {
                    offset = 1;
                }

                this.inContainer = false;

                if (this.isOriginal === true) {
                    this.containerEl.hide();
                    this.iconEl.detach();
                }

                if (this.iconImgEl !== null) {
                    this.iconImgEl.css({
                        'width': this.iconWidth,
                        'height': this.iconHeight
                    });
                }

                this.iconEl.css({
                    'background-color': this.iconElBGColor,
                    'padding-left': this.iconElPadding,
                    'padding-right': this.iconElPadding,
                    'border': this.iconElBorder,
                    'width': this.iconWidth,
                    'height': this.iconHeight
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
                if (moveType === 'target') {
                    this.iconEl.css({
                        'left': target.offset.left + 0.5 * target.w - this.iconWidth * 0.5 + offset - this.iconElLeftOffset,
                        'top': target.offset.top + 0.5 * target.h - this.iconHeight * 0.5 + offset
                    });
                } else {
                    this.iconEl.css({
                        'left': target.x - this.iconWidth * 0.5 + offset - this.iconElLeftOffset,
                        'top': target.y - this.iconHeight * 0.5 + offset
                    });
                }
                this.iconEl.appendTo(this.state.baseImageEl.parent());

                if (this.labelEl !== null) {
                    if (this.isOriginal === true) {
                        this.labelEl.detach();
                    }
                    this.labelEl.css({
                        'background-color': this.state.config.labelBgColor,
                        'padding-left': 8,
                        'padding-right': 8,
                        'border': '1px solid black'
                    });
                    if (moveType === 'target') {
                        this.labelEl.css({
                            'left': target.offset.left + 0.5 * target.w - this.labelWidth * 0.5 + offset - 9, // Account for padding, border.
                            'top': target.offset.top + 0.5 * target.h + this.iconHeight * 0.5 + 5 + offset
                        });
                    } else {
                        this.labelEl.css({
                            'left': target.x - this.labelWidth * 0.5 + offset - 9, // Account for padding, border.
                            'top': target.y - this.iconHeight * 0.5 + this.iconHeight + 5 + offset
                        });
                    }
                    this.labelEl.appendTo(this.state.baseImageEl.parent());
                }
88

89 90 91 92 93 94
                if (moveType === 'target') {
                    target.addDraggable(this);
                } else {
                    this.x = target.x;
                    this.y = target.y;
                }
95

96 97
                this.zIndex = 1000;
                this.correctZIndexes();
98

99
                Targets.initializeTargetField(this);
100

101 102 103 104
                if (this.isOriginal === true) {
                    this.state.numDraggablesInSlider -= 1;
                    this.state.updateArrowOpacity();
                }
105

106 107 108 109
                if ($.isFunction(funcCallback) === true) {
                    funcCallback();
                }
            },
110 111 112 113 114 115

    // At this point the mouse was realeased, and we need to check
    // where the draggable eneded up. Based on several things, we
    // will either move the draggable back to the slider, or update
    // the input with the user's answer (X-Y position of the draggable,
    // or the ID of the target where it landed.
116 117
            'checkLandingElement': function() {
                var positionIE;
118

119 120
                this.mousePressed = false;
                positionIE = this.iconEl.position();
121

122 123 124
                if (this.state.config.individualTargets === true) {
                    if (this.checkIfOnTarget(positionIE) === true) {
                        this.correctZIndexes();
125

126 127 128 129 130
                        Targets.initializeTargetField(this);
                    } else {
                        if (this.onTarget !== null) {
                            this.onTarget.removeDraggable(this);
                        }
131

132
                        this.moveBackToSlider();
133

134 135 136 137 138 139
                        if (this.isOriginal === true) {
                            this.state.numDraggablesInSlider += 1;
                        }
                    }
                } else {
                    if (
140 141 142 143 144
                (positionIE.left < 0) ||
                (positionIE.left + this.iconWidth > this.state.baseImageEl.width()) ||
                (positionIE.top < 0) ||
                (positionIE.top + this.iconHeight > this.state.baseImageEl.height())
            ) {
145
                        this.moveBackToSlider();
146

147 148
                        this.x = -1;
                        this.y = -1;
149

150 151 152 153 154
                        if (this.isOriginal === true) {
                            this.state.numDraggablesInSlider += 1;
                        }
                    } else {
                        this.correctZIndexes();
155

156 157
                        this.x = positionIE.left + this.iconWidth * 0.5;
                        this.y = positionIE.top + this.iconHeight * 0.5;
158

159 160 161
                        Targets.initializeTargetField(this);
                    }
                }
162

163 164 165 166 167
                if (this.isOriginal === true) {
                    this.state.updateArrowOpacity();
                }
                updateInput.update(this.state);
            },
168 169 170 171 172 173 174 175 176

    // Determine if a draggable, after it was relased, ends up on a
    // target. We do this by iterating over all of the targets, and
    // for each one we check whether the draggable's center is
    // within the target's dimensions.
    //
    // positionIE is the object as returned by
    //
    //     this.iconEl.position()
177 178
            'checkIfOnTarget': function(positionIE) {
                var c1, target;
179

180 181
                for (c1 = 0; c1 < this.state.targets.length; c1 += 1) {
                    target = this.state.targets[c1];
182 183 184 185 186

            // If only one draggable per target is allowed, and
            // the current target already has a draggable on it
            // (with an ID different from the one we are checking
            // against), then go to next target.
187
                    if (
188 189 190 191
                (this.state.config.onePerTarget === true) &&
                (target.draggableList.length === 1) &&
                (target.draggableList[0].uniqueId !== this.uniqueId)
            ) {
192 193
                        continue;
                    }
194 195 196

            // If the target is on a draggable (from target field), we must make sure that
            // this draggable is not the same as "this" one.
197 198 199
                    if ((target.type === 'on_drag') && (target.draggableObj.uniqueId === this.uniqueId)) {
                        continue;
                    }
200 201 202

            // Check if the draggable's center coordinate is within
            // the target's dimensions. If not, go to next target.
203
                    if (
204 205 206 207 208
                (positionIE.top + this.iconHeight * 0.5 < target.offset.top) ||
                (positionIE.top + this.iconHeight * 0.5 > target.offset.top + target.h) ||
                (positionIE.left + this.iconWidth * 0.5 < target.offset.left) ||
                (positionIE.left + this.iconWidth * 0.5 > target.offset.left + target.w)
            ) {
209 210
                        continue;
                    }
211 212 213 214 215

            // If the draggable was moved from one target to
            // another, then we need to remove it from the
            // previous target's draggables list, and add it to the
            // new target's draggables list.
216 217 218 219
                    if ((this.onTarget !== null) && (this.onTarget.uniqueId !== target.uniqueId)) {
                        this.onTarget.removeDraggable(this);
                        target.addDraggable(this);
                    }
220 221 222
            // If the draggable was moved from the slider to a
            // target, remember the target, and add ID to the
            // target's draggables list.
223 224 225
                    else if (this.onTarget === null) {
                        target.addDraggable(this);
                    }
226 227 228

            // Reposition the draggable so that it's center
            // coincides with the center of the target.
229
                    this.snapToTarget(target);
230 231

            // Target was found.
232 233
                    return true;
                }
234 235

        // Target was not found.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
                return false;
            },

            'toggleTargets': function(isEnabled) {
                var effect = isEnabled ? 'move' : null;

                this.state.baseImageEl.attr('aria-dropeffect', effect);
                $.each(this.state.targets, function(index, target) {
                    target.targetEl.attr('aria-dropeffect', effect);
                });
            },

            'snapToTarget': function(target) {
                var offset;

                offset = 0;
                if (this.state.config.targetOutline === true) {
                    offset = 1;
                }

                this.iconEl.css({
                    'left': target.offset.left + 0.5 * target.w - this.iconWidth * 0.5 + offset - this.iconElLeftOffset,
                    'top': target.offset.top + 0.5 * target.h - this.iconHeight * 0.5 + offset
                });

                if (this.labelEl !== null) {
                    this.labelEl.css({
                        'left': target.offset.left + 0.5 * target.w - this.labelWidth * 0.5 + offset - 9, // Acoount for padding, border.
                        'top': target.offset.top + 0.5 * target.h + this.iconHeight * 0.5 + 5 + offset
                    });
                }
            },
268 269 270 271 272 273 274 275 276 277 278

    // Go through all of the draggables subtract 1 from the z-index
    // of all whose z-index is higher than the old z-index of the
    // current element. After, set the z-index of the current
    // element to 1 + N (where N is the number of draggables - i.e.
    // the highest z-index possible).
    //
    // This will make sure that after releasing a draggable, it
    // will be on top of all of the other draggables. Also, the
    // ordering of the visibility (z-index) of the other draggables
    // will not change.
279 280
            'correctZIndexes': function() {
                var c1, highestZIndex;
281

282
                highestZIndex = -10000;
283

284 285 286 287
                if (this.state.config.individualTargets === true) {
                    if (this.onTarget.draggableList.length > 0) {
                        for (c1 = 0; c1 < this.onTarget.draggableList.length; c1 += 1) {
                            if (
288 289 290
                        (this.onTarget.draggableList[c1].zIndex > highestZIndex) &&
                        (this.onTarget.draggableList[c1].zIndex !== 1000)
                    ) {
291 292 293 294 295
                                highestZIndex = this.onTarget.draggableList[c1].zIndex;
                            }
                        }
                    } else {
                        highestZIndex = 0;
296
                    }
297 298 299 300
                } else {
                    for (c1 = 0; c1 < this.state.draggables.length; c1++) {
                        if (this.inContainer === false) {
                            if (
301 302 303
                        (this.state.draggables[c1].zIndex > highestZIndex) &&
                        (this.state.draggables[c1].zIndex !== 1000)
                    ) {
304 305 306
                                highestZIndex = this.state.draggables[c1].zIndex;
                            }
                        }
307 308 309
                    }
                }

310 311 312
                if (highestZIndex === -10000) {
                    highestZIndex = 0;
                }
313

314
                this.zIndex = highestZIndex + 1;
315

316 317 318 319 320
                this.iconEl.css('z-index', this.zIndex);
                if (this.labelEl !== null) {
                    this.labelEl.css('z-index', this.zIndex);
                }
            },
321 322 323 324

    // If a draggable was released in a wrong positione, we will
    // move it back to the slider, placing it in the same position
    // that it was dragged out of.
325 326
            'moveBackToSlider': function() {
                var c1;
327

328
                Targets.destroyTargetField(this);
329

330 331 332 333 334 335 336
                if (this.isOriginal === false) {
                    this.iconEl.remove();
                    if (this.labelEl !== null) {
                        this.labelEl.remove();
                    }

                    this.state.draggables.splice(this.stateDraggablesIndex, 1);
337

338 339 340 341 342
                    for (c1 = 0; c1 < this.state.draggables.length; c1 += 1) {
                        if (this.state.draggables[c1].stateDraggablesIndex > this.stateDraggablesIndex) {
                            this.state.draggables[c1].stateDraggablesIndex -= 1;
                        }
                    }
343

344
                    return;
345 346
                }

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
                this.containerEl.show();
                this.zIndex = 1;

                this.iconEl.detach();
                if (this.iconImgEl !== null) {
                    this.iconImgEl.css({
                        'width': this.iconWidthSmall,
                        'height': this.iconHeightSmall
                    });
                }
                this.iconEl.css({
                    'border': 'none',
                    'background-color': 'transparent',
                    'padding-left': 0,
                    'padding-right': 0,
                    'z-index': this.zIndex,
                    'width': this.iconWidthSmall,
                    'height': this.iconHeightSmall,
                    'left': 50 - this.iconWidthSmall * 0.5,
366 367 368 369

            // Before:
            // 'top': ((this.labelEl !== null) ? (100 - this.iconHeightSmall - 25) * 0.5 : 50 - this.iconHeightSmall * 0.5)
            // After:
370 371 372 373 374 375 376 377 378 379 380 381 382
                    'top': ((this.labelEl !== null) ? 37.5 : 50.0) - 0.5 * this.iconHeightSmall
                });
                this.iconEl.appendTo(this.containerEl);

                if (this.labelEl !== null) {
                    this.labelEl.detach();
                    this.labelEl.css({
                        'border': 'none',
                        'background-color': 'transparent',
                        'padding-left': 0,
                        'padding-right': 0,
                        'z-index': this.zIndex,
                        'left': 50 - this.labelWidth * 0.5,
383 384 385 386

                // Before:
                // 'top': (100 - this.iconHeightSmall - 25) * 0.5 + this.iconHeightSmall + 5
                // After:
387 388 389 390 391 392 393 394 395
                        'top': 42.5 + 0.5 * this.iconHeightSmall
                    });
                    this.labelEl.appendTo(this.containerEl);
                }

                this.inContainer = true;
            }
        }; // End-of: return {
    }); // End-of: define(['update_input', 'targets'], function (updateInput, Targets) {
396
}(RequireJS.requirejs, RequireJS.require, RequireJS.define)); // End-of: (function (requirejs, require, define) {