1 Lyrix = {}; // Our own little namespace.
3 // We can pass values from the server this way
4 // I like this syntax better
5 $P = function(element) {
6 return $(element).innerHTML;
9 // ===============================================
10 // = Behavior proxies to Script.aculo.us objects =
11 // ===============================================
12 Lyrix.Draggable = Behavior.create({
13 initialize: function(options) {
14 this.options = Object.extend({
17 this.draggable = new Draggable(this.element, this.options);
21 Lyrix.Droppable = Behavior.create({
22 initialize: function(options) {
23 this.options = (options || {});
24 Droppables.add(this.element, this.options);
28 Lyrix.Sortable = Behavior.create({
29 initialize: function(options) {
30 this.options = Object.extend({
33 Sortable.create(this.element, this.options);
37 Lyrix.InPlaceEditor = Behavior.create({
38 initialize: function(options) {
39 this.options = Object.extend({
40 urlBuilder: this._makeRequestURL,
41 ajaxOptions: {method: 'put'}
44 this.url = this.options.urlBuilder(this.element);
45 this.options.urlBuilder = null;
47 new Ajax.InPlaceEditor(this.element, this.url, this.options);
50 _makeRequestURL: function(element) {
51 split_id = element.id.split('_');
53 params = {object: split_id[0], method: split_id[1], id: split_id[2]};
54 return '/' + params.object + 's/set_' + params.object + '_' + params.method + '/' + params.id;
58 // ====================
59 // = Custom behaviors =
60 // ====================
61 Lyrix.DeleteLink = Behavior.create({
62 initialize: function() {
64 style: 'display:none',
66 action: this.element.href
73 this.element.appendChild(this.form);
75 onclick: function(e) {
81 // =================================
82 // = Behaviors for the application =
83 // =================================
85 // Add some magical in-place-editors
86 'span.in_place_editor_field': Lyrix.InPlaceEditor(),
88 // Delete things using a hidden form
89 'a.deletable': Lyrix.DeleteLink,
91 // Allow songs to be dragged back and forth
92 '.song, .usage': Lyrix.Draggable({handle: 'draggable'}),
94 // Make the songs in a show sortable and
95 // allow songs from the sidebar to be dropped in
96 '#songs_list': function() {
100 onUpdate: function(element) {
101 new Ajax.Request('/shows/' + $P('show_id') + ';reorder', {
103 parameters: Sortable.serialize(element)
108 Lyrix.Droppable.attach(this, {
110 onDrop: function(element) {
111 new Ajax.Request('/usages?show_id=' + $P('show_id'), {
112 parameters: 'id=' + encodeURIComponent(element.id.gsub('song_', '')),
113 onComplete: function() {
114 Sortable.create('songs_list', sortableOptions);
120 Lyrix.Sortable.attach(this, sortableOptions);
123 // Allow songs from a show to be dropped out of the show
124 '#side_songs_list': Lyrix.Droppable({
126 onDrop: function(element) {
127 id = encodeURIComponent(element.id.gsub('usage_', ''));
128 new Ajax.Request('/usages/' + id, {