comparison js/dnd-library.js @ 30:2d49adbd8992

Start of applying a last layer of polish: starting the process of cleaning up the library code.
author David Eads <eads@chicagotech.org>
date Tue, 17 Mar 2009 21:29:00 -0500
parents 7d6bf2dca269
children 767ebf925654
comparison
equal deleted inserted replaced
29:37ca57016cbe 30:2d49adbd8992
18 // Custom selectors 18 // Custom selectors
19 $.extend($.expr[":"], { 19 $.extend($.expr[":"], {
20 'empty' : function(a, i, m) { 20 'empty' : function(a, i, m) {
21 return !$(a).filter(function(i) { 21 return !$(a).filter(function(i) {
22 return !$(this).is('br'); 22 return !$(this).is('br');
23 }).length && !$.trim(a.textContent || a.innerText||$(a).text()||""); 23 }).length && !$.trim(a.textContent || a.innerText||$(a).text() || "");
24 } 24 }
25 }); 25 });
26 }) (jQuery); 26 }) (jQuery);
27 27
28 /**
29 * Initialize and load drag and drop library and pass off rendering and
30 * behavior attachment.
31 */
28 Drupal.behaviors.dndLibrary = function(context) { 32 Drupal.behaviors.dndLibrary = function(context) {
29 $('.dnd-library-wrapper', context).each(function() { 33 $('.dnd-library-wrapper', context).each(function() {
30 var $this = $(this); 34 var $this = $(this);
31 35
32 // This is a bad hack to lop off '-dnd-library' from the id to get the editor name 36 // This is a bad hack to lop off '-dnd-library' from the id to get the editor name
33 var $editor = $('#' + this.id.slice(0, -12)); 37 var $editor = $('#' + this.id.slice(0, -12));
34 38
35 var settings = Drupal.settings.dndEnabledLibraries[$editor.get(0).id]; 39 // Set up some initial settings for BeautyTips
36 var bt_settings = $.extend({ 40 var settings = Drupal.settings.dndEnabledLibraries[$editor.get(0).id] = $.extend({
37 'trigger': 'none', 41 'trigger': 'none',
38 'width': 375, 42 'width': 375,
39 'spikeLength': 7, 43 'spikeLength': 7,
40 'spikeGirth': 9, 44 'spikeGirth': 9,
41 'corner-radius' : 3, 45 'corner-radius' : 3,
42 'strokeWidth': 1, 46 'strokeWidth': 1,
43 'fill': '#ffd', 47 'fill': '#ffd',
44 'strokeStyle': '#555' 48 'strokeStyle': '#555'
45 }, settings.bt_settings); 49 }, Drupal.settings.dndEnabledLibraries[$editor.get(0).id]);
46 50
47 // Bind Drag and Drop plugin invocation to events emanating from Wysiwyg 51 // Bind Drag and Drop plugin invocation to events emanating from Wysiwyg
48 $editor.bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library); 52 $editor.bind('wysiwygAttach', Drupal.behaviors.dndLibrary.attach_library);
49 $editor.bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library); 53 $editor.bind('wysiwygDetach', Drupal.behaviors.dndLibrary.detach_library);
50 54
51 // Add preview behavior to editor items (thanks, BeautyTips!) 55 // Set up empty objects to keep track of things
52 $('.editor-item', $this).each(function () { 56 Drupal.settings.dndEditorRepresentations = {};
53 $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], bt_settings); 57 Drupal.settings.dndLibraryPreviews = {};
54 var hover_opts = $.extend({ 58
55 'interval': 500, 59 // Populate
56 'timeout' : 0, 60 $.getJSON(Drupal.settings.basePath + settings.url, function(data) {
57 'over': function() { 61 Drupal.behaviors.dndLibrary.renderLibrary.call($this.get(0), data, $editor);
58 var $this = $(this);
59 this.btOn();
60 // Remove the preview once dragging of any image has commenced
61 $('img', $this).bind('drag', function(e) {
62 $this.btOff();
63 });
64 $('img', $this).bind('click', function(e) {
65 $this.btOff();
66 });
67 },
68 'out': function() { this.btOff(); }
69 }, settings.libraryHoverIntentOpts);
70 $(this).hoverIntent(hover_opts);
71 }); 62 });
72 63
73 // Simple AJAX pager
74 $('.pager a', $this).click(function() {
75 $.getJSON(this.href, function(data) {
76 Drupal.behaviors.dndLibrary.refreshLibrary.call($this.get(0), data, $editor);
77 });
78 Drupal.behaviors.dndLibrary();
79 return false;
80 });
81
82 $('.view-filters input[type=submit]', $this).click(function() {
83 $(this).ajaxSubmit({
84 'url' : Drupal.settings.basePath + Drupal.settings.dndEnabledLibraries[$editor.get(0).id].url,
85 'dataType': 'json',
86 'success': function(responsetext, statustext) {
87 Drupal.behaviors.dndLibrary.refreshLibrary.call($this.get(0), responsetext, $editor);
88 Drupal.behaviors.dndLibrary();
89 }
90 });
91 return false;
92 });
93
94 // Preload images in editor representations
95 var cached = $.data($editor, 'dnd_preload') || {};
96 for (editor_id in Drupal.settings.dndEditorRepresentations) {
97 if (!cached[editor_id]) {
98 $representation = $(Drupal.settings.dndEditorRepresentations[editor_id].body);
99 if ($representation.is('img') && $representation.get(0).src) {
100 $representation.attr('src', $representation.get(0).src);
101 } else {
102 $('img', $representation).each(function() {
103 this.attr('src', this.src);
104 });
105 }
106 }
107 }
108 $.data($editor, 'dnd_preload', cached);
109 }); 64 });
110 } 65 }
111 66
112 Drupal.behaviors.dndLibrary.refreshLibrary = function(data, editor) { 67 Drupal.behaviors.dndLibrary.renderLibrary = function(data, editor) {
113 $this = $(this); 68 $this = $(this);
114 69
115 $('.header', $this).html(data.header); 70 $this.html(data.library);
116 $('.library', $this).html(data.library); 71
117 $('.footer', $this).html(data.footer); 72 var settings = Drupal.settings.dndEnabledLibraries[editor.get(0).id];
118
119 var params = Drupal.wysiwyg.instances[editor.get(0).id]; 73 var params = Drupal.wysiwyg.instances[editor.get(0).id];
74
120 editor.trigger('wysiwygDetach', params); 75 editor.trigger('wysiwygDetach', params);
121 editor.trigger('wysiwygAttach', params); 76 editor.trigger('wysiwygAttach', params);
122 77
123 for (editor_id in data.editor_representations) { 78 for (editor_id in data.editor_representations) {
124 Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id]; 79 Drupal.settings.dndEditorRepresentations[editor_id] = data.editor_representations[editor_id];
125 } 80 }
126 for (preview_id in data.library_previews) { 81 for (preview_id in data.library_previews) {
127 Drupal.settings.dndLibraryPreviews[preview_id] = data.library_previews[preview_id]; 82 Drupal.settings.dndLibraryPreviews[preview_id] = data.library_previews[preview_id];
128 } 83 }
129 84
130 // Reattach behaviors 85 // Add preview behavior to editor items (thanks, BeautyTips!)
131 Drupal.behaviors.dndLibrary(); 86 $('.editor-item', $this).each(function () {
87 $(this).bt(Drupal.settings.dndLibraryPreviews[this.id], settings.bt_settings);
88 var hover_opts = $.extend({
89 'interval': 500,
90 'timeout' : 0,
91 'over': function() {
92 var $this = $(this);
93 this.btOn();
94 // Remove the preview once dragging of any image has commenced
95 $('img', $this).bind('drag', function(e) {
96 $this.btOff();
97 });
98 $('img', $this).bind('click', function(e) {
99 $this.btOff();
100 });
101 },
102 'out': function() { this.btOff(); }
103 }, settings.libraryHoverIntentOpts);
104 $(this).hoverIntent(hover_opts);
105 });
106
107 // Preload images in editor representations
108 var cached = $.data($(editor), 'dnd_preload') || {};
109 /*for (editor_id in Drupal.settings.dndEditorRepresentations) {
110 if (!cached[editor_id]) {
111 $representation = $(Drupal.settings.dndEditorRepresentations[editor_id].body);
112 if ($representation.is('img') && $representation.get(0).src) {
113 $representation.attr('src', $representation.get(0).src);
114 } else {
115 $('img', $representation).each(function() {
116 this.attr('src', this.src);
117 });
118 }
119 }
120 }
121 $.data($(editor), 'dnd_preload', cached);*/
122
132 } 123 }
133 124
134 125
135 // Dynamically compose a callback based on the editor name 126 // Dynamically compose a callback based on the editor name
136 Drupal.behaviors.dndLibrary.attach_library = function(e, data) { 127 Drupal.behaviors.dndLibrary.attach_library = function(e, data) {