aboutsummaryrefslogtreecommitdiff
path: root/js/panels/properties/sections/position-and-size.reel/position-and-size.js
blob: 43f08fcf83234f1d49f2dea33bd8febe79d8e480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

var Montage = require("montage/core/core").Montage;
var Component = require("montage/ui/component").Component;

exports.PosSize = Montage.create(Component, {
    leftPosition: {
        value: 0
    },

    topPosition: {
        value: 0
    },

    heightSize: {
        value: 0
    },

    widthSize: {
        value: 0
    },

    savedPosition: {
        value: null
    },

    _disablePosition: {
        value: true
    },

    disablePosition: {
        get: function () {
            return this._disablePosition;
        },
        set: function (value) {
            if(value !== this._disablePosition) {
                this._disablePosition  = value;
                this.needsDraw = true;
            }
        }
    },

    prepareForDraw: {
        value: function() {
            this.leftControl.identifier = "left";
            this.leftControl.addEventListener("change", this, false);
            this.leftControl.addEventListener("changing", this, false);

            this.topControl.identifier = "top";
            this.topControl.addEventListener("change", this, false);
            this.topControl.addEventListener("changing", this, false);

            this.heightControl.identifier = "height";
            this.heightControl.addEventListener("change", this, false);
            this.heightControl.addEventListener("changing", this, false);

            this.widthControl.identifier = "width";
            this.widthControl.addEventListener("change", this, false);
            this.widthControl.addEventListener("changing", this, false);

            
            //this._controlList[0].control.addEventListener("action", this._handleStageEvent.bind(this), false);
            //PropertiesPanelModule.PropertiesPanelBase.PIControlList["stageWidthHeightLock"] = this._controlList[0].control;

        }
    },

    draw: {
        value: function() {
            if(this._disablePosition) {
                this.leftPosition = 0;
                this.leftControl.enabled = false;
                this.topPosition = 0;
                this.topControl.enabled = false;
                this.leftLabel.classList.add("disabled");
                this.topLabel.classList.add("disabled");
            } else {
                this.leftControl.enabled = true;
                this.topControl.enabled = true;
                this.leftLabel.classList.remove("disabled");
                this.topLabel.classList.remove("disabled");
            }
        }
    },

    handleLeftChange: {
        value: function(event) {
            var prevPosition;

            if(!event.wasSetByCode) {
                if(this.savedPosition) prevPosition = [this.savedPosition + "px"];

                this.application.ninja.elementMediator.setProperty(this.application.ninja.selectedElements, "left", [this.leftControl.value + "px"] , "Change", "pi", prevPosition);
                this.savedPosition = null;
            }
        }
    },

    handleTopChange: {
        value: function(event) {
            var prevPosition;

            if(!event.wasSetByCode) {
                if(this.savedPosition) prevPosition = [this.savedPosition + "px"];

                this.application.ninja.elementMediator.setProperty(this.application.ninja.selectedElements, "top", [this.topControl.value + "px"] , "Change", "pi", prevPosition);
                this.savedPosition = null;
            }
        }
    },

    handleHeightChange: {
        value: function(event) {
            var prevPosition, items;

            if(!event.wasSetByCode) {
                if(this.savedPosition) prevPosition = [this.savedPosition + "px"];

                this.application.ninja.selectedElements.length ? items = this.application.ninja.selectedElements : items = [this.application.ninja.currentDocument.documentRoot];
                this.application.ninja.elementMediator.setProperty(items, "height", [this.heightControl.value + "px"] , "Change", "pi", prevPosition);
                this.savedPosition = null;
            }
        }
    },

    handleWidthChange: {
        value: function(event) {
            var prevPosition, items;

            if(!event.wasSetByCode) {
                if(this.savedPosition) prevPosition = [this.savedPosition + "px"];

                this.application.ninja.selectedElements.length ? items = this.application.ninja.selectedElements : items = [this.application.ninja.currentDocument.documentRoot];
                this.application.ninja.elementMediator.setProperty(items, "width", [this.widthControl.value + "px"] , "Change", "pi", prevPosition);
                this.savedPosition = null;
            }
        }
    },

    handleLeftChanging: {
        value: function(event) {
            if(!event.wasSetByCode) {
                if(!this.savedPosition) this.savedPosition = this.leftPosition;
                this.application.ninja.elementMediator.setProperty(this.application.ninja.selectedElements, "left", [this.leftControl.value + "px"] , "Changing", "pi");
            }

        }
    },

    handleTopChanging: {
        value: function(event) {
            if(!event.wasSetByCode) {
                if(!this.savedPosition) this.savedPosition = this.topPosition;
                this.application.ninja.elementMediator.setProperty(this.application.ninja.selectedElements, "top", [this.topControl.value + "px"] , "Changing", "pi");
            }

        }
    },

    handleHeightChanging: {
        value: function(event) {
            var items;
            if(!event.wasSetByCode) {

                if(this.bindButton.value) {
                    if(!this.savedPosition) this.savedPosition = this.heightSize;
                    var delta = this.heightControl.value - this.savedPosition;

                    var hwRatio = Math.round(Math.round(this.widthControl.value / this.savedPosition * 10) / 10);
                    var newWidth = this.widthControl.value + hwRatio * delta;

                    this.application.ninja.selectedElements.length ? items = this.application.ninja.selectedElements : items = [this.application.ninja.currentDocument.documentRoot];
                    this.widthControl.value = newWidth;
                    this.application.ninja.elementMediator.setProperty(items, "height", [this.heightControl.value + "px"] , "Changing", "pi");
                    this.application.ninja.elementMediator.setProperty(items, "width", [newWidth + "px"] , "Changing", "pi");
                } else {

                    if(!this.savedPosition) this.savedPosition = this.heightSize;

                    this.application.ninja.selectedElements.length ? items = this.application.ninja.selectedElements : items = [this.application.ninja.currentDocument.documentRoot];
                    this.application.ninja.elementMediator.setProperty(items, "height", [this.heightControl.value + "px"] , "Changing", "pi");
                }
            }
        }
    },

    handleWidthChanging: {
        value: function(event) {
            var items;
            if(!event.wasSetByCode) {
                if(!this.savedPosition) this.savedPosition = this.widthSize;
                this.application.ninja.selectedElements.length ? items = this.application.ninja.selectedElements : items = [this.application.ninja.currentDocument.documentRoot];
                this.application.ninja.elementMediator.setProperty(items, "width", [this.widthControl.value + "px"] , "Changing", "pi");
            }
        }
    }


});