i18n_backend_database_rails3 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.textile +125 -0
- data/Rakefile +1 -0
- data/data/locales.yml +4 -0
- data/generators/i18n_backend_database/i18n_backend_database_generator.rb +8 -0
- data/generators/i18n_backend_database/templates/migrate/create_i18n_tables.rb +19 -0
- data/i18n_backend_database.gemspec +24 -0
- data/init.rb +1 -0
- data/lib/controllers/locales_controller.rb +86 -0
- data/lib/controllers/translations_controller.rb +141 -0
- data/lib/ext/i18n.rb +68 -0
- data/lib/google_language.rb +30 -0
- data/lib/i18n_backend_database.rb +9 -0
- data/lib/i18n_backend_database/database.rb +263 -0
- data/lib/i18n_backend_database/version.rb +7 -0
- data/lib/i18n_util.rb +148 -0
- data/lib/models/locale.rb +67 -0
- data/lib/models/translation.rb +46 -0
- data/lib/models/translation_option.rb +26 -0
- data/lib/public/images/custom1_bar.gif +0 -0
- data/lib/public/images/custom1_box.gif +0 -0
- data/lib/public/images/percentImage.png +0 -0
- data/lib/public/images/percentImage_back.png +0 -0
- data/lib/public/images/percentImage_back1.png +0 -0
- data/lib/public/images/percentImage_back2.png +0 -0
- data/lib/public/images/percentImage_back3.png +0 -0
- data/lib/public/images/percentImage_back4.png +0 -0
- data/lib/public/javascripts/jsProgressBarHandler.js +509 -0
- data/lib/routing.rb +15 -0
- data/lib/views/layouts/translations.html.haml +16 -0
- data/lib/views/locales/edit.html.haml +16 -0
- data/lib/views/locales/index.html.haml +14 -0
- data/lib/views/locales/new.html.haml +14 -0
- data/lib/views/locales/show.html.haml +10 -0
- data/lib/views/translations/asset_translations.html.haml +23 -0
- data/lib/views/translations/edit.html.haml +24 -0
- data/lib/views/translations/index.html.haml +21 -0
- data/lib/views/translations/new.html.haml +14 -0
- data/lib/views/translations/show.html.haml +21 -0
- data/lib/views/translations/translations.html.haml +20 -0
- data/lib/views/translations/update.rjs +7 -0
- data/routes.rb +3 -0
- data/spec/assets/public/es/favicons/favicon1.gif +0 -0
- data/spec/assets/public/es/images/icons/icon1.gif +0 -0
- data/spec/assets/public/es/images/image1.gif +0 -0
- data/spec/assets/public/favicons/favicon1.gif +0 -0
- data/spec/assets/public/favicons/favicon2.gif +0 -0
- data/spec/assets/public/images/icons/icon1.gif +0 -0
- data/spec/assets/public/images/icons/icon2.gif +0 -0
- data/spec/assets/public/images/image1.gif +0 -0
- data/spec/assets/public/images/image2.gif +0 -0
- data/spec/assets/public/images/promo/sfc08_140x400_3.gif +0 -0
- data/spec/assets/views/test_view1.html.erb +1 -0
- data/spec/assets/views/test_view2.html.erb +30 -0
- data/spec/caching_spec.rb +87 -0
- data/spec/controllers/locales_controller_spec.rb +173 -0
- data/spec/controllers/locales_routing_spec.rb +59 -0
- data/spec/controllers/translations_controller_spec.rb +189 -0
- data/spec/controllers/translations_routing_spec.rb +59 -0
- data/spec/database_spec.rb +199 -0
- data/spec/i18n_ext_spec.rb +40 -0
- data/spec/localize_spec.rb +66 -0
- data/spec/localize_text_spec.rb +76 -0
- data/spec/models/locale_spec.rb +111 -0
- data/spec/models/translation_spec.rb +44 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/translate_asset_spec.rb +57 -0
- data/spec/translate_spec.rb +546 -0
- data/tasks/i18n.rake +60 -0
- metadata +143 -0
@@ -0,0 +1,67 @@
|
|
1
|
+
class Locale < ActiveRecord::Base
|
2
|
+
validates_presence_of :code
|
3
|
+
validates_uniqueness_of :code
|
4
|
+
|
5
|
+
has_many :translations, :dependent => :destroy
|
6
|
+
scope :non_defaults, :conditions => ["code != ?", I18n.default_locale.to_s]
|
7
|
+
|
8
|
+
#named_scope :english, lambda { |m| { return Hash.new if m.nil?; :conditions => "locales.locale = '#{m}'" } }
|
9
|
+
# named_scope :in_city, lambda { |m| { return {} if m.nil?; :joins => [cities], :conditions => "cities.name = '#{m}' } }
|
10
|
+
|
11
|
+
|
12
|
+
@@default_locale = nil
|
13
|
+
|
14
|
+
def self.default_locale
|
15
|
+
@@default_locale ||= self.find(:first, :conditions => {:code => I18n.default_locale.to_s})
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.reset_default_locale
|
19
|
+
@@default_locale = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def translation_from_key(key)
|
23
|
+
self.translations.find(:first, :conditions => {:key => Translation.hk(key)})
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_translation(key, value, pluralization_index=1)
|
27
|
+
conditions = {:key => key, :raw_key => key.to_s, :pluralization_index => pluralization_index}
|
28
|
+
|
29
|
+
# set the key as the value if we're using the default locale and the key is a string
|
30
|
+
conditions.merge!({:value => value}) if (self.code == I18n.default_locale.to_s && key.is_a?(String))
|
31
|
+
translation = self.translations.create(conditions)
|
32
|
+
|
33
|
+
# hackity hack. bug #922 maybe?
|
34
|
+
self.connection.commit_db_transaction unless RAILS_ENV['test']
|
35
|
+
translation
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_translation_or_copy_from_default_locale(key, pluralization_index)
|
39
|
+
self.translations.find_by_key_and_pluralization_index(Translation.hk(key), pluralization_index) || copy_from_default(key, pluralization_index)
|
40
|
+
end
|
41
|
+
|
42
|
+
def copy_from_default(key, pluralization_index)
|
43
|
+
if !self.default_locale? && Locale.default_locale.has_translation?(key, pluralization_index)
|
44
|
+
create_translation(key, key, pluralization_index)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_translation?(key, pluralization_index=1)
|
49
|
+
self.translations.exists?(:key => Translation.hk(key), :pluralization_index => pluralization_index)
|
50
|
+
end
|
51
|
+
|
52
|
+
def percentage_translated
|
53
|
+
(self.translations.translated.count.to_f / self.translations.count.to_f * 100).round rescue 100
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.available_locales
|
57
|
+
all.map(&:code).map(&:to_sym) rescue []
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_locale?
|
61
|
+
self == Locale.default_locale
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_param
|
65
|
+
self.code
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
class Translation < ActiveRecord::Base
|
4
|
+
belongs_to :locale
|
5
|
+
validates_presence_of :key
|
6
|
+
before_validation_on_create :generate_hash_key
|
7
|
+
after_update :update_cache
|
8
|
+
|
9
|
+
scope :untranslated, :conditions => {:value => nil}, :order => :raw_key
|
10
|
+
scope :translated, :conditions => "value IS NOT NULL", :order => :raw_key
|
11
|
+
|
12
|
+
def default_locale_value(rescue_value='No default locale value')
|
13
|
+
begin
|
14
|
+
Locale.default_locale.translations.find_by_key_and_pluralization_index(self.key, self.pluralization_index).value
|
15
|
+
rescue
|
16
|
+
rescue_value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def value_or_default
|
21
|
+
value = self.value || self.default_locale_value(self.raw_key)
|
22
|
+
value =~ /^---(.*)\n/ ? YAML.load(value) : value # supports using YAML e.g. order: [ :year, :month, :day ] values are stored as Symbols "--- :year\n", "--- :month\n", "--- :day\n"
|
23
|
+
end
|
24
|
+
|
25
|
+
# create hash key
|
26
|
+
def self.hk(key)
|
27
|
+
Base64.encode64(Digest::MD5.hexdigest(key.to_s)).gsub(/\n/, '')
|
28
|
+
end
|
29
|
+
|
30
|
+
# create cache key
|
31
|
+
def self.ck(locale, key, hash=true)
|
32
|
+
key = self.hk(key) if hash
|
33
|
+
"#{locale.code}:#{key}"
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
def generate_hash_key
|
38
|
+
self.raw_key = key.to_s
|
39
|
+
self.key = Translation.hk(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
def update_cache
|
43
|
+
new_cache_key = Translation.ck(self.locale, self.key, false)
|
44
|
+
I18n.backend.cache_store.write(new_cache_key, self.value)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class TranslationOption
|
2
|
+
|
3
|
+
attr_accessor :code, :description
|
4
|
+
cattr_accessor :translated, :untranslated
|
5
|
+
|
6
|
+
def initialize(code, description)
|
7
|
+
@code, @description = code, description
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.all
|
11
|
+
[untranslated, translated]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.translated
|
15
|
+
@@translated ||= TranslationOption.new('translated', 'Translated')
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.untranslated
|
19
|
+
@@untranslated ||= TranslationOption.new('untranslated', 'Untranslated')
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.find(code)
|
23
|
+
all.detect{|option| option.code == code} || untranslated
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,509 @@
|
|
1
|
+
/*****************************************************************
|
2
|
+
*
|
3
|
+
* jsProgressBarHandler 0.3.3 - by Bramus! - http://www.bram.us/
|
4
|
+
*
|
5
|
+
* v 0.3.3 - 2008.11.10 - UPD: fixed IE compatibility issue (thanks Kevin - Sep 19 2008 / 6pm)
|
6
|
+
* - UPD: setPercentage now parses the targetPercentage to an Integer to avoid infinite loop (thanks Jack - Sep 07 2008 / 9pm)
|
7
|
+
* - UPD: Moved from Event.Observe(window, 'load', fn) to document.observe('dom:loaded', fn) in order to force people to use an up to date Prototype release.
|
8
|
+
* - UPD: setPercentage now takes an overrideQueue param. If set the current queue is cleared.
|
9
|
+
* - ADD: Added onTick callback event which gets called when the percentage is updated.
|
10
|
+
* - ADD: Added stable (as in "non-crashing") versions of the additions which first surfaced in the (unreleased) 0.3.2 release
|
11
|
+
* Preloading support partially implemented in IE as all versions (IE6,7&8) are quite hard to tame (one time they work, the next reload they don't anymore)
|
12
|
+
* v 0.3.2 - 2008.04.09 (*UNRELEASED*)
|
13
|
+
* - ADD: implemented preloading of images to avoid slight flicker when switching images (BUGGY!)
|
14
|
+
* - ADD: percentage image now has class percentImage and percentage Text now has class percentText; This allows you to style the output easily.
|
15
|
+
* v 0.3.1 - 2008.02.20 - UPD: fixed queue bug when animate was set to false (thanks Jamie Chong)
|
16
|
+
* - UPD: update Prototype to version 1.6.0.2
|
17
|
+
* v 0.3.0 - 2008.02.01 - ADD: animation queue, prevents from the progressbar getting stuck when multiple calls are made during an animation
|
18
|
+
* - UPD: multiple barImages now work properly in Safari
|
19
|
+
* v 0.2.1 - 2007.12.20 - ADD: option : set boxImage
|
20
|
+
* ADD: option : set barImage (one or more)
|
21
|
+
* ADD: option : showText
|
22
|
+
* v 0.2 - 2007.12.13 - SYS: rewrite in 2 classs including optimisations
|
23
|
+
* ADD: Config options
|
24
|
+
* v 0.1 - 2007.08.02 - initial release
|
25
|
+
*
|
26
|
+
* @see http://www.barenakedapp.com/the-design/displaying-percentages on how to create a progressBar Background Image!
|
27
|
+
*
|
28
|
+
* Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
|
29
|
+
*
|
30
|
+
*****************************************************************/
|
31
|
+
|
32
|
+
|
33
|
+
/**
|
34
|
+
* CONFIG
|
35
|
+
* -------------------------------------------------------------
|
36
|
+
*/
|
37
|
+
|
38
|
+
// Should jsProgressBarHandler hook itself to all span.progressBar elements? - default : true
|
39
|
+
var autoHook = true;
|
40
|
+
|
41
|
+
// Default Options
|
42
|
+
var defaultOptions = {
|
43
|
+
animate : true, // Animate the progress? - default: true
|
44
|
+
showText : true, // show text with percentage in next to the progressbar? - default : true
|
45
|
+
width : 120, // Width of the progressbar - don't forget to adjust your image too!!!
|
46
|
+
barImage : Array(
|
47
|
+
'images/percentImage_back4.png',
|
48
|
+
'images/percentImage_back3.png',
|
49
|
+
'images/percentImage_back2.png',
|
50
|
+
'images/percentImage_back1.png'
|
51
|
+
),
|
52
|
+
boxImage : 'images/percentImage.png', // boxImage : image around the progress bar
|
53
|
+
/*barImage : 'images/percentImage_back.png',*/ // Image to use in the progressbar. Can be an array of images too.
|
54
|
+
height : 12, // Height of the progressbar - don't forget to adjust your image too!!!
|
55
|
+
onTick : function(pbObj) { return true }
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* NO NEED TO CHANGE ANYTHING BENEATH THIS LINE
|
60
|
+
* -------------------------------------------------------------
|
61
|
+
*/
|
62
|
+
|
63
|
+
/**
|
64
|
+
* JS_BRAMUS Object
|
65
|
+
* -------------------------------------------------------------
|
66
|
+
*/
|
67
|
+
|
68
|
+
if (!JS_BRAMUS) { var JS_BRAMUS = new Object(); }
|
69
|
+
|
70
|
+
|
71
|
+
/**
|
72
|
+
* ProgressBar Class
|
73
|
+
* -------------------------------------------------------------
|
74
|
+
*/
|
75
|
+
|
76
|
+
JS_BRAMUS.jsProgressBar = Class.create();
|
77
|
+
|
78
|
+
JS_BRAMUS.jsProgressBar.prototype = {
|
79
|
+
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Datamembers
|
83
|
+
* -------------------------------------------------------------
|
84
|
+
*/
|
85
|
+
|
86
|
+
el : null, // Element where to render the progressBar in
|
87
|
+
id : null, // Unique ID of the progressbar
|
88
|
+
percentage : null, // Percentage of the progressbar
|
89
|
+
|
90
|
+
options : null, // The options
|
91
|
+
|
92
|
+
initialPos : null, // Initial postion of the background in the progressbar
|
93
|
+
initialPerc : null, // Initial percentage the progressbar should hold
|
94
|
+
pxPerPercent : null, // Number of pixels per 1 percent
|
95
|
+
|
96
|
+
backIndex : null, // index in the array of background images currently used
|
97
|
+
numPreloaded : null, // number of images preloaded
|
98
|
+
|
99
|
+
running : null, // is this one running (being animated) or not?
|
100
|
+
|
101
|
+
queue : false, // queue of percentages to set to
|
102
|
+
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Constructor
|
106
|
+
*
|
107
|
+
* @param HTMLElement el
|
108
|
+
* @param string id
|
109
|
+
* @param int percentage
|
110
|
+
* @return void
|
111
|
+
* -------------------------------------------------------------
|
112
|
+
*/
|
113
|
+
|
114
|
+
initialize : function(el, percentage, options) {
|
115
|
+
|
116
|
+
// get the options
|
117
|
+
this.options = Object.clone(defaultOptions);
|
118
|
+
Object.extend(this.options, options || {});
|
119
|
+
|
120
|
+
// datamembers from arguments
|
121
|
+
this.el = $(el);
|
122
|
+
this.id = $(el).id;
|
123
|
+
this.percentage = 0; // Set to 0 intially, we'll change this later.
|
124
|
+
this.backIndex = 0; // Set to 0 initially
|
125
|
+
this.numPreloaded = 0; // Set to 0 initially
|
126
|
+
this.running = false; // Set to false initially
|
127
|
+
this.queue = Array(); // Set to empty Array initially
|
128
|
+
|
129
|
+
// datamembers which are calculatef
|
130
|
+
this.imgWidth = this.options.width * 2; // define the width of the image (twice the width of the progressbar)
|
131
|
+
this.initialPos = this.options.width * (-1); // Initial postion of the background in the progressbar (0% is the middle of our image!)
|
132
|
+
this.pxPerPercent = this.options.width / 100; // Define how much pixels go into 1%
|
133
|
+
this.initialPerc = percentage; // Store this, we'll need it later.
|
134
|
+
|
135
|
+
// enfore backimage array
|
136
|
+
if (this.options.barImage.constructor != Array) { // used to be (but doesn't work in Safari): if (this.options.barImage.constructor.toString().indexOf("Array") == -1) {
|
137
|
+
this.options.barImage = Array(this.options.barImage);
|
138
|
+
}
|
139
|
+
|
140
|
+
// preload Images
|
141
|
+
this.preloadImages();
|
142
|
+
|
143
|
+
},
|
144
|
+
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Preloads the images needed for the progressbar
|
148
|
+
*
|
149
|
+
* @return void
|
150
|
+
* -------------------------------------------------------------
|
151
|
+
*/
|
152
|
+
|
153
|
+
preloadImages : function() {
|
154
|
+
|
155
|
+
// loop all barimages
|
156
|
+
for (i = 0; i < this.options.barImage.length; i++) {
|
157
|
+
|
158
|
+
// create new image ref
|
159
|
+
var newImage = null;
|
160
|
+
newImage = new Image();
|
161
|
+
|
162
|
+
// set onload, onerror and onabort functions
|
163
|
+
newImage.onload = function() { this.numPreloaded++; }.bind(this);
|
164
|
+
newImage.onerror = function() { this.numPreloaded++; }.bind(this);
|
165
|
+
newImage.onabort = function() { this.numPreloaded++; }.bind(this);
|
166
|
+
|
167
|
+
// set image source (preload it!)
|
168
|
+
newImage.src = this.options.barImage[i];
|
169
|
+
|
170
|
+
// image is in cache
|
171
|
+
if (newImage.complete) {
|
172
|
+
this.numPreloaded++;
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
// if not IE, check if they're loaded
|
178
|
+
if (!Prototype.Browser.IE) {
|
179
|
+
this.checkPreloadedImages();
|
180
|
+
|
181
|
+
// if IE, just init the visuals as it's quite hard to tame all IE's
|
182
|
+
} else {
|
183
|
+
this.initVisuals();
|
184
|
+
}
|
185
|
+
|
186
|
+
},
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
* Check whether all images are preloaded and loads the percentage if so
|
191
|
+
*
|
192
|
+
* @return void
|
193
|
+
* -------------------------------------------------------------
|
194
|
+
*/
|
195
|
+
|
196
|
+
checkPreloadedImages : function() {
|
197
|
+
|
198
|
+
// all images are loaded, go init the visuals
|
199
|
+
if (parseInt(this.numPreloaded,10) >= parseInt(this.options.barImage.length,10) ) {
|
200
|
+
|
201
|
+
// initVisuals
|
202
|
+
this.initVisuals();
|
203
|
+
|
204
|
+
// not all images are loaded ... wait a little and then retry
|
205
|
+
} else {
|
206
|
+
|
207
|
+
if ( parseInt(this.numPreloaded,10) <= parseInt(this.options.barImage.length,10) ) {
|
208
|
+
// $(this.el).update(this.id + ' : ' + this.numPreloaded + '/' + this.options.barImage.length);
|
209
|
+
setTimeout(function() { this.checkPreloadedImages(); }.bind(this), 100);
|
210
|
+
}
|
211
|
+
|
212
|
+
}
|
213
|
+
|
214
|
+
},
|
215
|
+
|
216
|
+
|
217
|
+
/**
|
218
|
+
* Intializes the visual output and sets the percentage
|
219
|
+
*
|
220
|
+
* @return void
|
221
|
+
* -------------------------------------------------------------
|
222
|
+
*/
|
223
|
+
|
224
|
+
initVisuals : function () {
|
225
|
+
|
226
|
+
// create the visual aspect of the progressBar
|
227
|
+
$(this.el).update(
|
228
|
+
'<img id="' + this.id + '_percentImage" src="' + this.options.boxImage + '" alt="0%" style="width: ' + this.options.width + 'px; height: ' + this.options.height + 'px; background-position: ' + this.initialPos + 'px 50%; background-image: url(' + this.options.barImage[this.backIndex] + '); padding: 0; margin: 0;" class="percentImage" />' +
|
229
|
+
((this.options.showText == true)?'<span id="' + this.id + '_percentText" class="percentText">0%</span>':''));
|
230
|
+
|
231
|
+
// set the percentage
|
232
|
+
this.setPercentage(this.initialPerc);
|
233
|
+
},
|
234
|
+
|
235
|
+
|
236
|
+
/**
|
237
|
+
* Sets the percentage of the progressbar
|
238
|
+
*
|
239
|
+
* @param string targetPercentage
|
240
|
+
* @param boolen clearQueue
|
241
|
+
* @return void
|
242
|
+
* -------------------------------------------------------------
|
243
|
+
*/
|
244
|
+
setPercentage : function(targetPercentage, clearQueue) {
|
245
|
+
|
246
|
+
// if clearQueue is set, empty the queue and then set the percentage
|
247
|
+
if (clearQueue) {
|
248
|
+
|
249
|
+
this.percentage = (this.queue.length != 0) ? this.queue[0] : targetPercentage;
|
250
|
+
this.timer = null;
|
251
|
+
this.queue = [];
|
252
|
+
|
253
|
+
setTimeout(function() { this.setPercentage(targetPercentage); }.bind(this), 10);
|
254
|
+
|
255
|
+
// no clearQueue defined, set the percentage
|
256
|
+
} else {
|
257
|
+
|
258
|
+
// add the percentage on the queue
|
259
|
+
this.queue.push(targetPercentage);
|
260
|
+
|
261
|
+
// process the queue (if not running already)
|
262
|
+
if (this.running == false) {
|
263
|
+
this.processQueue();
|
264
|
+
}
|
265
|
+
}
|
266
|
+
|
267
|
+
},
|
268
|
+
|
269
|
+
|
270
|
+
/**
|
271
|
+
* Processes the queue
|
272
|
+
*
|
273
|
+
* @return void
|
274
|
+
* -------------------------------------------------------------
|
275
|
+
*/
|
276
|
+
|
277
|
+
processQueue : function() {
|
278
|
+
|
279
|
+
// stuff on queue?
|
280
|
+
if (this.queue.length > 0) {
|
281
|
+
|
282
|
+
// tell the world that we're busy
|
283
|
+
this.running = true;
|
284
|
+
|
285
|
+
// process the entry
|
286
|
+
this.processQueueEntry(this.queue[0]);
|
287
|
+
|
288
|
+
// no stuff on queue
|
289
|
+
} else {
|
290
|
+
|
291
|
+
// return;
|
292
|
+
return;
|
293
|
+
|
294
|
+
}
|
295
|
+
|
296
|
+
},
|
297
|
+
|
298
|
+
|
299
|
+
/**
|
300
|
+
* Processes an entry from the queue (viz. animates it)
|
301
|
+
*
|
302
|
+
* @param string targetPercentage
|
303
|
+
* @return void
|
304
|
+
* -------------------------------------------------------------
|
305
|
+
*/
|
306
|
+
|
307
|
+
processQueueEntry : function(targetPercentage) {
|
308
|
+
|
309
|
+
// get the current percentage
|
310
|
+
var curPercentage = parseInt(this.percentage,10);
|
311
|
+
|
312
|
+
// define the new percentage
|
313
|
+
if ((targetPercentage.toString().substring(0,1) == "+") || (targetPercentage.toString().substring(0,1) == "-")) {
|
314
|
+
targetPercentage = curPercentage + parseInt(targetPercentage);
|
315
|
+
}
|
316
|
+
|
317
|
+
// min and max percentages
|
318
|
+
if (targetPercentage < 0) targetPercentage = 0;
|
319
|
+
if (targetPercentage > 100) targetPercentage = 100;
|
320
|
+
|
321
|
+
// if we don't need to animate, just change the background position right now and return
|
322
|
+
if (this.options.animate == false) {
|
323
|
+
|
324
|
+
// remove the entry from the queue
|
325
|
+
this.queue.splice(0,1); // @see: http://www.bram.us/projects/js_bramus/jsprogressbarhandler/#comment-174878
|
326
|
+
|
327
|
+
// Change the background position (and update this.percentage)
|
328
|
+
this._setBgPosition(targetPercentage);
|
329
|
+
|
330
|
+
// call onTick
|
331
|
+
if (!this.options.onTick(this)) {
|
332
|
+
return;
|
333
|
+
}
|
334
|
+
|
335
|
+
// we're not running anymore
|
336
|
+
this.running = false;
|
337
|
+
|
338
|
+
// continue processing the queue
|
339
|
+
this.processQueue();
|
340
|
+
|
341
|
+
// we're done!
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
|
345
|
+
// define if we need to add/subtract something to the current percentage in order to reach the target percentage
|
346
|
+
if (targetPercentage != curPercentage) {
|
347
|
+
if (curPercentage < targetPercentage) {
|
348
|
+
newPercentage = curPercentage + 1;
|
349
|
+
} else {
|
350
|
+
newPercentage = curPercentage - 1;
|
351
|
+
}
|
352
|
+
callTick = true;
|
353
|
+
} else {
|
354
|
+
newPercentage = curPercentage;
|
355
|
+
callTick = false;
|
356
|
+
}
|
357
|
+
|
358
|
+
// Change the background position (and update this.percentage)
|
359
|
+
this._setBgPosition(newPercentage);
|
360
|
+
|
361
|
+
// call onTick
|
362
|
+
if (callTick && !this.options.onTick(this)) {
|
363
|
+
return;
|
364
|
+
}
|
365
|
+
|
366
|
+
// Percentage not reached yet : continue processing entry
|
367
|
+
if (curPercentage != newPercentage) {
|
368
|
+
|
369
|
+
this.timer = setTimeout(function() { this.processQueueEntry(targetPercentage); }.bind(this), 10);
|
370
|
+
|
371
|
+
// Percentage reached!
|
372
|
+
} else {
|
373
|
+
|
374
|
+
// remove the entry from the queue
|
375
|
+
this.queue.splice(0,1);
|
376
|
+
|
377
|
+
// we're not running anymore
|
378
|
+
this.running = false;
|
379
|
+
|
380
|
+
// unset timer
|
381
|
+
this.timer = null;
|
382
|
+
|
383
|
+
// process the rest of the queue
|
384
|
+
this.processQueue();
|
385
|
+
|
386
|
+
// we're done!
|
387
|
+
return;
|
388
|
+
}
|
389
|
+
|
390
|
+
},
|
391
|
+
|
392
|
+
|
393
|
+
/**
|
394
|
+
* Gets the percentage of the progressbar
|
395
|
+
*
|
396
|
+
* @return int
|
397
|
+
*/
|
398
|
+
getPercentage : function(id) {
|
399
|
+
return this.percentage;
|
400
|
+
},
|
401
|
+
|
402
|
+
|
403
|
+
/**
|
404
|
+
* Set the background position
|
405
|
+
*
|
406
|
+
* @param int percentage
|
407
|
+
*/
|
408
|
+
_setBgPosition : function(percentage) {
|
409
|
+
// adjust the background position
|
410
|
+
$(this.id + "_percentImage").style.backgroundPosition = (this.initialPos + (percentage * this.pxPerPercent)) + "px 50%";
|
411
|
+
|
412
|
+
// adjust the background image and backIndex
|
413
|
+
var newBackIndex = Math.floor((percentage-1) / (100/this.options.barImage.length));
|
414
|
+
|
415
|
+
if ((newBackIndex != this.backIndex) && (this.options.barImage[newBackIndex] != undefined)) {
|
416
|
+
$(this.id + "_percentImage").style.backgroundImage = "url(" + this.options.barImage[newBackIndex] + ")";
|
417
|
+
}
|
418
|
+
|
419
|
+
this.backIndex = newBackIndex;
|
420
|
+
|
421
|
+
// Adjust the alt & title of the image
|
422
|
+
$(this.id + "_percentImage").alt = percentage + "%";
|
423
|
+
$(this.id + "_percentImage").title = percentage + "%";
|
424
|
+
|
425
|
+
// Update the text
|
426
|
+
if (this.options.showText == true) {
|
427
|
+
$(this.id + "_percentText").update("" + percentage + "%");
|
428
|
+
}
|
429
|
+
|
430
|
+
// adjust datamember to stock the percentage
|
431
|
+
this.percentage = percentage;
|
432
|
+
}
|
433
|
+
}
|
434
|
+
|
435
|
+
|
436
|
+
/**
|
437
|
+
* ProgressHandlerBar Class - automatically create ProgressBar instances
|
438
|
+
* -------------------------------------------------------------
|
439
|
+
*/
|
440
|
+
|
441
|
+
JS_BRAMUS.jsProgressBarHandler = Class.create();
|
442
|
+
|
443
|
+
|
444
|
+
JS_BRAMUS.jsProgressBarHandler.prototype = {
|
445
|
+
|
446
|
+
|
447
|
+
/**
|
448
|
+
* Datamembers
|
449
|
+
* -------------------------------------------------------------
|
450
|
+
*/
|
451
|
+
|
452
|
+
pbArray : new Array(), // Array of progressBars
|
453
|
+
|
454
|
+
|
455
|
+
/**
|
456
|
+
* Constructor
|
457
|
+
*
|
458
|
+
* @return void
|
459
|
+
* -------------------------------------------------------------
|
460
|
+
*/
|
461
|
+
|
462
|
+
initialize : function() {
|
463
|
+
|
464
|
+
// get all span.progressBar elements
|
465
|
+
$$('span.progressBar').each(function(el) {
|
466
|
+
|
467
|
+
// create a progressBar for each element
|
468
|
+
this.pbArray[el.id] = new JS_BRAMUS.jsProgressBar(el, parseInt(el.innerHTML.replace("%","")));
|
469
|
+
|
470
|
+
}.bind(this));
|
471
|
+
},
|
472
|
+
|
473
|
+
|
474
|
+
/**
|
475
|
+
* Set the percentage of a progressbar
|
476
|
+
*
|
477
|
+
* @param string el
|
478
|
+
* @param string percentage
|
479
|
+
* @return void
|
480
|
+
* -------------------------------------------------------------
|
481
|
+
*/
|
482
|
+
setPercentage : function(el, percentage, clearQueue) {
|
483
|
+
this.pbArray[el].setPercentage(percentage, clearQueue);
|
484
|
+
},
|
485
|
+
|
486
|
+
|
487
|
+
/**
|
488
|
+
* Get the percentage of a progressbar
|
489
|
+
*
|
490
|
+
* @param string el
|
491
|
+
* @return int percentage
|
492
|
+
* -------------------------------------------------------------
|
493
|
+
*/
|
494
|
+
getPercentage : function(el) {
|
495
|
+
return this.pbArray[el].getPercentage();
|
496
|
+
}
|
497
|
+
|
498
|
+
}
|
499
|
+
|
500
|
+
|
501
|
+
/**
|
502
|
+
* ProgressHandlerBar Class - hook me or not?
|
503
|
+
* -------------------------------------------------------------
|
504
|
+
*/
|
505
|
+
|
506
|
+
if (autoHook == true) {
|
507
|
+
function initProgressBarHandler() { myJsProgressBarHandler = new JS_BRAMUS.jsProgressBarHandler(); }
|
508
|
+
document.observe('dom:loaded', initProgressBarHandler, false);
|
509
|
+
}
|