1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.CodeMirrorDragToAdjustNumberController = function(codeMirror)
27{
28    this._codeMirror = codeMirror;
29
30    this._dragToAdjustController = new WebInspector.DragToAdjustController(this);
31};
32
33WebInspector.CodeMirrorDragToAdjustNumberController.prototype = {
34    constructor: WebInspector.CodeMirrorDragToAdjustNumberController,
35
36    // Public
37
38    get enabled()
39    {
40        return this._dragToAdjustController.enabled;
41    },
42
43    set enabled(enabled)
44    {
45        if (this.enabled === enabled)
46            return;
47
48        this._dragToAdjustController.element = this._codeMirror.getWrapperElement();
49        this._dragToAdjustController.enabled = enabled;
50    },
51
52    // Protected
53
54    dragToAdjustControllerActiveStateChanged: function(dragToAdjustController)
55    {
56        if (!dragToAdjustController.active)
57            this._hoveredTokenInfo = null;
58    },
59
60    dragToAdjustControllerCanBeActivated: function(dragToAdjustController)
61    {
62        return !this._codeMirror.getOption("readOnly");
63    },
64
65    dragToAdjustControllerCanBeAdjusted: function(dragToAdjustController)
66    {
67
68        return this._hoveredTokenInfo && this._hoveredTokenInfo.containsNumber;
69    },
70
71    dragToAdjustControllerWasAdjustedByAmount: function(dragToAdjustController, amount)
72    {
73        this._codeMirror.alterNumberInRange(amount, this._hoveredTokenInfo.startPosition, this._hoveredTokenInfo.endPosition, false);
74    },
75
76    dragToAdjustControllerDidReset: function(dragToAdjustController)
77    {
78        this._hoveredTokenInfo = null;
79    },
80
81    dragToAdjustControllerCanAdjustObjectAtPoint: function(dragToAdjustController, point)
82    {
83        var position = this._codeMirror.coordsChar({left: point.x, top: point.y});
84        var token = this._codeMirror.getTokenAt(position);
85
86        if (!token || !token.type || !token.string) {
87            if (this._hoveredTokenInfo)
88                dragToAdjustController.reset();
89            return false;
90        }
91
92        // Stop right here if we're hovering the same token as we were last time.
93        if (this._hoveredTokenInfo && this._hoveredTokenInfo.line === position.line &&
94            this._hoveredTokenInfo.token.start === token.start && this._hoveredTokenInfo.token.end === token.end)
95            return this._hoveredTokenInfo.token.type.indexOf("number") !== -1;
96
97        var containsNumber = token.type.indexOf("number") !== -1;
98        this._hoveredTokenInfo = {
99            token: token,
100            line: position.line,
101            containsNumber: containsNumber,
102            startPosition: {
103                ch: token.start,
104                line: position.line
105            },
106            endPosition: {
107                ch: token.end,
108                line: position.line
109            }
110        };
111
112        return containsNumber;
113    }
114};
115
116CodeMirror.defineOption("dragToAdjustNumbers", true, function(codeMirror, value, oldValue) {
117    if (!codeMirror.dragToAdjustNumberController)
118        codeMirror.dragToAdjustNumberController = new WebInspector.CodeMirrorDragToAdjustNumberController(codeMirror);
119    codeMirror.dragToAdjustNumberController.enabled = value;
120});
121