5936d383c2931778be513851ac3ad35b79e19aa7
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function( $, joey, app ) {
17
18         var ui = app.ns("ui");
19         var ux = app.ns("ux");
20
21         ui.AbstractWidget = ux.Observable.extend({
22                 defaults : {
23                         id: null     // the id of the widget
24                 },
25
26                 el: null,       // this is the jquery wrapped dom element(s) that is the root of the widget
27
28                 init: function() {
29                         this._super();
30                         for(var prop in this) {       // automatically bind all the event handlers
31                                 if(prop.contains("_handler")) {
32                                         this[prop] = this[prop].bind(this);
33                                 }
34                         }
35                 },
36
37                 id: function(suffix) {
38                         return this.config.id ? (this.config.id + (suffix ? "-" + suffix : "")) : undefined;
39                 },
40
41                 attach: function( parent, method ) {
42                         if( parent ) {
43                                 this.el[ method || "appendTo"]( parent );
44                         }
45                         this.fire("attached", this );
46                         return this;
47                 },
48
49                 remove: function() {
50                         this.el.remove();
51                         this.fire("removed", this );
52                         this.removeAllObservers();
53                         this.el = null;
54                         return this;
55                 }
56         });
57
58         joey.plugins.push( function( obj ) {
59                 if( obj instanceof ui.AbstractWidget ) {
60                         return obj.el[0];
61                 }
62         });
63
64 })( this.jQuery, this.joey, this.app );