aboutsummaryrefslogtreecommitdiff
path: root/js/panels/css-panel/styles-view-delegate.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/panels/css-panel/styles-view-delegate.js')
-rw-r--r--js/panels/css-panel/styles-view-delegate.js288
1 files changed, 288 insertions, 0 deletions
diff --git a/js/panels/css-panel/styles-view-delegate.js b/js/panels/css-panel/styles-view-delegate.js
new file mode 100644
index 00000000..3f29ba27
--- /dev/null
+++ b/js/panels/css-panel/styles-view-delegate.js
@@ -0,0 +1,288 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6
7var Montage = require("montage/core/core").Montage,
8 Component = require("montage/ui/component").Component,
9 Keyboard = require("js/mediators/keyboard-mediator").Keyboard;
10
11exports.StylesViewMediator = Montage.create(Component, {
12 newClassPrefix : { value: "new-class" },
13 elementOutlineClass : { value: "nj-element-highlight" },
14
15 stylesController : {
16 get: function() {
17 return this.application.ninja.stylesController;
18 },
19 set: function(){
20 return;
21 }
22 },
23
24 ///// Selector Actions
25 //// -------------------------------------
26
27 ///// Show the targeted elements in the document by applying a class
28 ///// temporarily
29 handleSelectorHover : {
30 value: function(selector, direction) {
31 if(!selector) { return false; }
32
33 var elements = this.stylesController._activeDocument._document.querySelectorAll(selector),
34 method = (direction === "out") ? "remove" : "add";
35
36 Array.prototype.slice.call(elements).forEach(function(el) {
37 el.classList[method](this.elementOutlineClass);
38 }, this);
39 }
40 },
41
42 ///// Apply new selector to the rule
43 //// Verify that it applies to the selected elements
44 //// Remove rule if the selector is deleted
45 handleSelectorChange : {
46 value: function(rule, newSelector, ruleComponent) {
47 if(newSelector === "") {
48 ruleComponent.parentComponent.removeRule(ruleComponent);
49 this.stylesController.deleteRule(rule);
50 ///// Remove the hover style
51 this.handleSelectorHover(rule.selectorText, 'out');
52 this._dispatchChange();
53 return false;
54 }
55
56 rule.selectorText = newSelector;
57
58 ruleComponent.applied = this.ruleListContainer.displayedList.selection.every(function(el) {
59 return this._doesSelectorTargetElement(newSelector, el);
60 }, this);
61
62 this._dispatchChange();
63 }
64 },
65
66 ///// Returns true if the passed-in selector targets the passed-in element
67 _doesSelectorTargetElement : {
68 value: function doesSelectorTargetElement(selector, element) {
69 var doc = element.ownerDocument,
70 matchingEls = Array.prototype.slice.call(doc.querySelectorAll(selector));
71 return matchingEls.indexOf(element) !== -1;
72 }
73 },
74
75 ///// Style event handlers
76 //// -------------------------------------
77
78 ///// Enable/Disable Style when checkbox is clicked
79 handleStyleToggle : {
80 value: function(rule, enable, style) {
81 if(enable) {
82 this.stylesController.setStyle(rule, style.propertyText, style.browserValue, style.priority);
83 } else {
84 this.stylesController.deleteStyle(rule, style.propertyText);
85 }
86
87 this._dispatchChange();
88 }
89 },
90
91 handlePropertyStop: {
92 value: function(e, style) {
93 var key, nextFocus;
94
95 console.log("Handle Style Stop");
96
97 if(e._event.detail.type === 'keydown') {
98 key = e._event.detail.keyCode;
99
100 if(key === Keyboard.ENTER || key === Keyboard.TAB) {
101 e._event.detail.preventDefault();
102
103 if(e._event.detail.shiftKey) {
104 nextFocus = style.getSiblingStyle('prev') || style.getSiblingStyle('last');
105 nextFocus.valueField.start();
106 } else {
107 style.valueField.start();
108 }
109 }
110 }
111 }
112 },
113 handleValueStop: {
114 value: function(e, style) {
115 var key, nextFocus
116 console.log("Handle Value Stop");
117 console.log("Editing new style: ", style.editingNewStyle);
118
119 if(e._event.detail.type === 'keydown') {
120 key = e._event.detail.keyCode;
121
122 if(key === Keyboard.ENTER || key === Keyboard.TAB) {
123 e._event.detail.preventDefault();
124
125 if(e._event.detail.shiftKey) {
126 style.propertyField.start();
127 } else {
128 nextFocus = style.getSiblingStyle('next');
129 if(nextFocus) {
130 nextFocus.propertyField.start();
131 } else {
132 //style.parentComponent.parentComponent.addNewStyleAfter(style);
133 style.parentComponent.parentComponent.addNewStyle();
134 style.editingNewStyle = false;
135 setTimeout(function() {
136 style.getSiblingStyle('next').propertyField.start();
137 }, 50);
138 }
139 }
140 }
141 }
142 }
143 },
144 handlePropertyChange : {
145 value: function(rule, property, value, oldProperty, style) {
146 var browserValue;
147
148 if(style.editingNewStyle) {
149 return false;
150 }
151
152 if(property === '') {
153 style.remove();
154 this._dispatchChange(oldProperty, browserValue);
155 return false;
156 }
157
158 ///// Remove old property and add new one
159 this.stylesController.deleteStyle(rule, oldProperty);
160 browserValue = this.stylesController.setStyle(rule, property, value);
161
162 ///// Mark style as invalid if the browser doesn't accept it
163 style.invalid = (browserValue === null);
164
165 console.log("BrowserValue: ", browserValue, rule);
166
167 this._dispatchChange(property, browserValue);
168 }
169 },
170 handleValueChange : {
171 value: function(rule, property, value, style) {
172 var browserValue, units;
173
174 if(value === '') {
175 style.remove();
176 this._dispatchChange(property, browserValue);
177 return false;
178 }
179
180 ///// Auto-fill units if not provided and units
181 ///// not previously stored
182 units = style.getUnits(value);
183 if(style.units && units === null && parseInt(value)) {
184 value += style.units;
185 style.valueField.value = value;
186 } else if (value !== '0') {
187 style.units = units;
188 }
189
190 ///// update value
191 browserValue = this.stylesController.setStyle(rule, property, value);
192 style.browserValue = browserValue;
193
194 ///// Mark style as invalid if the browser doesn't accept it
195 style.invalid = (browserValue === null);
196
197 console.log("BrowserValue: ", browserValue, rule);
198
199 this._dispatchChange(property, browserValue);
200
201 if(style.editingNewStyle) {
202 style.parentComponent.parentComponent.addNewStyle();
203 style.editingNewStyle = false;
204 }
205 }
206 },
207
208 handlePaste : {
209 value: function(e) {
210 var text = document.execCommand('insertHTML', null, e._event.clipboardData.getData("Text")).trim();
211
212 if(text.matches(/([a-zA-Z-]+:[a-zA-Z-]+){,1}/)) {
213
214 }
215 }
216 },
217
218 /// Toolbar Button Actions
219 /// -----------------------
220
221 ///// Add rule button action
222 handleAddAction : {
223 value: function(e) {
224 var selector,
225 newRule,