program_card_view.js 3.99 KB
Newer Older
1
(function(define) {
2 3 4 5 6 7
    'use strict';

    define(['backbone',
            'jquery',
            'underscore',
            'gettext',
8 9
            'text!../../../templates/learner_dashboard/program_card.underscore',
            'picturefill'
10 11 12 13 14 15
           ],
         function(
             Backbone,
             $,
             _,
             gettext,
16 17
             programCardTpl,
             picturefill
18
         ) {
19 20 21 22 23 24
             return Backbone.View.extend({

                 className: 'program-card',

                 attributes: function() {
                     return {
25
                         'aria-labelledby': 'program-' + this.model.get('uuid'),
26 27 28 29 30 31 32 33 34 35
                         'role': 'group'
                     };
                 },

                 tpl: _.template(programCardTpl),

                 initialize: function(data) {
                     this.progressCollection = data.context.progressCollection;
                     if (this.progressCollection) {
                         this.progressModel = this.progressCollection.findWhere({
36
                             uuid: this.model.get('uuid')
37 38 39 40 41 42
                         });
                     }
                     this.render();
                 },

                 render: function() {
43
                     var orgList = _.map(this.model.get('authoring_organizations'), function(org) {
44 45 46
                             return gettext(org.key);
                         }),
                         data = $.extend(
47 48 49 50 51
                            this.model.toJSON(),
                            this.getProgramProgress(),
                            {orgList: orgList.join(' ')}
                        );

52 53 54
                     this.$el.html(this.tpl(data));
                     this.postRender();
                 },
55

56 57 58
                 postRender: function() {
                     if (navigator.userAgent.indexOf('MSIE') !== -1 ||
                        navigator.appVersion.indexOf('Trident/') > 0) {
59
                        /* Microsoft Internet Explorer detected in. */
60 61 62 63 64
                         window.setTimeout(function() {
                             this.reLoadBannerImage();
                         }.bind(this), 100);
                     }
                 },
65

66
                // Calculate counts for progress and percentages for styling
67 68 69 70 71
                 getProgramProgress: function() {
                     var progress = this.progressModel ? this.progressModel.toJSON() : false;

                     if (progress) {

72 73 74
                         progress.total = progress.completed +
                                          progress.in_progress +
                                          progress.not_started;
75

76
                         progress.percentage = {
77 78
                             completed: this.getWidth(progress.completed, progress.total),
                             in_progress: this.getWidth(progress.in_progress, progress.total)
79 80 81 82 83 84 85
                         };
                     }

                     return {
                         progress: progress
                     };
                 },
86

87 88
                 getWidth: function(val, total) {
                     var int = (val / total) * 100;
89

90 91
                     return int + '%';
                 },
92

93
                // Defer loading the rest of the page to limit FOUC
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                 reLoadBannerImage: function() {
                     var $img = this.$('.program_card .banner-image'),
                         imgSrcAttr = $img ? $img.attr('src') : {};

                     if (!imgSrcAttr || imgSrcAttr.length < 0) {
                         try {
                             this.reEvaluatePicture();
                         } catch (err) {
                            // Swallow the error here
                         }
                     }
                 },

                 reEvaluatePicture: function() {
                     picturefill({
                         reevaluate: true
                     });
                 }
             });
         }
114 115
    );
}).call(this, define || RequireJS.define);