Skip to content

Instantly share code, notes, and snippets.

@supersheep
Created January 26, 2013 13:13
Show Gist options
  • Save supersheep/4642313 to your computer and use it in GitHub Desktop.
Save supersheep/4642313 to your computer and use it in GitHub Desktop.
iframeMapAccessor
(function(win,DP){
// 适配mootools与neuron框架创建元素方法
function create(tag){
if(DP.DOM){
return DP.DOM.create(tag).el(0)
}else{
return new Element(tag);
}
}
function Map(elem,options,callback){
this.iframe = null;
this.$iframe = null;
this.loaded = false;
this.realMapInstance = null;
this.funcQueue = [];
this.loadIframe();
this.init(elem,options.pois,options,callback);
}
Map.prototype.runAfterLoaded = function(func){
if(this.loaded){
func.apply(this,arguments);
}else{
this.funcQueue.push(function(){
func.apply(this,arguments);
});
}
}
Map.prototype.loadIframe = function(){
var self = this,
funcQueue = self.funcQueue;
self.iframe = create("iframe");
self.$iframe = $(self.iframe);
self.$iframe.css("border","none");
self.iframe.onload = function(){
var func;
self.loaded = true;
while(funcQueue.length){
funcQueue.shift()();
}
}
self.iframe.src="iframe.html";
}
Map.prototype.init = function(elem,pois,options,callback){
var self = this,
iframe = self.iframe,
$iframe = self.$iframe,
size = {
"width":options.css && options.css.width,
"height":options.css && options.css.height
};
self.$iframe.inject(elem);
self.$iframe.css(size);
elem.css(size);
self.runAfterLoaded(function(){
var mapcontainer = iframe.contentWindow.document.getElementById("map");
realMapInstance = iframe.contentWindow.init(mapcontainer,pois,options);
["DPMap","DPMarker","DPLatLng","DPIcon","DPEvent","DPLabel"].forEach(function(key){
win[key] = iframe.contentWindow[key];
});
callback(realMapInstance,mapcontainer);
});
}
win.Map = Map;
})(window,DP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment