-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
140 lines (128 loc) · 4.23 KB
/
plugin.js
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
/*******************************************************************************
* Copyright (c) 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/**
* @namespace The global container for eclipse APIs.
*/
var eclipse = eclipse || {};
/**
* A Service Provider is an object that implements a Service Type
* @class A Service Provider is an object that implements a Service Type
*/
eclipse.ServiceProvider = function() {
};
eclipse.ServiceProvider.prototype = {
_initialize: function(plugin) {
this._plugin = plugin;
},
dispatchEvent: function(eventType, eventData, serviceType, serviceId) {
var event = {
pluginURL: this.pluginURL,
eventType: eventType,
eventData: eventData,
serviceType: serviceType,
serviceId: serviceId
};
this._plugin._hubClient.publish("orion.plugin.response", {type: "event", result: event, error: null});
}
};
eclipse.ServiceProvider.extend = function(extender) {
var serviceProvider = new eclipse.ServiceProvider();
for (var name in extender) {
serviceProvider[name] = extender[name];
}
return serviceProvider;
};
/**
* A plugin is an object that is isolated in its own frame, and obtains and provides services
* via the asynchronous postMessage mechanism.
* @class A plugin is an object that is isolated in its own frame, and obtains and provides services
* via the asynchronous postMessage mechanism.
*/
eclipse.Plugin = function(pluginData, service) {
this._initialize(pluginData, service);
};
eclipse.Plugin.prototype = {
_initialize: function(pluginData, service) {
this.pluginURL = window.location.protocol + "//" + window.location.host + window.location.pathname;
this.pluginData = pluginData;
if (service !== undefined) {
this.service = service;
if (service["_initialize"] !== undefined) {
service._initialize(this);
}
}
},
start: function() {
this._hubClient = new OpenAjax.hub.IframeHubClient({
HubClient: {
onSecurityAlert: function(source, alertType) {}
}
});
var scope = this;
this._hubClient.connect(function(hubClient, success, error) {
if (success) {
scope._hubClient.subscribe("orion.plugin.request["+scope.pluginURL+"]", scope._handlePluginRequest, scope);
var result = {
pluginURL: scope.pluginURL,
pluginData: scope.pluginData
};
scope._hubClient.publish("orion.plugin.load", result);
}
});
},
stop: function() {
this._hubClient.disconnect();
},
_handlePluginRequest: function(topic, request, subscriberData) {
if (request.type === "metadata") {
this._getMetadata(request);
} else if (request.type === "service") {
this._callService(request);
}
},
_getMetadata: function(request) {
var result = {
pluginURL: this.pluginURL,
pluginData: this.pluginData
};
this._hubClient.publish("orion.plugin.response", {type: "metadata", id: request.id, result: result, error: null});
},
_callService: function(request) {
var method = request.msgData.method;
var params = request.msgData.params;
var result = null, error = null;
try {
if (this.service !== undefined) {
if (typeof this.service[method] === "function") {
result = this.service[method].apply(this, params);
}
} else if (typeof this[method] === "function") {
result = this[method].apply(this, params);
}
if (result === null) {
error = new Error("Unable to locate service method with name ["+method+"]");
}
} catch (e) {
error = e;
}
this._hubClient.publish("orion.plugin.response", {type: "service", id: request.id, result: result, error: error});
},
dispatchEvent: function(eventType, eventData, serviceType, serviceId) {
var event = {
pluginURL: this.pluginURL,
eventType: eventType,
eventData: eventData,
serviceType: serviceType,
serviceId: serviceId
};
this._hubClient.publish("org.eclipse.e4.plugin.PluginResponse", {type: "event", result: event, error: null});
}
};