wp/wp-includes/js/imgareaselect/jquery.imgareaselect.js
author ymh <ymh.work@gmail.com>
Fri, 05 Sep 2025 18:40:08 +0200
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
permissions -rw-r--r--
Add CLAUDE.md documentation and sync WordPress core files - Add comprehensive CLAUDE.md documentation file with project architecture, development setup, database operations, WordPress CLI usage, file sync procedures, and Mercurial commands - Update WordPress core files from wordpress/ to wp/ directory - Sync latest WordPress admin interface, includes, and core functionality - Update plugins: portfolio plugin with latest BWS framework and fancybox integration - Maintain custom configuration and theme files 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
 * imgAreaSelect jQuery plugin
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
     3
 * version 0.9.10-wp-6.2
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * Copyright (c) 2008-2013 Michal Wojciechowski (odyniec.net)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * Dual licensed under the MIT (MIT-LICENSE.txt)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * and GPL (GPL-LICENSE.txt) licenses.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    10
 * https://github.com/odyniec/imgareaselect
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
(function($) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * Math functions will be used extensively, so it's convenient to make a few
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * shortcuts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
var abs = Math.abs,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    max = Math.max,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    min = Math.min,
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    23
    floor = Math.floor;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * Create a new HTML div element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @return A jQuery object representing the new element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
function div() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    return $('<div/>');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 * imgAreaSelect initialization
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
 * @param img
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
 *            A HTML image element to attach the plugin to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
 * @param options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
 *            An options object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
$.imgAreaSelect = function (img, options) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    var
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        /* jQuery object representing the image */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        $img = $(img),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        /* Has the image finished loading? */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        imgLoaded,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        /* Plugin elements */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        /* Container box */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        $box = div(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        /* Selection area */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        $area = div(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        /* Border (four divs) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        $border = div().add(div()).add(div()).add(div()),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        /* Outer area (four divs) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        $outer = div().add(div()).add(div()).add(div()),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        /* Handles (empty by default, initialized in setOptions()) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $handles = $([]),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
         * Additional element to work around a cursor problem in Opera
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
         * (explained later)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        $areaOpera,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        /* Image position (relative to viewport) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        left, top,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        /* Image offset (as returned by .offset()) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        imgOfs = { left: 0, top: 0 },
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        /* Image dimensions (as returned by .width() and .height()) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        imgWidth, imgHeight,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
         * jQuery object representing the parent element that the plugin
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
         * elements are appended to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        $parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        /* Parent element offset (as returned by .offset()) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        parOfs = { left: 0, top: 0 },
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        /* Base z-index for plugin elements */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        zIndex = 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        /* Plugin elements position */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        position = 'absolute',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        /* X/Y coordinates of the starting point for move/resize operations */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        startX, startY,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        /* Horizontal and vertical scaling factors */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        scaleX, scaleY,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        /* Current resize mode ("nw", "se", etc.) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        resize,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        /* Selection area constraints */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        minWidth, minHeight, maxWidth, maxHeight,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        /* Aspect ratio to maintain (floating point number) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        aspectRatio,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        /* Are the plugin elements currently displayed? */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        shown,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        /* Current selection (relative to parent element) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        x1, y1, x2, y2,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        /* Current selection (relative to scaled image) */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        selection = { x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0 },
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        /* Document element */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        docElem = document.documentElement,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        /* User agent */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        ua = navigator.userAgent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        /* Various helper variables used throughout the code */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        $p, d, i, o, w, h, adjusted;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
     * Translate selection coordinates (relative to scaled image) to viewport
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
     * coordinates (relative to parent element)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
     * Translate selection X to viewport X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
     * @param x
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
     *            Selection X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
     * @return Viewport X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    function viewX(x) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        return x + imgOfs.left - parOfs.left;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
     * Translate selection Y to viewport Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
     * @param y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
     *            Selection Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
     * @return Viewport Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
    function viewY(y) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        return y + imgOfs.top - parOfs.top;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
     * Translate viewport coordinates to selection coordinates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
     * Translate viewport X to selection X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
     * @param x
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
     *            Viewport X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
     * @return Selection X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    function selX(x) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        return x - imgOfs.left + parOfs.left;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
     * Translate viewport Y to selection Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
     * @param y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
     *            Viewport Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
     * @return Selection Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    function selY(y) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
        return y - imgOfs.top + parOfs.top;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
     * Translate event coordinates (relative to document) to viewport
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
     * coordinates
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
     * Get event X and translate it to viewport X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
     * @return Viewport X
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    function evX(event) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
        return max(event.pageX || 0, touchCoords(event).x) - parOfs.left;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
     * Get event Y and translate it to viewport Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
     * @return Viewport Y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    function evY(event) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
        return max(event.pageY || 0, touchCoords(event).y) - parOfs.top;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
    }
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
    /**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
     * Get X and Y coordinates of a touch event
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
     *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
     * @param event
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
     *            The event object
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   211
     * @return Coordinates object
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
     */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
    function touchCoords(event) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
        var oev = event.originalEvent || {};
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
        if (oev.touches && oev.touches.length)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
            return { x: oev.touches[0].pageX, y: oev.touches[0].pageY };
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
        else
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
            return { x: 0, y: 0 };
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
     * Get the current selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
     * @param noScale
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
     *            If set to <code>true</code>, scaling is not applied to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
     *            returned selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
     * @return Selection object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    function getSelection(noScale) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        var sx = noScale || scaleX, sy = noScale || scaleY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   233
        return { x1: floor(selection.x1 * sx),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   234
            y1: floor(selection.y1 * sy),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   235
            x2: floor(selection.x2 * sx),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   236
            y2: floor(selection.y2 * sy),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   237
            width: floor(selection.x2 * sx) - floor(selection.x1 * sx),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   238
            height: floor(selection.y2 * sy) - floor(selection.y1 * sy) };
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
     * Set the current selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
     * @param x1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
     *            X coordinate of the upper left corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
     * @param y1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
     *            Y coordinate of the upper left corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
     * @param x2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
     *            X coordinate of the lower right corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
     * @param y2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
     *            Y coordinate of the lower right corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
     * @param noScale
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
     *            If set to <code>true</code>, scaling is not applied to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
     *            new selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
    function setSelection(x1, y1, x2, y2, noScale) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        var sx = noScale || scaleX, sy = noScale || scaleY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        selection = {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   260
            x1: floor(x1 / sx || 0),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   261
            y1: floor(y1 / sy || 0),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   262
            x2: floor(x2 / sx || 0),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   263
            y2: floor(y2 / sy || 0)
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        selection.width = selection.x2 - selection.x1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
        selection.height = selection.y2 - selection.y1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
     * Recalculate image and parent offsets
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    function adjust() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
         * Do not adjust if image has not yet loaded or if width is not a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
         * positive number. The latter might happen when imgAreaSelect is put
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
         * on a parent element which is then hidden.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        if (!imgLoaded || !$img.width())
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
            return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
         * Get image offset. The .offset() method returns float values, so they
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
         * need to be rounded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
         */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   286
        imgOfs = { left: floor($img.offset().left), top: floor($img.offset().top) };
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        /* Get image dimensions */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
        imgWidth = $img.innerWidth();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
        imgHeight = $img.innerHeight();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        imgOfs.top += ($img.outerHeight() - imgHeight) >> 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        imgOfs.left += ($img.outerWidth() - imgWidth) >> 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        /* Set minimum and maximum selection area dimensions */
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   296
        minWidth = floor(options.minWidth / scaleX) || 0;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   297
        minHeight = floor(options.minHeight / scaleY) || 0;
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   298
        maxWidth = floor(min(options.maxWidth / scaleX || 1<<24, imgWidth));
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   299
        maxHeight = floor(min(options.maxHeight / scaleY || 1<<24, imgHeight));
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
         * Workaround for jQuery 1.3.2 incorrect offset calculation, originally
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
         * observed in Safari 3. Firefox 2 is also affected.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
        if ($().jquery == '1.3.2' && position == 'fixed' &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
            !docElem['getBoundingClientRect'])
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
        {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
            imgOfs.top += max(document.body.scrollTop, docElem.scrollTop);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
            imgOfs.left += max(document.body.scrollLeft, docElem.scrollLeft);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        /* Determine parent element offset */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
        parOfs = /absolute|relative/.test($parent.css('position')) ?
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   314
            { left: floor($parent.offset().left) - $parent.scrollLeft(),
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   315
                top: floor($parent.offset().top) - $parent.scrollTop() } :
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
            position == 'fixed' ?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
                { left: $(document).scrollLeft(), top: $(document).scrollTop() } :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
                { left: 0, top: 0 };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
        left = viewX(0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
        top = viewY(0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
         * Check if selection area is within image boundaries, adjust if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
         * necessary
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
        if (selection.x2 > imgWidth || selection.y2 > imgHeight)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
            doResize();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
     * Update plugin elements
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
     * @param resetKeyPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
     *            If set to <code>false</code>, this instance's keypress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
     *            event handler is not activated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
    function update(resetKeyPress) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
        /* If plugin elements are hidden, do nothing */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        if (!shown) return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
         * Set the position and size of the container box and the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
         * inside it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
        $box.css({ left: viewX(selection.x1), top: viewY(selection.y1) })
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
            .add($area).width(w = selection.width).height(h = selection.height);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
         * Reset the position of selection area, borders, and handles (IE6/IE7
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
         * position them incorrectly if we don't do this)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
        $area.add($border).add($handles).css({ left: 0, top: 0 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
        /* Set border dimensions */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
        $border
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
            .width(max(w - $border.outerWidth() + $border.innerWidth(), 0))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
            .height(max(h - $border.outerHeight() + $border.innerHeight(), 0));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
        /* Arrange the outer area elements */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
        $($outer[0]).css({ left: left, top: top,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
            width: selection.x1, height: imgHeight });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
        $($outer[1]).css({ left: left + selection.x1, top: top,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
            width: w, height: selection.y1 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
        $($outer[2]).css({ left: left + selection.x2, top: top,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
            width: imgWidth - selection.x2, height: imgHeight });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
        $($outer[3]).css({ left: left + selection.x1, top: top + selection.y2,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
            width: w, height: imgHeight - selection.y2 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
        w -= $handles.outerWidth();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
        h -= $handles.outerHeight();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
        /* Arrange handles */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
        switch ($handles.length) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
        case 8:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
            $($handles[4]).css({ left: w >> 1 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
            $($handles[5]).css({ left: w, top: h >> 1 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
            $($handles[6]).css({ left: w >> 1, top: h });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
            $($handles[7]).css({ top: h >> 1 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
        case 4:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
            $handles.slice(1,3).css({ left: w });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
            $handles.slice(2,4).css({ top: h });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
        if (resetKeyPress !== false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
            /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
             * Need to reset the document keypress event handler -- unbind the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
             * current handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
             */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
            if ($.imgAreaSelect.onKeyPress != docKeyPress)
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   391
                $(document).off($.imgAreaSelect.keyPress,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
                    $.imgAreaSelect.onKeyPress);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
            if (options.keys)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
                /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
                 * Set the document keypress event handler to this instance's
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
                 * docKeyPress() function
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
                 */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   399
                $(document).on( $.imgAreaSelect.keyPress, function() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   400
                    $.imgAreaSelect.onKeyPress = docKeyPress;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   401
                });
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
         * Internet Explorer displays 1px-wide dashed borders incorrectly by
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
         * filling the spaces between dashes with white. Toggling the margin
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
         * property between 0 and "auto" fixes this in IE6 and IE7 (IE8 is still
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
         * broken). This workaround is not perfect, as it requires setTimeout()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
         * and thus causes the border to flicker a bit, but I haven't found a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
         * better solution.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
         *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
         * Note: This only happens with CSS borders, set with the borderWidth,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
         * borderOpacity, borderColor1, and borderColor2 options (which are now
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
         * deprecated). Borders created with GIF background images are fine.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
        if (msie && $border.outerWidth() - $border.innerWidth() == 2) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
            $border.css('margin', 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
            setTimeout(function () { $border.css('margin', 'auto'); }, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
     * Do the complete update sequence: recalculate offsets, update the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
     * elements, and set the correct values of x1, y1, x2, and y2.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
     * @param resetKeyPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
     *            If set to <code>false</code>, this instance's keypress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
     *            event handler is not activated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
    function doUpdate(resetKeyPress) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
        adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
        update(resetKeyPress);
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   433
        updateSelectionRelativeToParentElement();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   434
    }
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   435
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   436
    /**
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   437
     * Set the correct values of x1, y1, x2, and y2.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   438
     */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   439
    function updateSelectionRelativeToParentElement() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
        x1 = viewX(selection.x1); y1 = viewY(selection.y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
        x2 = viewX(selection.x2); y2 = viewY(selection.y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
     * Hide or fade out an element (or multiple elements)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
     * @param $elem
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
     *            A jQuery object containing the element(s) to hide/fade out
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
     * @param fn
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
     *            Callback function to be called when fadeOut() completes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
    function hide($elem, fn) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
        options.fadeSpeed ? $elem.fadeOut(options.fadeSpeed, fn) : $elem.hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
     * Selection area mousemove event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
    function areaMouseMove(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
        var x = selX(evX(event)) - selection.x1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
            y = selY(evY(event)) - selection.y1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
        if (!adjusted) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
            adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
            adjusted = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
            $box.one('mouseout', function () { adjusted = false; });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
        /* Clear the resize mode */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
        resize = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
        if (options.resizable) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
            /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
             * Check if the mouse pointer is over the resize margin area and set
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
             * the resize mode accordingly
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
             */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
            if (y <= options.resizeMargin)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
                resize = 'n';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
            else if (y >= selection.height - options.resizeMargin)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
                resize = 's';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
            if (x <= options.resizeMargin)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
                resize += 'w';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
            else if (x >= selection.width - options.resizeMargin)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
                resize += 'e';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
        $box.css('cursor', resize ? resize + '-resize' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
            options.movable ? 'move' : '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
        if ($areaOpera)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
            $areaOpera.toggle();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
     * Document mouseup event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
    function docMouseUp(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
        /* Set back the default cursor */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
        $('body').css('cursor', '');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
         * If autoHide is enabled, or if the selection has zero width/height,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
         * hide the selection and the outer area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
        if (options.autoHide || selection.width * selection.height == 0)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
            hide($box.add($outer), function () { $(this).hide(); });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
        $(document).off('mousemove touchmove', selectingMouseMove);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
        $box.on('mousemove touchmove', areaMouseMove);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
        options.onSelectEnd(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
     * Selection area mousedown event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
     * @return false
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
    function areaMouseDown(event) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
        if (event.type == 'mousedown' && event.which != 1) return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   529
    	/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
    	 * With mobile browsers, there is no "moving the pointer over" action,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   531
    	 * so we need to simulate one mousemove event happening prior to
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   532
    	 * mousedown/touchstart.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   533
    	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
    	areaMouseMove(event);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
        adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
        if (resize) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
            /* Resize mode is in effect */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
            $('body').css('cursor', resize + '-resize');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
            x1 = viewX(selection[/w/.test(resize) ? 'x2' : 'x1']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
            y1 = viewY(selection[/n/.test(resize) ? 'y2' : 'y1']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   545
            $(document).on('mousemove touchmove', selectingMouseMove)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   546
                .one('mouseup touchend', docMouseUp);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
            $box.off('mousemove touchmove', areaMouseMove);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
        else if (options.movable) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
            startX = left + selection.x1 - evX(event);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
            startY = top + selection.y1 - evY(event);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   553
            $box.off('mousemove touchmove', areaMouseMove);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   555
            $(document).on('mousemove touchmove', movingMouseMove)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
                .one('mouseup touchend', function () {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
                    options.onSelectEnd(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
                    $(document).off('mousemove touchmove', movingMouseMove);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
                    $box.on('mousemove touchmove', areaMouseMove);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
                });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
        else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
            $img.mousedown(event);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
     * Adjust the x2/y2 coordinates to maintain aspect ratio (if defined)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
     * @param xFirst
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
     *            If set to <code>true</code>, calculate x2 first. Otherwise,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
     *            calculate y2 first.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
    function fixAspectRatio(xFirst) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
        if (aspectRatio)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
            if (xFirst) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
                x2 = max(left, min(left + imgWidth,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
                    x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1)));
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   581
                y2 = floor(max(top, min(top + imgHeight,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
                    y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1))));
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   583
                x2 = floor(x2);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
            else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
                y2 = max(top, min(top + imgHeight,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
                    y1 + abs(x2 - x1) / aspectRatio * (y2 > y1 || -1)));
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   588
                x2 = floor(max(left, min(left + imgWidth,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
                    x1 + abs(y2 - y1) * aspectRatio * (x2 > x1 || -1))));
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   590
                y2 = floor(y2);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
     * Resize the selection area respecting the minimum/maximum dimensions and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
     * aspect ratio
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
    function doResize() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
        /*
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   600
         * Make sure x1, x2, y1, y2 are initialized to avoid the following calculation
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   601
         * getting incorrect results.
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   602
         */
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   603
        if ( x1 == null || x2 == null || y1 == null || y2 == null ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   604
            updateSelectionRelativeToParentElement();
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   605
        }
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   606
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   607
        /*
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
         * Make sure the top left corner of the selection area stays within
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
         * image boundaries (it might not if the image source was dynamically
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
         * changed).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
        x1 = min(x1, left + imgWidth);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
        y1 = min(y1, top + imgHeight);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
        if (abs(x2 - x1) < minWidth) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
            /* Selection width is smaller than minWidth */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
            x2 = x1 - minWidth * (x2 < x1 || -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
            if (x2 < left)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
                x1 = left + minWidth;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
            else if (x2 > left + imgWidth)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
                x1 = left + imgWidth - minWidth;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
        if (abs(y2 - y1) < minHeight) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
            /* Selection height is smaller than minHeight */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
            y2 = y1 - minHeight * (y2 < y1 || -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
            if (y2 < top)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
                y1 = top + minHeight;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
            else if (y2 > top + imgHeight)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
                y1 = top + imgHeight - minHeight;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
        x2 = max(left, min(x2, left + imgWidth));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
        y2 = max(top, min(y2, top + imgHeight));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
        fixAspectRatio(abs(x2 - x1) < abs(y2 - y1) * aspectRatio);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
        if (abs(x2 - x1) > maxWidth) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
            /* Selection width is greater than maxWidth */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
            x2 = x1 - maxWidth * (x2 < x1 || -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
            fixAspectRatio();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
        if (abs(y2 - y1) > maxHeight) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
            /* Selection height is greater than maxHeight */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
            y2 = y1 - maxHeight * (y2 < y1 || -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
            fixAspectRatio(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
        selection = { x1: selX(min(x1, x2)), x2: selX(max(x1, x2)),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
            y1: selY(min(y1, y2)), y2: selY(max(y1, y2)),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
            width: abs(x2 - x1), height: abs(y2 - y1) };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
        update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
        options.onSelectChange(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
     * Mousemove event handler triggered when the user is selecting an area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
     * @return false
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
    function selectingMouseMove(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
        x2 = /w|e|^$/.test(resize) || aspectRatio ? evX(event) : viewX(selection.x2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
        y2 = /n|s|^$/.test(resize) || aspectRatio ? evY(event) : viewY(selection.y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
        doResize();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
     * Move the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
     * @param newX1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
     *            New viewport X1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
     * @param newY1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
     *            New viewport Y1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
    function doMove(newX1, newY1) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
        x2 = (x1 = newX1) + selection.width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
        y2 = (y1 = newY1) + selection.height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
        $.extend(selection, { x1: selX(x1), y1: selY(y1), x2: selX(x2),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
            y2: selY(y2) });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
        update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
        options.onSelectChange(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
     * Mousemove event handler triggered when the selection area is being moved
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
     * @return false
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
    function movingMouseMove(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
        x1 = max(left, min(startX + evX(event), left + imgWidth - selection.width));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
        y1 = max(top, min(startY + evY(event), top + imgHeight - selection.height));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
        doMove(x1, y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
        event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
     * Start selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
    function startSelection() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
        $(document).off('mousemove touchmove', startSelection);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
        adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
        x2 = x1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
        y2 = y1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
        doResize();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
        resize = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
        if (!$outer.is(':visible'))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
            /* Show the plugin elements */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
            $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
        shown = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   733
        $(document).off('mouseup touchend', cancelSelection)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   734
            .on('mousemove touchmove', selectingMouseMove)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   735
            .one('mouseup touchend', docMouseUp);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   736
        $box.off('mousemove touchmove', areaMouseMove);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
        options.onSelectStart(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
     * Cancel selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
    function cancelSelection() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   745
        $(document).off('mousemove touchmove', startSelection)
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
            .off('mouseup touchend', cancelSelection);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
        hide($box.add($outer));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
        setSelection(selX(x1), selY(y1), selX(x1), selY(y1));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
        /* If this is an API call, callback functions should not be triggered */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
        if (!(this instanceof $.imgAreaSelect)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
            options.onSelectChange(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
            options.onSelectEnd(img, getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
     * Image mousedown event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
     * @return false
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
    function imgMouseDown(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
        /* Ignore the event if animation is in progress */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   767
        if (event.which > 1 || $outer.is(':animated')) return false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
        adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
        startX = x1 = evX(event);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
        startY = y1 = evY(event);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
        /* Selection will start when the mouse is moved */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   774
        $(document).on({ 'mousemove touchmove': startSelection,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   775
            'mouseup touchend': cancelSelection });
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
     * Window resize event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
    function windowResize() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
        doUpdate(false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
     * Image load event handler. This is the final part of the initialization
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
     * process.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
    function imgLoad() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
        imgLoaded = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
        /* Set options */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
        setOptions(options = $.extend({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
            classPrefix: 'imgareaselect',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
            movable: true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
            parent: 'body',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
            resizable: true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
            resizeMargin: 10,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
            onInit: function () {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
            onSelectStart: function () {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
            onSelectChange: function () {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
            onSelectEnd: function () {}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
        }, options));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
        $box.add($outer).css({ visibility: '' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
        if (options.show) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
            shown = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
            adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
            update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
            $box.add($outer).hide().fadeIn(options.fadeSpeed||0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
         * Call the onInit callback. The setTimeout() call is used to ensure
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
         * that the plugin has been fully initialized and the object instance is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
         * available (so that it can be obtained in the callback).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
        setTimeout(function () { options.onInit(img, getSelection()); }, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
     * Document keypress event handler
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
     * @param event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
     *            The event object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
     * @return false
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
    var docKeyPress = function(event) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
        var k = options.keys, d, t, key = event.keyCode;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
        d = !isNaN(k.alt) && (event.altKey || event.originalEvent.altKey) ? k.alt :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
            !isNaN(k.ctrl) && event.ctrlKey ? k.ctrl :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
            !isNaN(k.shift) && event.shiftKey ? k.shift :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
            !isNaN(k.arrows) ? k.arrows : 10;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
        if (k.arrows == 'resize' || (k.shift == 'resize' && event.shiftKey) ||
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
            (k.ctrl == 'resize' && event.ctrlKey) ||
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
            (k.alt == 'resize' && (event.altKey || event.originalEvent.altKey)))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
        {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
            /* Resize selection */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
            switch (key) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
            case 37:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
                /* Left */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
                d = -d;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
            case 39:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
                /* Right */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
                t = max(x1, x2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
                x1 = min(x1, x2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
                x2 = max(t + d, x1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
                fixAspectRatio();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
            case 38:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
                /* Up */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
                d = -d;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
            case 40:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
                /* Down */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
                t = max(y1, y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
                y1 = min(y1, y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
                y2 = max(t + d, y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
                fixAspectRatio(true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
            default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
                return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
            doResize();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
        else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
            /* Move selection */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
            x1 = min(x1, x2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
            y1 = min(y1, y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
            switch (key) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
            case 37:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
                /* Left */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
                doMove(max(x1 - d, left), y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
            case 38:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
                /* Up */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
                doMove(x1, max(y1 - d, top));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
            case 39:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
                /* Right */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
                doMove(x1 + min(d, imgWidth - selX(x2)), y1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
            case 40:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
                /* Down */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
                doMove(x1, y1 + min(d, imgHeight - selY(y2)));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
                break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
            default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
                return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
        return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
    };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
     * Apply style options to plugin element (or multiple elements)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
     * @param $elem
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
     *            A jQuery object representing the element(s) to style
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
     * @param props
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
     *            An object that maps option names to corresponding CSS
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
     *            properties
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
    function styleOptions($elem, props) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
        for (var option in props)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
            if (options[option] !== undefined)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
                $elem.css(props[option], options[option]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
     * Set plugin options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
     * @param newOptions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
     *            The new options object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
    function setOptions(newOptions) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
        if (newOptions.parent)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
            ($parent = $(newOptions.parent)).append($box.add($outer));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
        /* Merge the new options with the existing ones */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
        $.extend(options, newOptions);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
        adjust();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
        if (newOptions.handles != null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
            /* Recreate selection area handles */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
            $handles.remove();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
            $handles = $([]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
            i = newOptions.handles ? newOptions.handles == 'corners' ? 4 : 8 : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
            while (i--)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
                $handles = $handles.add(div());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
            /* Add a class to handles and set the CSS properties */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
            $handles.addClass(options.classPrefix + '-handle').css({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
                position: 'absolute',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
                /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
                 * The font-size property needs to be set to zero, otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
                 * Internet Explorer makes the handles too large
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
                 */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   950
                fontSize: '0',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
                zIndex: zIndex + 1 || 1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
            });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
            /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
             * If handle width/height has not been set with CSS rules, set the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
             * default 5px
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
             */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
            if (!parseInt($handles.css('width')) >= 0)
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   959
                $handles.width(10).height(10);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
            /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
             * If the borderWidth option is in use, add a solid border to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
             * handles
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
             */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
            if (o = options.borderWidth)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
                $handles.css({ borderWidth: o, borderStyle: 'solid' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
            /* Apply other style options */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
            styleOptions($handles, { borderColor1: 'border-color',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
                borderColor2: 'background-color',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
                borderOpacity: 'opacity' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
        /* Calculate scale factors */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
        scaleX = options.imageWidth / imgWidth || 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
        scaleY = options.imageHeight / imgHeight || 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
        /* Set selection */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
        if (newOptions.x1 != null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
            setSelection(newOptions.x1, newOptions.y1, newOptions.x2,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
                newOptions.y2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
            newOptions.show = !newOptions.hide;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
        if (newOptions.keys)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
            /* Enable keyboard support */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
            options.keys = $.extend({ shift: 1, ctrl: 'resize' },
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
                newOptions.keys);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
        /* Add classes to plugin elements */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
        $outer.addClass(options.classPrefix + '-outer');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
        $area.addClass(options.classPrefix + '-selection');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
        for (i = 0; i++ < 4;)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
            $($border[i-1]).addClass(options.classPrefix + '-border' + i);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
        /* Apply style options */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
        styleOptions($area, { selectionColor: 'background-color',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
            selectionOpacity: 'opacity' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
        styleOptions($border, { borderOpacity: 'opacity',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
            borderWidth: 'border-width' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
        styleOptions($outer, { outerColor: 'background-color',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
            outerOpacity: 'opacity' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
        if (o = options.borderColor1)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
            $($border[0]).css({ borderStyle: 'solid', borderColor: o });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
        if (o = options.borderColor2)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
            $($border[1]).css({ borderStyle: 'dashed', borderColor: o });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
        /* Append all the selection area elements to the container box */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
        $box.append($area.add($border).add($areaOpera)).append($handles);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
        if (msie) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
            if (o = ($outer.css('filter')||'').match(/opacity=(\d+)/))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
                $outer.css('opacity', o[1]/100);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
            if (o = ($border.css('filter')||'').match(/opacity=(\d+)/))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
                $border.css('opacity', o[1]/100);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
        if (newOptions.hide)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
            hide($box.add($outer));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
        else if (newOptions.show && imgLoaded) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
            shown = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
            $box.add($outer).fadeIn(options.fadeSpeed||0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
            doUpdate();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
        /* Calculate the aspect ratio factor */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
        aspectRatio = (d = (options.aspectRatio || '').split(/:/))[0] / d[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1029
        $img.add($outer).off('mousedown', imgMouseDown);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
        if (options.disable || options.enable === false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
            /* Disable the plugin */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1033
            $box.off({ 'mousemove touchmove': areaMouseMove,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1034
                'mousedown touchstart': areaMouseDown });
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1035
            $(window).off('resize', windowResize);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
        else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
            if (options.enable || options.disable === false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
                /* Enable the plugin */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
                if (options.resizable || options.movable)
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1041
                    $box.on({ 'mousemove touchmove': areaMouseMove,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
                        'mousedown touchstart': areaMouseDown });
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1044
                $(window).on( 'resize', windowResize);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
            if (!options.persistent)
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1048
                $img.add($outer).on('mousedown touchstart', imgMouseDown);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
        options.enable = options.disable = undefined;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
     * Remove plugin completely
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
    this.remove = function () {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
         * Call setOptions with { disable: true } to unbind the event handlers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
        setOptions({ disable: true });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
        $box.add($outer).remove();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
    };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
     * Public API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
     * Get current options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
     * @return An object containing the set of options currently in use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
    this.getOptions = function () { return options; };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
     * Set plugin options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
     * @param newOptions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
     *            The new options object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
    this.setOptions = setOptions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
     * Get the current selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
     * @param noScale
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
     *            If set to <code>true</code>, scaling is not applied to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
     *            returned selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
     * @return Selection object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
    this.getSelection = getSelection;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
     * Set the current selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
     * @param x1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
     *            X coordinate of the upper left corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
     * @param y1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
     *            Y coordinate of the upper left corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
     * @param x2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
     *            X coordinate of the lower right corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
     * @param y2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
     *            Y coordinate of the lower right corner of the selection area
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
     * @param noScale
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
     *            If set to <code>true</code>, scaling is not applied to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
     *            new selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
    this.setSelection = setSelection;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
     * Cancel selection
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
    this.cancelSelection = cancelSelection;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
    /**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
     * Update plugin elements
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
     *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
     * @param resetKeyPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
     *            If set to <code>false</code>, this instance's keypress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
     *            event handler is not activated
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
    this.update = doUpdate;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
    /* Do the dreaded browser detection */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
    var msie = (/msie ([\w.]+)/i.exec(ua)||[])[1],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
        opera = /opera/i.test(ua),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
        safari = /webkit/i.test(ua) && !/chrome/i.test(ua);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
     * Traverse the image's parent elements (up to <body>) and find the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
     * highest z-index
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
    $p = $img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
    while ($p.length) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
        zIndex = max(zIndex,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
            !isNaN($p.css('z-index')) ? $p.css('z-index') : zIndex);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
        /* Also check if any of the ancestor elements has fixed position */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
        if ($p.css('position') == 'fixed')
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
            position = 'fixed';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
        $p = $p.parent(':not(body)');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
    }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
     * If z-index is given as an option, it overrides the one found by the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
     * above loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
    zIndex = options.zIndex || zIndex;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
    if (msie)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
        $img.attr('unselectable', 'on');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
     * In MSIE and WebKit, we need to use the keydown event instead of keypress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
    $.imgAreaSelect.keyPress = msie || safari ? 'keydown' : 'keypress';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
     * There is a bug affecting the CSS cursor property in Opera (observed in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
     * versions up to 10.00) that prevents the cursor from being updated unless
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
     * the mouse leaves and enters the element again. To trigger the mouseover
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
     * event, we're adding an additional div to $box and we're going to toggle
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
     * it when mouse moves inside the selection area.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
    if (opera)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
        $areaOpera = div().css({ width: '100%', height: '100%',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
            position: 'absolute', zIndex: zIndex + 2 || 2 });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
     * We initially set visibility to "hidden" as a workaround for a weird
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
     * behaviour observed in Google Chrome 1.0.154.53 (on Windows XP). Normally
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
     * we would just set display to "none", but, for some reason, if we do so
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
     * then Chrome refuses to later display the element with .show() or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
     * .fadeIn().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
    $box.add($outer).css({ visibility: 'hidden', position: position,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
        overflow: 'hidden', zIndex: zIndex || '0' });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
    $box.css({ zIndex: zIndex + 2 || 2 });
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1181
    $area.add($border).css({ position: 'absolute', fontSize: '0' });
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
     * If the image has been fully loaded, or if it is not really an image (eg.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
     * a div), call imgLoad() immediately; otherwise, bind it to be called once
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
     * on image load event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
    img.complete || img.readyState == 'complete' || !$img.is('img') ?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
        imgLoad() : $img.one('load', imgLoad);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
    /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
     * MSIE 9.0 doesn't always fire the image load event -- resetting the src
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
     * attribute seems to trigger it. The check is for version 7 and above to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
     * accommodate for MSIE 9 running in compatibility mode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
     */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
    if (!imgLoaded && msie && msie >= 7)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
        img.src = img.src;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
 * Invoke imgAreaSelect on a jQuery object containing the image(s)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
 * @param options
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
 *            Options object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
 * @return The jQuery object or a reference to imgAreaSelect instance (if the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
 *         <code>instance</code> option was specified)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
$.fn.imgAreaSelect = function (options) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
    options = options || {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
    this.each(function () {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
        /* Is there already an imgAreaSelect instance bound to this element? */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
        if ($(this).data('imgAreaSelect')) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
            /* Yes there is -- is it supposed to be removed? */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
            if (options.remove) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
                /* Remove the plugin */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
                $(this).data('imgAreaSelect').remove();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
                $(this).removeData('imgAreaSelect');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
            else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
                /* Reset options */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
                $(this).data('imgAreaSelect').setOptions(options);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
        else if (!options.remove) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
            /* No exising instance -- create a new one */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
            /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
             * If neither the "enable" nor the "disable" option is present, add
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
             * "enable" as the default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
             */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
            if (options.enable === undefined && options.disable === undefined)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
                options.enable = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
            $(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
    });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
    if (options.instance)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
        /*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
         * Return the imgAreaSelect instance bound to the first element in the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
         * set
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
         */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
        return $(this).data('imgAreaSelect');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
    return this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
})(jQuery);