OpenLayers学习11-格式Format控件Control

Format,没太看懂, 应该只是个父类.
先看个大概齐, 回头看了别的类 再返回头来再补充吧

Control类只定义了一堆接口, 如初始化, GC, 使能等等.
等看到他的子类估计才能用得上这些

一:Format

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
//Format感觉就是个父类, 下边的子类都使用他定义的接口
OpenLayers.Format = OpenLayers.Class({
//构造需要的选项
options: null,
//对象持有一个OpenLayers.Projection, 使用 reads 或者 writes 接口
externalProjection: null,
/**
* APIProperty: internalProjection
* {} When passed a externalProjection and
* internalProjection, the format will reproject the geometries it
* reads or writes. The internalProjection is the projection used by
* the geometries which are returned by read or which are passed into
* write. In order to reproject, a projection transformation function
* for the specified projections must be available. This support may be
* provided via proj4js or via a custom transformation function. See
* {} for more information on
* custom transformations.
*/
internalProjection: null,
//根据keepData配置, 存储data
data: null,
//保持最新的 read data的选项
keepData: false,
//扩展本对象, 更新选项
initialize: function(options) {
OpenLayers.Util.extend(this, options);
this.options = options;
},
//GC的接口, 此父类为空
destroy: function() {
},
//这个方法等着子类继承重写, 没继承的话直接报错
read: function(data) {
throw new Error('Read not implemented.');
},
//同上
write: function(object) {
throw new Error('Write not implemented.');
},
CLASS_NAME: "OpenLayers.Format"
});
//这个完全不知所云, 貌似为了拿到一个默认的Format父类
OpenLayers.Format.CSWGetRecords = function(options) {
options = OpenLayers.Util.applyDefaults(
options, OpenLayers.Format.CSWGetRecords.DEFAULTS
);
var cls = OpenLayers.Format.CSWGetRecords["v"+options.version.replace(/\./g, "_")];
if(!cls) {
throw "Unsupported CSWGetRecords version: " + options.version;
}
return new cls(options);
};
OpenLayers.Format.CSWGetRecords.DEFAULTS = {
"version": "2.0.2"
};

二: 控件类

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
OpenLayers.Control = OpenLayers.Class({
//分配的id
id: null,
//OpenLayers.Map, addControl的map对象
map: null,
//control的容器, 没有的话就是map
div: null,
//OpenLayers.Control.Panel用的type标记
type: null,
//是否允许被选择 默认不许, 因为这样不影响地图操作 比如拖拽
allowSelection: false,
//控件的css类
displayClass: "",
//tooltips字串
title: "",
//自动激活控件, 添加到地图后, 是否自动activate
autoActivate: false,
//当前的激活状态
active: null,
//控件handler的options对象
handlerOptions: null,
//控件的handler
handler: null,
//会自动添加到OpenLayers.Events.on, 构造函数里要传入此对象
eventListeners: null,
//给控件注册事件的
/*
* (code)
* control.events.register(type, obj, listener);
* (end)
*/
events: null,
//构造 var control = new OpenLayers.Control({div: myDiv});
initialize: function (options) {
//缩短类名: olControl
this.displayClass =
this.CLASS_NAME.replace("OpenLayers.", "ol").replace(/\./g, "");
OpenLayers.Util.extend(this, options);//合并options
this.events = new OpenLayers.Events(this);//自身持有new OpenLayers.Events(this)出来的事件对象
if(this.eventListeners instanceof Object) {
this.events.on(this.eventListeners);//如果传参里有eventListeners, 就用刚new出来的this.events对象on注册
}
//不指定id就olControl_自增id
if (this.id == null) {
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
}
},
//GC
destroy: function () {
//先gc事件, 再gc事件eventListeners
if(this.events) {
if(this.eventListeners) {
this.events.un(this.eventListeners);
}
this.events.destroy();
this.events = null;
}
this.eventListeners = null;
//gc持有的handler
if (this.handler) {
this.handler.destroy();
this.handler = null;
}
if(this.handlers) {
for(var key in this.handlers) {
if(this.handlers.hasOwnProperty(key) &&
typeof this.handlers[key].destroy == "function") {
this.handlers[key].destroy();
}
}
this.handlers = null;
}
//删除对map的引用
if (this.map) {
this.map.removeControl(this);
this.map = null;
}
//断开dom的引用
this.div = null;
},
//持有map
setMap: function(map) {
this.map = map;
if (this.handler) {
this.handler.setMap(map);
}
},
//渲染到指定px
draw: function (px) {
//没有div的话, 生成一个
if (this.div == null) {
this.div = OpenLayers.Util.createDiv(this.id);
this.div.className = this.displayClass;
if (!this.allowSelection) {
this.div.className += " olControlNoSelect";//类名
this.div.setAttribute("unselectable", "on", 0);//最后一个参数0查半天不知道干嘛的
this.div.onselectstart = OpenLayers.Function.False;
}
if (this.title != "") {
this.div.title = this.title;
}
}
if (px != null) {
//为了预防传参px改变或者被回收, 特意clone了一份
this.position = px.clone();
}
//画
this.moveTo(this.position);
return this.div;
},
//用style.left/top 绘制在持有的div上
moveTo: function (px) {
if ((px != null) && (this.div != null)) {
this.div.style.left = px.x + "px";
this.div.style.top = px.y + "px";
}
},
//激活控件
activate: function () {
//已激活的直接返回
if (this.active) {
return false;
}
//激活控件持有的handler
if (this.handler) {
this.handler.activate();
}
this.active = true;//状态位
if(this.map) {
//往viewPortDiv中加一个Active类
OpenLayers.Element.addClass(
this.map.viewPortDiv,
this.displayClass.replace(/ /g, "") + "Active"//去空格
);
}
this.events.triggerEvent("activate");//触发activate事件
return true;
},
//关闭控件使能
deactivate: function () {
//已激活的
if (this.active) {
if (this.handler) {
this.handler.deactivate();//handler先关掉
}
this.active = false;//状态
if(this.map) {
//删掉Active类
OpenLayers.Element.removeClass(
this.map.viewPortDiv,
this.displayClass.replace(/ /g, "") + "Active"
);
}
this.events.triggerEvent("deactivate");//触发"deactivate"事件
return true;
}
return false;
},
CLASS_NAME: "OpenLayers.Control"
});
//三类常量
OpenLayers.Control.TYPE_BUTTON = 1;
OpenLayers.Control.TYPE_TOGGLE = 2;
OpenLayers.Control.TYPE_TOOL = 3;