Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
/*
2
  Downloadify: Client Side File Creation
3
  JavaScript + Flash Library
4
 
5
  Version: 0.2
6
 
7
  Copyright (c) 2009 Douglas C. Neiner
8
 
9
  Permission is hereby granted, free of charge, to any person obtaining a copy
10
  of this software and associated documentation files (the "Software"), to deal
11
  in the Software without restriction, including without limitation the rights
12
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
  copies of the Software, and to permit persons to whom the Software is
14
  furnished to do so, subject to the following conditions:
15
 
16
  The above copyright notice and this permission notice shall be included in
17
  all copies or substantial portions of the Software.
18
 
19
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
  THE SOFTWARE.
26
*/
27
 
28
(function(){
29
  Downloadify = window.Downloadify = {
30
    queue: {},
31
    uid: new Date().getTime(),
32
    getTextForSave: function(queue){
33
      var obj = Downloadify.queue[queue];
34
      if(obj) return obj.getData();
35
      return "";
36
    },
37
    getFileNameForSave: function(queue){
38
      var obj = Downloadify.queue[queue];
39
      if(obj) return obj.getFilename();
40
      return "";
41
    },
42
    getDataTypeForSave: function(queue){
43
      var obj = Downloadify.queue[queue];
44
      if(obj) return obj.getDataType();
45
      return "";
46
    },
47
    saveComplete: function(queue){
48
      var obj = Downloadify.queue[queue];
49
      if(obj) obj.complete();
50
      return true;
51
    },
52
    saveCancel: function(queue){
53
      var obj = Downloadify.queue[queue];
54
      if(obj) obj.cancel();
55
      return true;
56
    },
57
    saveError: function(queue){
58
      var obj = Downloadify.queue[queue];
59
      if(obj) obj.error();
60
      return true;
61
    },
62
    addToQueue: function(container){
63
      Downloadify.queue[container.queue_name] = container;
64
    },
65
    // Concept adapted from: http://tinyurl.com/yzsyfto
66
    // SWF object runs off of ID's, so this is the good way to get a unique ID
67
    getUID: function(el){
68
      if(el.id == "") el.id = 'downloadify_' + Downloadify.uid++;
69
      return el.id;
70
    }
71
  };
72
 
73
  Downloadify.create = function( idOrDOM, options ){
74
    var el = (typeof(idOrDOM) == "string" ? document.getElementById(idOrDOM) : idOrDOM );
75
    return new Downloadify.Container(el, options);
76
  };
77
 
78
  Downloadify.Container = function(el, options){
79
    var base = this;
80
 
81
    base.el = el;
82
    base.enabled = true;
83
    base.dataCallback = null;
84
    base.filenameCallback = null;
85
    base.data = null;
86
    base.filename = null;
87
 
88
     var init = function(){
89
       base.options = options;
90
 
91
      if( !base.options.append ) base.el.innerHTML = "";
92
 
93
      base.flashContainer = document.createElement('span');
94
      base.el.appendChild(base.flashContainer);
95
 
96
      base.queue_name = Downloadify.getUID( base.flashContainer );
97
 
98
      if( typeof(base.options.filename) === "function" )
99
         base.filenameCallback = base.options.filename;
100
      else if (base.options.filename)
101
        base.filename = base.options.filename;
102
 
103
      if( typeof(base.options.data) === "function" )
104
        base.dataCallback = base.options.data;
105
      else if (base.options.data)
106
        base.data = base.options.data;
107
 
108
 
109
      var flashVars = {
110
        queue_name: base.queue_name,
111
        width: base.options.width,
112
        height: base.options.height
113
      };
114
 
115
      var params = {
116
        allowScriptAccess: 'always'
117
      };
118
 
119
      var attributes = {
120
        id: base.flashContainer.id,
121
        name: base.flashContainer.id
122
      };
123
 
124
      if(base.options.enabled === false) base.enabled = false;
125
 
126
      if(base.options.transparent === true) params.wmode = "transparent";
127
 
128
      if(base.options.downloadImage) flashVars.downloadImage = base.options.downloadImage;
129
 
130
      swfobject.embedSWF(base.options.swf, base.flashContainer.id, base.options.width, base.options.height, "10", null, flashVars, params, attributes );
131
 
132
      Downloadify.addToQueue( base );
133
     };
134
 
135
    base.enable = function(){
136
      var swf = document.getElementById(base.flashContainer.id);
137
      swf.setEnabled(true);
138
      base.enabled = true;
139
    };
140
 
141
    base.disable = function(){
142
      var swf = document.getElementById(base.flashContainer.id);
143
      swf.setEnabled(false);
144
      base.enabled = false;
145
    };
146
 
147
    base.getData = function(){
148
      if( !base.enabled ) return "";
149
      if( base.dataCallback ) return base.dataCallback();
150
      else if (base.data) return base.data;
151
      else return "";
152
    };
153
 
154
    base.getFilename = function(){
155
      if( base.filenameCallback ) return base.filenameCallback();
156
      else if (base.filename) return base.filename;
157
      else return "";
158
    };
159
 
160
    base.getDataType = function(){
161
      if (base.options.dataType) return base.options.dataType;
162
      return "string";
163
    };
164
 
165
    base.complete = function(){
166
      if( typeof(base.options.onComplete) === "function" ) base.options.onComplete();
167
    };
168
 
169
    base.cancel = function(){
170
      if( typeof(base.options.onCancel) === "function" ) base.options.onCancel();
171
    };
172
 
173
    base.error = function(){
174
      if( typeof(base.options.onError) === "function" ) base.options.onError();
175
    };
176
 
177
    init();
178
  };
179
 
180
  Downloadify.defaultOptions = {
181
    swf: 'media/downloadify.swf',
182
    downloadImage: 'images/download.png',
183
    width: 100,
184
    height: 30,
185
    transparent: true,
186
    append: false,
187
    dataType: "string"
188
  };
189
})();
190
 
191
// Support for jQuery
192
if(typeof(jQuery) != "undefined"){
193
  (function($){
194
    $.fn.downloadify = function(options){
195
      return this.each(function(){
196
        options = $.extend({}, Downloadify.defaultOptions, options);
197
        var dl = Downloadify.create( this, options);
198
        $(this).data('Downloadify', dl);
199
      });
200
    };
201
  })(jQuery);
202
};
203
 
204
/* mootools helper */
205
if(typeof(MooTools) != 'undefined'){
206
  Element.implement({
207
    downloadify: function(options) {
208
      options = $merge(Downloadify.defaultOptions,options);
209
      return this.store('Downloadify',Downloadify.create(this,options));
210
    }
211
  });
212
};
213