1 //
  2 // Copyright 2010, Tridium, Inc. All Rights Reserved.
  3 //
  4 
  5 /**
  6  * BajaScript Virtual Support.
  7  *
  8  * @author Gareth Johnson
  9  * @version 1.0.0.0
 10  */
 11 
 12 //JsLint options (see http://www.jslint.com )
 13 /*jslint rhino: true, onevar: false, plusplus: true, white: true, undef: false, nomen: false, eqeqeq: true, 
 14 bitwise: true, regexp: true, newcap: true, immed: true, strict: false, indent: 2, vars: true, continue: true */
 15 
 16 /*global setTimeout, clearTimeout, setInterval, clearInterval, document, window, loadTypes, bajaJsPrint, baja*/ 
 17 
 18 (function virt(baja) {
 19 
 20   // Use ECMAScript 5 Strict Mode
 21   "use strict";
 22   
 23   ////////////////////////////////////////////////////////////////
 24   // Virtual Component Space
 25   //////////////////////////////////////////////////////////////// 
 26   
 27   /**
 28    * @class Represents a baja:VirtualComponentSpace in BajaScript.
 29    *
 30    * @name baja.VirtualComponentSpace
 31    * @extends baja.BoxComponentSpace
 32    */  
 33   baja.VirtualComponentSpace = function (gateway) {
 34     baja.VirtualComponentSpace.$super.call(this, "virtual");
 35     this.$gateway = baja.strictArg(gateway);
 36   }.$extend(baja.BoxComponentSpace).registerType("baja:VirtualComponentSpace");
 37   
 38   
 39   /**
 40    * Return absolute ORD for the Component Space.
 41    *
 42    * @returns {baja.Ord}
 43    */
 44   baja.VirtualComponentSpace.prototype.getAbsoluteOrd = function () {
 45     return baja.Ord.make(this.$gateway.getNavOrd().toString() + "|virtual:");
 46   };
 47   
 48   /**
 49    * Private framework handler for a Component Space.
 50    * <p>
 51    * This is a private internal method for framework developers.
 52    *
 53    * @private
 54    */
 55   baja.VirtualComponentSpace.prototype.$fw = function (x, a, b, c) {    
 56     if (x === "unmount") {
 57       // When a Virtual Component is removed then reset the parent broker properties loaded flag 
 58       var parent = a.getParent();
 59       if (parent) {
 60         parent.$bPropsLoaded = false;
 61       }
 62     }
 63     baja.VirtualComponentSpace.$super.prototype.$fw.apply(this, arguments);
 64   };  
 65   
 66   ////////////////////////////////////////////////////////////////
 67   // Virtual Component
 68   //////////////////////////////////////////////////////////////// 
 69   
 70   /**
 71    * @class Represents a baja:VirtualComponent in BajaScript.
 72    *
 73    * @name baja.VirtualComponent
 74    * @extends baja.Component
 75    */  
 76   baja.VirtualComponent = function () {
 77     baja.VirtualComponent.$super.apply(this, arguments);
 78   }.$extend(baja.Component).registerType("baja:VirtualComponent");
 79   
 80   
 81   /**
 82    * Return the Nav ORD for the Virtual Component.
 83    *
 84    * @private
 85    *
 86    * @returns {baja.Ord} the Nav ORD or null if it's not mounted.
 87    */
 88   baja.VirtualComponent.prototype.getNavOrd = function () {
 89     if (!this.isMounted()) {
 90       return null;
 91     }
 92     var spaceOrd = this.$space.getAbsoluteOrd(),
 93         body = "/",
 94         slotPath = this.getSlotPath(),
 95         i;
 96     
 97     for (i = 0; i < slotPath.depth(); ++i) {
 98       if (i > 0) {
 99         body += "/";
100       }
101       body += baja.SlotPath.unescape(slotPath.nameAt(i));
102     }
103     
104     return baja.Ord.make(spaceOrd.toString() + body);
105   };
106   
107   ////////////////////////////////////////////////////////////////
108   // Virtual Gateway
109   //////////////////////////////////////////////////////////////// 
110   
111   /**
112    * @class Represents a baja:VirtualGateway in BajaScript.
113    *
114    * @name baja.VirtualGateway
115    * @extends baja.Component
116    */  
117   baja.VirtualGateway = function () {
118     baja.VirtualGateway.$super.apply(this, arguments);
119     this.$virtualSpace = null;
120   }.$extend(baja.Component).registerType("baja:VirtualGateway");
121   
122   function fwLoadVirtualSpace(gateway) {
123     var cb = new baja.comm.Callback();
124   
125     if (gateway.$virtualSpace) {
126       cb.ok();
127       return;
128     }
129     
130     try {   
131       // Create the Virtual Space and initialize it
132       gateway.$virtualSpace = new baja.VirtualComponentSpace(gateway);
133       gateway.$virtualSpace.init(cb.getBatch());
134       
135       // TODO: Currently made synchronously on purpose. We don't want to accidently create this twice
136       // so this is the safest option for now!
137       cb.commitSync();
138     }
139     catch (err) {
140       cb.fail(err);
141     }
142   }
143   
144   /**
145    * Internal Framework Method.
146    *
147    * @private
148    *
149    * @see baja.Component#$fw
150    */
151   baja.VirtualGateway.prototype.$fw = function (x) {   
152     if (x === "loadVirtualSpace") {   
153       // Load the virtual space    
154       fwLoadVirtualSpace(this);
155     }
156     else {
157       return baja.VirtualGateway.$super.prototype.$fw.apply(this, arguments);
158     }
159   };
160   
161   /**
162    * Return the Virtual Space for the Gateway. 
163    *
164    * @returns {baja.VirtualComponentSpace}
165    */
166   baja.VirtualGateway.prototype.getVirtualSpace = function () {
167     if (this.$virtualSpace === null) {
168       // TODO: This is currently mounted synchronously as we don't want to accidently mount multiple Component Spaces
169       this.$fw("loadVirtualSpace");
170     }
171   
172     return this.$virtualSpace;
173   };
174   
175   /**
176    * Load the Slots for the VirtualGateway.
177    * 
178    * @see baja.Component#loadSlots
179    */
180   baja.VirtualGateway.prototype.loadSlots = function (obj) {
181     // Ensure the Virtual Space is all loaded when this happens
182     this.getVirtualSpace();
183     
184     // Call super
185     baja.VirtualGateway.$super.prototype.loadSlots.apply(this, arguments);
186   };
187   
188   /**
189    * Access the Nav Children.
190    * 
191    * @see baja.Component#getNavChildren
192    */
193   baja.VirtualGateway.prototype.getNavChildren = function (obj) {
194     // Access the Nav Children of the Virtual Space
195     var space = this.getVirtualSpace();
196     
197     if (space) {
198       space.getRootComponent().getNavChildren(obj);
199     }
200     else {
201       baja.VirtualGateway.$super.prototype.getNavChildren(obj);
202     }
203   };
204   
205   ////////////////////////////////////////////////////////////////
206   // Virtual Path
207   //////////////////////////////////////////////////////////////// 
208   
209   /**
210    * @class Resolves Virtual Slot Paths.
211    *
212    * @name baja.VirtualPath
213    * @extends baja.SlotPath
214    */  
215   baja.VirtualPath = function () {
216     baja.VirtualPath.$super.apply(this, arguments);
217   }.$extend(new baja.SlotPath(""));
218   
219   /**
220    * Make a Slot Path.
221    *
222    * @private
223    *
224    * @param {Object} query  the ORD Query used in resolving the ORD.
225    * @returns {baja.VirtualPath} the new Slot Path.
226    */
227   baja.VirtualPath.prototype.makeSlotPath = function (query) {
228     return new baja.VirtualPath(query);
229   };
230   
231   /**
232    * Return whether the specified path name is valid.
233    * 
234    * @param {String} pathName the path name to validate.
235    *
236    * @returns {Boolean} true if the slot name is valid.
237    */ 
238   baja.VirtualPath.prototype.isValidPathName = function (pathName) {
239     return baja.VirtualPath.isValidName(pathName);
240   };
241   
242   /**
243    * Return whether the slot name is valid.
244    *
245    * @param {String} nm the name to validate.
246    *
247    * @returns {Boolean} true if the slot name is valid.
248    */ 
249   baja.VirtualPath.isValidName = function (nm) {
250     return (/^[^\/\|\$\:]+$/).test(nm); 
251   };
252   
253   /**
254    * Return the scheme name for the SlotPath.
255    *
256    * @returns {String}
257    */
258   baja.VirtualPath.prototype.getSchemeName = function () {
259     return "virtual";
260   };
261   
262   /**
263    * Return the scheme used with this SlotPath.
264    *
265    * @returns {baja.OrdScheme}
266    */
267   baja.VirtualPath.prototype.getScheme = function () {
268     return baja.VirtualScheme.DEFAULT;
269   };
270     
271   ////////////////////////////////////////////////////////////////
272   // Virtual ORD Scheme
273   //////////////////////////////////////////////////////////////// 
274     
275   /**
276    * @class Virtual Slot Scheme for Virtual Components.
277    *
278    * @name baja.VirtualScheme
279    * @extends baja.SlotScheme
280    */  
281   baja.VirtualScheme = function () {
282     baja.VirtualScheme.$super.apply(this, arguments);
283   }.$extend(baja.SlotScheme);
284   
285   /**
286    * Default Virtual ORD Scheme instance.
287    * @private
288    */
289   baja.VirtualScheme.DEFAULT = new baja.VirtualScheme();
290   
291   // Register Type
292   baja.VirtualScheme.registerType("baja:VirtualScheme"); 
293   
294   /**
295    * Return an ORD Query for the scheme.
296    *
297    * @returns {Object}
298    */
299   baja.VirtualScheme.prototype.parse = function (schemeName, body) {
300     return new baja.VirtualPath(body);
301   };
302       
303 }(baja));