From e09e4a8e24e5daf899d24314d0ffc218bbb2a97d Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 1 Jun 2012 11:51:16 -0700 Subject: Fixing Google Map component Pulled in from components branch. --- node_modules/montage-google/map.reel/map.js | 82 ++++++++++++++++------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/node_modules/montage-google/map.reel/map.js b/node_modules/montage-google/map.reel/map.js index fc104df5..879a80d4 100644 --- a/node_modules/montage-google/map.reel/map.js +++ b/node_modules/montage-google/map.reel/map.js @@ -20,8 +20,9 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ didCreate: { value: function() { var self = this; + + this._latLng = this.defaultLatLng; window.initialize = function initialize() { - console.log('google maps api loaded'); self._mapLoaded = true; self._geoCoder = new google.maps.Geocoder(); self.needsDraw = true; @@ -32,7 +33,10 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ _geoCoder: {value: null}, // HTMLElement to load the Map into - mapEl: {value: null}, + mapEl: { + serializable: true, + value: null + }, _mapLoaded: { enumerable: false, @@ -49,9 +53,9 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ }, _latLng: { - value: this.defaultLatLng, - distinct: true + value: null }, + latLng: { get: function() { return this._latLng; @@ -67,6 +71,7 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ // {lat, lon} Or a String representing the location (eg: Paris, France) center: { + serializable: true, get: function() { return this._center; }, @@ -78,10 +83,12 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ if(String.isString(value)) { // geocode + self.LatLng = null; geocoder.geocode( { 'address': value}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var loc = results[0].geometry.location; self.latLng = {lat: loc.lat(), lng: loc.lng()}; + self.category = self._category; } else { console.log('Geocode was not successful : ' + status); } @@ -101,15 +108,17 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ }, category: { + serializable: true, get: function() { return this._category; }, set: function(value) { - console.log('category set: ' + value); if(value) { this._category = value; - this._getPlaces(this._category); - this.needsDraw = true; + if (this._mapLoaded && this.latLng) { + this._getPlaces(this._category); + this.needsDraw = true; + } } } }, @@ -159,12 +168,12 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ } var service = new google.maps.places.PlacesService(this._map); service.search(request, function(results, status) { - console.log('got places for ', self.category, status, results); if (status == google.maps.places.PlacesServiceStatus.OK) { self._places = results; } else { self._places = []; } + self.needsDraw = true; }); } }, @@ -252,35 +261,36 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ }; this._map = new window.google.maps.Map(this.mapEl, myOptions); - } else { - var map = this._map; - map.setZoom(10); - var latLng = new window.google.maps.LatLng(latLng.lat, latLng.lng); - map.setCenter(latLng); - var marker = new google.maps.Marker({ - map: map, - position: latLng - }); - - var places = this._places; - if(places && places.length > 0) { - map.setZoom(13); - this._removeAllMarkers(); - for (var i = 0; i < places.length; i++) { - this._createMarker(places[i]); - } - } - - if(this.traffic === true) { - this.trafficLayer = new google.maps.TrafficLayer(); - this.trafficLayer.setMap(map); - } else { - if(this.trafficLayer) { - this.trafficLayer.setMap(null); - } - } + this.center = this._center; + this.category = this._category; + } + + var map = this._map; + map.setZoom(10); + var latLng = new window.google.maps.LatLng(latLng.lat, latLng.lng); + map.setCenter(latLng); + var marker = new google.maps.Marker({ + map: map, + position: latLng + }); + + var places = this._places; + if(places && places.length > 0) { + map.setZoom(13); + this._removeAllMarkers(); + for (var i = 0; i < places.length; i++) { + this._createMarker(places[i]); + } } + if(this.traffic === true) { + this.trafficLayer = new google.maps.TrafficLayer(); + this.trafficLayer.setMap(map); + } else { + if(this.trafficLayer) { + this.trafficLayer.setMap(null); + } + } } //window.google.maps.event.addDomListener(window, 'load', initialize); @@ -294,4 +304,4 @@ var Map = exports.Map = Montage.create(Component, /** @lends module:"montage/ui/ serializer.set("center", this.center); } } -}); +}); \ No newline at end of file -- cgit v1.2.3 From 86837d69186bc4d1d6f54fc893c523de0972a0b9 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 1 Jun 2012 11:53:57 -0700 Subject: Creating template mediator class This is to simplify code, more to be changed and added. --- js/document/mediators/template.js | 637 +++++++++++++++++++++++++++++++++++- js/document/views/design.js | 6 +- js/mediators/io-mediator.js | 658 +------------------------------------- 3 files changed, 646 insertions(+), 655 deletions(-) diff --git a/js/document/mediators/template.js b/js/document/mediators/template.js index c5b45ba1..86055a1f 100755 --- a/js/document/mediators/template.js +++ b/js/document/mediators/template.js @@ -6,16 +6,647 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot //////////////////////////////////////////////////////////////////////// // -var Montage = require("montage/core/core").Montage, - Component = require("montage/ui/component").Component; +var Montage = require("montage/core/core").Montage, + Component = require("montage/ui/component").Component, + TemplateCreator = require("node_modules/tools/template/template-creator").TemplateCreator; //////////////////////////////////////////////////////////////////////// // exports.TemplateDocumentMediator = Montage.create(Component, { //////////////////////////////////////////////////////////////////// // hasTemplate: { - enumerable: false, value: false + }, + //////////////////////////////////////////////////////////////////// + // + getAppTemplatesUrlRegEx: { + value: function () { + var regex = new RegExp(chrome.extension.getURL(this.application.ninja.currentDocument.model.views.design.iframe.src.split(chrome.extension.getURL('/'))[1]).replace(/\//gi, '\\\/'), 'gi'); + return regex; + } + }, + //////////////////////////////////////////////////////////////////// + // + getDataDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(path+'data/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + getNinjaDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(this.getDataDirectory(path)+'ninja/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + getCanvasDirectory: { + value: function (path) { + //TODO: Implement user overwrite + return this._getUserDirectory(this.getNinjaDirectory(path)+'canvas/'); + } + }, + //////////////////////////////////////////////////////////////////// + // + _getUserDirectory: { + value: function (path) { + //Checking for data directory + var check = this.application.ninja.coreIoApi.fileExists({uri: path}), directory; + //Creating directory if doesn't exists + switch (check.status) { + case 204: //Exists + directory = path; + break; + case 404: //Doesn't exists + directory = this.application.ninja.coreIoApi.createDirectory({uri: path}); + //Checking for success + if (directory.status === 201) { + directory = path; + } else { + //Error + directory = null; + } + break; + default: //Error + directory = null; + break; + } + //Returning the path to the directory on disk (null for any error) + return directory; + } + }, + //////////////////////////////////////////////////////////////////// + // + parseHtmlToNinjaTemplate: { + value: function (html) { + //Creating temp object to mimic HTML + var doc = window.document.implementation.createHTMLDocument(), template; + //Setting content to temp + doc.getElementsByTagName('html')[0].innerHTML = html; + //Creating return object + return {head: doc.head.innerHTML, body: doc.body.innerHTML, document: doc}; + } + }, + //////////////////////////////////////////////////////////////////// + //TODO: Expand to allow more templates, clean up variables + parseNinjaTemplateToHtml: { + value: function (template, ninjaWrapper) { + //TODO: Improve reference for rootUrl + var regexRootUrl, + rootUrl = this.application.ninja.coreIoApi.rootUrl + escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1])), + mjsCreator = template.mjsTemplateCreator.create(), + mJsSerialization, + toremovetags = [], + presentNodes, + montageTemplate; + //Creating instance of template creator + montageTemplate = mjsCreator.initWithDocument(template.document); + //Setting up expression for parsing URLs + regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi'); + //Injecting head and body into old document + if (montageTemplate._ownerSerialization.length > 0) { + template.file.content.document.head.innerHTML = montageTemplate._document.head.innerHTML.replace(regexRootUrl, ''); + template.file.content.document.body.innerHTML = montageTemplate._document.body.innerHTML.replace(regexRootUrl, ''); + // + mJsSerialization = montageTemplate._ownerSerialization; + } else { + template.file.content.document.head.innerHTML = template.head.innerHTML.replace(regexRootUrl, ''); + template.file.content.document.body.innerHTML = template.body.innerHTML.replace(regexRootUrl, ''); + } + //Copying attributes to maintain same properties as the + for (var n in template.body.attributes) { + if (template.body.attributes[n].value) { + // + template.file.content.document.body.setAttribute(template.body.attributes[n].name, template.body.attributes[n].value); + } + } + + + + //TODO: Add attribute copying for and + + + + //Getting list of current nodes (Ninja DOM) + presentNodes = template.file.content.document.getElementsByTagName('*'); + //Looping through nodes to determine origin and removing if not inserted by Ninja + for (var n in presentNodes) { + if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') === null) { + toremovetags.push(presentNodes[n]); + } else if (presentNodes[n].getAttribute && presentNodes[n].getAttribute('data-ninja-node') !== null) { + //Removing attribute + presentNodes[n].removeAttribute('data-ninja-node'); + } + } + //Getting all CSS (style or link) tags + var styletags = template.file.content.document.getElementsByTagName('style'), + linktags = template.file.content.document.getElementsByTagName('link'), + njtemplatetags = template.file.content.document.querySelectorAll('[data-ninja-template]'); + + ////////////////////////////////////////////////// + //TODO: Remove, temp hack, this is to be fixed by Montage + var basetags = template.file.content.document.getElementsByTagName('base'); + for (var g in basetags) { + if (basetags[g].getAttribute && basetags[g].href && basetags[g].href.indexOf('chrome-extension://') !== -1) toremovetags.push(basetags[g]); + } + ////////////////////////////////////////////////// + + //Adding to tags to be removed form template + for (var f in njtemplatetags) { + if (njtemplatetags[f].getAttribute) toremovetags.push(njtemplatetags[f]); + } + //Getting styles tags to be removed from document + if (styletags.length) { + for (var j = 0; j < styletags.length; j++) { + if (styletags[j].getAttribute) { + if (styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) { + toremovetags.push(styletags[j]); + } else if (styletags[j].getAttribute('data-ninja-external-url')) { + toremovetags.push(styletags[j]); + } + } + } + } + //Removing styles tags from document + for (var h = 0; toremovetags[h]; h++) { + try { + //Checking head first + template.file.content.document.head.removeChild(toremovetags[h]); + } catch (e) { + + } + try { + //Checking body if not in head + template.file.content.document.body.removeChild(toremovetags[h]); + } catch (e) { + //Error, not found! + } + } + //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) + for (var l in linktags) { + if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll + for (var p = 0; toremovetags[p]; p++) { + if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) { + if (!toremovetags[p].getAttribute('data-ninja-disabled')) { + linktags[l].removeAttribute('disabled'); + } + } + } + } + } + //Checking for type of save: styles =