tizen beta release
[profile/ivi/webkit-efl.git] / debian / tmp / usr / share / ewebkit-0 / webinspector / HandlerRegistry.js
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.Object}
34  */
35 WebInspector.HandlerRegistry = function(setting)
36 {
37     WebInspector.Object.call(this);
38     this._handlers = {};
39     this._setting = setting;
40     this._activeHandler = this._setting.get();
41 }
42
43 WebInspector.HandlerRegistry.prototype = {
44     get handlerNames()
45     {
46         return Object.getOwnPropertyNames(this._handlers);
47     },
48
49     get activeHandler()
50     {
51         return this._activeHandler;
52     },
53
54     set activeHandler(value)
55     {
56         this._activeHandler = value;
57         this._setting.set(value);
58     },
59
60     /**
61      * @param {Object} data
62      */
63     dispatch: function(data)
64     {
65         this.dispatchToHandler(this._activeHandler, data);
66     },
67
68     /**
69      * @param {string} name
70      * @param {Object} data
71      */
72     dispatchToHandler: function(name, data)
73     {
74         var handler = this._handlers[name];
75         var result = handler && handler(data);
76         return !!result;
77     },
78
79     registerHandler: function(name, handler)
80     {
81         this._handlers[name] = handler;
82         this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
83     },
84
85     unregisterHandler: function(name)
86     {
87         delete this._handlers[name];
88         this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
89     }
90 }
91
92 WebInspector.HandlerRegistry.EventTypes = {
93     HandlersUpdated: "HandlersUpdated"
94 }
95
96 WebInspector.HandlerRegistry.prototype.__proto__ = WebInspector.Object.prototype;
97
98 /**
99  * @constructor
100  */
101 WebInspector.HandlerSelector = function(handlerRegistry)
102 {
103     this._handlerRegistry = handlerRegistry;
104     this.element = document.createElement("select");
105     this.element.addEventListener("change", this._onChange.bind(this), false);
106     this._update();
107     this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
108 }
109
110 WebInspector.HandlerSelector.prototype =
111 {
112     _update: function()
113     {
114         this.element.removeChildren();
115         var names = this._handlerRegistry.handlerNames;
116         var activeHandler = this._handlerRegistry.activeHandler;
117
118         for (var i = 0; i < names.length; ++i) {
119             var option = document.createElement("option");
120             option.textContent = names[i];
121             option.selected = activeHandler === names[i];
122             this.element.appendChild(option);
123         }
124         this.element.disabled = names.length <= 1;
125     },
126
127     _onChange: function(event)
128     {
129         var value = event.target.value;
130         this._handlerRegistry.activeHandler = value;
131     }
132 }