~johnsca/charms/trusty/cloudfoundry/reconciler-ui

« back to all changes in this revision

Viewing changes to reconciler/ui/static/semantic/less/modules/rating.js

  • Committer: Whit Morriss
  • Date: 2014-10-13 06:50:17 UTC
  • Revision ID: whit.morriss@canonical.com-20141013065017-0feo2ku3yllymkol
reorg reconciler

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * # Semantic - Rating
 
3
 * http://github.com/jlukic/semantic-ui/
 
4
 *
 
5
 *
 
6
 * Copyright 2014 Contributors
 
7
 * Released under the MIT license
 
8
 * http://opensource.org/licenses/MIT
 
9
 *
 
10
 */
 
11
 
 
12
;(function ($, window, document, undefined) {
 
13
 
 
14
$.fn.rating = function(parameters) {
 
15
  var
 
16
    $allModules     = $(this),
 
17
    moduleSelector  = $allModules.selector || '',
 
18
 
 
19
    time            = new Date().getTime(),
 
20
    performance     = [],
 
21
 
 
22
    query           = arguments[0],
 
23
    methodInvoked   = (typeof query == 'string'),
 
24
    queryArguments  = [].slice.call(arguments, 1),
 
25
    returnedValue
 
26
  ;
 
27
  $allModules
 
28
    .each(function() {
 
29
      var
 
30
        settings        = ( $.isPlainObject(parameters) )
 
31
          ? $.extend(true, {}, $.fn.rating.settings, parameters)
 
32
          : $.extend({}, $.fn.rating.settings),
 
33
 
 
34
        namespace       = settings.namespace,
 
35
        className       = settings.className,
 
36
        metadata        = settings.metadata,
 
37
        selector        = settings.selector,
 
38
        error           = settings.error,
 
39
 
 
40
        eventNamespace  = '.' + namespace,
 
41
        moduleNamespace = 'module-' + namespace,
 
42
 
 
43
        element         = this,
 
44
        instance        = $(this).data(moduleNamespace),
 
45
 
 
46
        $module         = $(this),
 
47
        $icon           = $module.find(selector.icon),
 
48
 
 
49
        module
 
50
      ;
 
51
 
 
52
      module = {
 
53
 
 
54
        initialize: function() {
 
55
          module.verbose('Initializing rating module', settings);
 
56
 
 
57
          if(settings.interactive) {
 
58
            module.enable();
 
59
          }
 
60
          else {
 
61
            module.disable();
 
62
          }
 
63
 
 
64
          if(settings.initialRating) {
 
65
            module.debug('Setting initial rating');
 
66
            module.setRating(settings.initialRating);
 
67
          }
 
68
          if( $module.data(metadata.rating) ) {
 
69
            module.debug('Rating found in metadata');
 
70
            module.setRating( $module.data(metadata.rating) );
 
71
          }
 
72
          module.instantiate();
 
73
        },
 
74
 
 
75
        instantiate: function() {
 
76
          module.verbose('Instantiating module', settings);
 
77
          instance = module;
 
78
          $module
 
79
            .data(moduleNamespace, module)
 
80
          ;
 
81
        },
 
82
 
 
83
        destroy: function() {
 
84
          module.verbose('Destroying previous instance', instance);
 
85
          $module
 
86
            .removeData(moduleNamespace)
 
87
          ;
 
88
          $icon
 
89
            .off(eventNamespace)
 
90
          ;
 
91
        },
 
92
 
 
93
        event: {
 
94
          mouseenter: function() {
 
95
            var
 
96
              $activeIcon = $(this)
 
97
            ;
 
98
            $activeIcon
 
99
              .nextAll()
 
100
                .removeClass(className.hover)
 
101
            ;
 
102
            $module
 
103
              .addClass(className.hover)
 
104
            ;
 
105
            $activeIcon
 
106
              .addClass(className.hover)
 
107
                .prevAll()
 
108
                .addClass(className.hover)
 
109
            ;
 
110
          },
 
111
          mouseleave: function() {
 
112
            $module
 
113
              .removeClass(className.hover)
 
114
            ;
 
115
            $icon
 
116
              .removeClass(className.hover)
 
117
            ;
 
118
          },
 
119
          click: function() {
 
120
            var
 
121
              $activeIcon   = $(this),
 
122
              currentRating = module.getRating(),
 
123
              rating        = $icon.index($activeIcon) + 1
 
124
            ;
 
125
            if(settings.clearable && currentRating == rating) {
 
126
              module.clearRating();
 
127
            }
 
128
            else {
 
129
              module.setRating( rating );
 
130
            }
 
131
          }
 
132
        },
 
133
 
 
134
        clearRating: function() {
 
135
          module.debug('Clearing current rating');
 
136
          module.setRating(0);
 
137
        },
 
138
 
 
139
        getRating: function() {
 
140
          var
 
141
            currentRating = $icon.filter('.' + className.active).size()
 
142
          ;
 
143
          module.verbose('Current rating retrieved', currentRating);
 
144
          return currentRating;
 
145
        },
 
146
 
 
147
        enable: function() {
 
148
          module.debug('Setting rating to interactive mode');
 
149
          $icon
 
150
            .on('mouseenter' + eventNamespace, module.event.mouseenter)
 
151
            .on('mouseleave' + eventNamespace, module.event.mouseleave)
 
152
            .on('click' + eventNamespace, module.event.click)
 
153
          ;
 
154
          $module
 
155
            .removeClass(className.disabled)
 
156
          ;
 
157
        },
 
158
 
 
159
        disable: function() {
 
160
          module.debug('Setting rating to read-only mode');
 
161
          $icon
 
162
            .off(eventNamespace)
 
163
          ;
 
164
          $module
 
165
            .addClass(className.disabled)
 
166
          ;
 
167
        },
 
168
 
 
169
        setRating: function(rating) {
 
170
          var
 
171
            ratingIndex = (rating - 1 >= 0)
 
172
              ? (rating - 1)
 
173
              : 0,
 
174
            $activeIcon = $icon.eq(ratingIndex)
 
175
          ;
 
176
          $module
 
177
            .removeClass(className.hover)
 
178
          ;
 
179
          $icon
 
180
            .removeClass(className.hover)
 
181
            .removeClass(className.active)
 
182
          ;
 
183
          if(rating > 0) {
 
184
            module.verbose('Setting current rating to', rating);
 
185
            $activeIcon
 
186
              .addClass(className.active)
 
187
                .prevAll()
 
188
                .addClass(className.active)
 
189
            ;
 
190
          }
 
191
          $.proxy(settings.onRate, element)(rating);
 
192
        },
 
193
 
 
194
        setting: function(name, value) {
 
195
          if( $.isPlainObject(name) ) {
 
196
            $.extend(true, settings, name);
 
197
          }
 
198
          else if(value !== undefined) {
 
199
            settings[name] = value;
 
200
          }
 
201
          else {
 
202
            return settings[name];
 
203
          }
 
204
        },
 
205
        internal: function(name, value) {
 
206
          if( $.isPlainObject(name) ) {
 
207
            $.extend(true, module, name);
 
208
          }
 
209
          else if(value !== undefined) {
 
210
            module[name] = value;
 
211
          }
 
212
          else {
 
213
            return module[name];
 
214
          }
 
215
        },
 
216
        debug: function() {
 
217
          if(settings.debug) {
 
218
            if(settings.performance) {
 
219
              module.performance.log(arguments);
 
220
            }
 
221
            else {
 
222
              module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
 
223
              module.debug.apply(console, arguments);
 
224
            }
 
225
          }
 
226
        },
 
227
        verbose: function() {
 
228
          if(settings.verbose && settings.debug) {
 
229
            if(settings.performance) {
 
230
              module.performance.log(arguments);
 
231
            }
 
232
            else {
 
233
              module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
 
234
              module.verbose.apply(console, arguments);
 
235
            }
 
236
          }
 
237
        },
 
238
        error: function() {
 
239
          module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
 
240
          module.error.apply(console, arguments);
 
241
        },
 
242
        performance: {
 
243
          log: function(message) {
 
244
            var
 
245
              currentTime,
 
246
              executionTime,
 
247
              previousTime
 
248
            ;
 
249
            if(settings.performance) {
 
250
              currentTime   = new Date().getTime();
 
251
              previousTime  = time || currentTime;
 
252
              executionTime = currentTime - previousTime;
 
253
              time          = currentTime;
 
254
              performance.push({
 
255
                'Element'        : element,
 
256
                'Name'           : message[0],
 
257
                'Arguments'      : [].slice.call(message, 1) || '',
 
258
                'Execution Time' : executionTime
 
259
              });
 
260
            }
 
261
            clearTimeout(module.performance.timer);
 
262
            module.performance.timer = setTimeout(module.performance.display, 100);
 
263
          },
 
264
          display: function() {
 
265
            var
 
266
              title = settings.name + ':',
 
267
              totalTime = 0
 
268
            ;
 
269
            time = false;
 
270
            clearTimeout(module.performance.timer);
 
271
            $.each(performance, function(index, data) {
 
272
              totalTime += data['Execution Time'];
 
273
            });
 
274
            title += ' ' + totalTime + 'ms';
 
275
            if(moduleSelector) {
 
276
              title += ' \'' + moduleSelector + '\'';
 
277
            }
 
278
            if($allModules.size() > 1) {
 
279
              title += ' ' + '(' + $allModules.size() + ')';
 
280
            }
 
281
            if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
 
282
              console.groupCollapsed(title);
 
283
              if(console.table) {
 
284
                console.table(performance);
 
285
              }
 
286
              else {
 
287
                $.each(performance, function(index, data) {
 
288
                  console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
 
289
                });
 
290
              }
 
291
              console.groupEnd();
 
292
            }
 
293
            performance = [];
 
294
          }
 
295
        },
 
296
        invoke: function(query, passedArguments, context) {
 
297
          var
 
298
            object = instance,
 
299
            maxDepth,
 
300
            found,
 
301
            response
 
302
          ;
 
303
          passedArguments = passedArguments || queryArguments;
 
304
          context         = element         || context;
 
305
          if(typeof query == 'string' && object !== undefined) {
 
306
            query    = query.split(/[\. ]/);
 
307
            maxDepth = query.length - 1;
 
308
            $.each(query, function(depth, value) {
 
309
              var camelCaseValue = (depth != maxDepth)
 
310
                ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
 
311
                : query
 
312
              ;
 
313
              if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
 
314
                object = object[camelCaseValue];
 
315
              }
 
316
              else if( object[camelCaseValue] !== undefined ) {
 
317
                found = object[camelCaseValue];
 
318
                return false;
 
319
              }
 
320
              else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
 
321
                object = object[value];
 
322
              }
 
323
              else if( object[value] !== undefined ) {
 
324
                found = object[value];
 
325
                return false;
 
326
              }
 
327
              else {
 
328
                return false;
 
329
              }
 
330
            });
 
331
          }
 
332
          if ( $.isFunction( found ) ) {
 
333
            response = found.apply(context, passedArguments);
 
334
          }
 
335
          else if(found !== undefined) {
 
336
            response = found;
 
337
          }
 
338
          if($.isArray(returnedValue)) {
 
339
            returnedValue.push(response);
 
340
          }
 
341
          else if(returnedValue !== undefined) {
 
342
            returnedValue = [returnedValue, response];
 
343
          }
 
344
          else if(response !== undefined) {
 
345
            returnedValue = response;
 
346
          }
 
347
          return found;
 
348
        }
 
349
      };
 
350
      if(methodInvoked) {
 
351
        if(instance === undefined) {
 
352
          module.initialize();
 
353
        }
 
354
        module.invoke(query);
 
355
      }
 
356
      else {
 
357
        if(instance !== undefined) {
 
358
          module.destroy();
 
359
        }
 
360
        module.initialize();
 
361
      }
 
362
    })
 
363
  ;
 
364
 
 
365
  return (returnedValue !== undefined)
 
366
    ? returnedValue
 
367
    : this
 
368
  ;
 
369
};
 
370
 
 
371
$.fn.rating.settings = {
 
372
 
 
373
  name          : 'Rating',
 
374
  namespace     : 'rating',
 
375
 
 
376
  verbose       : true,
 
377
  debug         : false,
 
378
  performance   : true,
 
379
 
 
380
  initialRating : 0,
 
381
  interactive   : true,
 
382
  clearable     : false,
 
383
 
 
384
  onRate        : function(rating){},
 
385
 
 
386
  error       : {
 
387
    method : 'The method you called is not defined'
 
388
  },
 
389
 
 
390
  metadata: {
 
391
    rating: 'rating'
 
392
  },
 
393
 
 
394
  className : {
 
395
    active   : 'active',
 
396
    disabled : 'disabled',
 
397
    hover    : 'hover',
 
398
    loading  : 'loading'
 
399
  },
 
400
 
 
401
  selector  : {
 
402
    icon : '.icon'
 
403
  }
 
404
 
 
405
};
 
406
 
 
407
})( jQuery, window , document );