-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqueryUImixin.js
150 lines (131 loc) · 5.12 KB
/
jqueryUImixin.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
141
142
143
144
145
146
147
148
149
150
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
JQ = Ember.Namespace.create();
// Create a new mixin for jQuery UI widgets using the Ember
// mixin syntax.
JQ.Widget = Ember.Mixin.create({
// When Ember creates the view's DOM element, it will call this
// method.
didInsertElement: function() {
// Make jQuery UI options available as Ember properties
var options = this._gatherOptions();
// Make sure that jQuery UI events trigger methods on this view.
this._gatherEvents(options);
// Create a new instance of the jQuery UI widget based on its `uiType`
// and the current element.
var ui = jQuery.ui[this.get('uiType')](options, this.get('element'));
// Save off the instance of the jQuery UI widget as the `ui` property
// on this Ember view.
this.set('ui', ui);
},
// When Ember tears down the view's DOM element, it will call
// this method.
willDestroyElement: function() {
var ui = this.get('ui');
if (ui) {
// Tear down any observers that were created to make jQuery UI
// options available as Ember properties.
var observers = this._observers;
for (var prop in observers) {
if (observers.hasOwnProperty(prop)) {
this.removeObserver(prop, observers[prop]);
}
}
ui._destroy();
}
},
// Each jQuery UI widget has a series of options that can be configured.
// For instance, to disable a button, you call
// `button.options('disabled', true)` in jQuery UI. To make this compatible
// with Ember bindings, any time the Ember property for a
// given jQuery UI option changes, we update the jQuery UI widget.
_gatherOptions: function() {
var uiOptions = this.get('uiOptions'),
options = {};
// The view can specify a list of jQuery UI options that should be treated
// as Ember properties.
uiOptions.forEach(function(key) {
options[key] = this.get(key);
// Set up an observer on the Ember property. When it changes,
// call jQuery UI's `setOption` method to reflect the property onto
// the jQuery UI widget.
var observer = function() {
var value = this.get(key);
this.get('ui')._setOption(key, value);
};
this.addObserver(key, observer);
// Insert the observer in a Hash so we can remove it later.
this._observers = this._observers || {};
this._observers[key] = observer;
}, this);
return options;
},
// Each jQuery UI widget has a number of custom events that they can
// trigger. For instance, the progressbar widget triggers a `complete`
// event when the progress bar finishes. Make these events behave like
// normal Ember events. For instance, a subclass of JQ.ProgressBar
// could implement the `complete` method to be notified when the jQuery
// UI widget triggered the event.
_gatherEvents: function(options) {
var uiEvents = this.get('uiEvents') || [],
self = this;
uiEvents.forEach(function(event) {
var callback;
event = "jq_" + event;
callback = self[event];
if (callback) {
// You can register a handler for a jQuery UI event by passing
// it in along with the creation options. Update the options hash
// to include any event callbacks.
options[event.replace("jq_", '')] = function(event, ui) {
callback.call(self, event, ui);
};
}
});
}
});
JQ.safeClone = function(event) {
var clone;
clone = $(this).clone();
clone.find('script[id^=metamorph]').remove();
clone.removeClass('ember-view');
//clone.removeClass('ui-draggable');
clone.find('*').each(function() {
var $this;
$this = $(this);
return $.each($this[0].attributes, function(index, attr) {
if (attr.name.indexOf('data-bindattr') === -1) {
return;
}
return $this.removeAttr(attr.name);
});
});
if (clone.attr('id') && clone.attr('id').indexOf('ember') !== -1) {
clone.removeAttr('id');
}
clone.find('[id^=ember]').removeAttr('id');
return clone;
}
// Create a new Ember view for the jQuery UI Button widget
JQ.Button = Ember.View.extend(JQ.Widget, {
uiType: 'button',
uiOptions: ['label', 'disabled'],
tagName: 'button'
});
JQ.Droppable = Ember.View.extend(JQ.Widget, {
uiType: 'droppable',
uiOptions: ['activeClass', 'hoverClass', 'accept'],
uiEvents: ['drop']
})
JQ.Draggable = Ember.View.extend(JQ.Widget, {
uiType: 'draggable',
uiOptions: ['appendTo', 'helper'],
uiEvents: []
})
JQ.Dialog = Ember.View.extend(JQ.Widget, {
uiType: 'dialog',
uiOptions: ['resizable','autoOpen','position','closeText','disabled','buttons', 'draggable', 'closeOnEscape','modal'],
uiEvents: ['create','open','close']
})