@@ -2,8 +2,12 @@ define([
22 'backbone' ,
33 'text!template/course-document.html'
44] , function ( Backbone , viewTemplate ) {
5+ function onError ( error ) {
6+ console . error ( error ) ;
7+ }
8+
59 var CourseDocumentView = Backbone . View . extend ( {
6- tagName : 'li ' ,
10+ tagName : 'div ' ,
711 className : 'media' ,
812 template : _ . template ( viewTemplate ) ,
913 render : function ( ) {
@@ -17,15 +21,81 @@ define([
1721 btnDownloadOnClick : function ( e ) {
1822 e . preventDefault ( ) ;
1923
20- if ( this . model . get ( 'type' ) === 'file' ) {
21- window . open ( this . model . get ( 'url' ) ) ;
24+ var filePath = this . model . get ( 'path' ) ,
25+ fileURL = this . model . get ( 'url' ) ;
26+
27+ if ( this . model . get ( 'type' ) !== 'file' ) {
28+ Backbone . history . navigate ( '#documents/' + this . model . get ( 'id' ) , {
29+ trigger : true
30+ } ) ;
2231
2332 return ;
2433 }
2534
26- Backbone . history . navigate ( '#documents/' + this . model . get ( 'id' ) , {
27- trigger : true
28- } ) ;
35+ var $pgb = this . $el . find ( '.progress' ) ,
36+ $txtSuccess = this . $el . find ( '.text-success' ) ,
37+ $txtDanger = this . $el . find ( '.text-danger' ) ;
38+
39+ if ( ! $pgb . length ) {
40+ return ;
41+ }
42+
43+ window . requestFileSystem ( LocalFileSystem . PERSISTENT , 0 , function ( fs ) {
44+ fs . root . getFile ( filePath , {
45+ create : true ,
46+ exclusive : false
47+ } , function ( fileEntry ) {
48+ $txtDanger . addClass ( 'hidden' ) ;
49+ $txtSuccess . addClass ( 'hidden' ) ;
50+
51+ $pgb
52+ . removeClass ( 'hidden' )
53+ . removeAttr ( 'aria-hidden' )
54+ . find ( '.progress-bar' )
55+ . attr ( 'aria-valuenow' , 0 )
56+ . css ( 'width' , 0 + '%' )
57+ . find ( '.sr-only' )
58+ . text ( 0 + '%' ) ;
59+
60+ var fileTransfer = new FileTransfer ( ) ;
61+ fileTransfer . onprogress = function ( e ) {
62+ if ( e . lengthComputable ) {
63+ var value = e . loaded / e . total * 100 ,
64+ percentage = value . toFixed ( 2 ) ;
65+
66+ $pgb . find ( '.progress-bar' )
67+ . attr ( 'aria-valuenow' , percentage )
68+ . css ( 'width' , percentage + '%' )
69+ . find ( '.sr-only' )
70+ . text ( percentage + '%' ) ;
71+
72+ return ;
73+ }
74+
75+ $pgb . find ( '.progress-bar' )
76+ . addClass ( 'progress-bar-striped active' )
77+ . attr ( 'aria-valuenow' , 100 )
78+ . css ( 'width' , 100 + '%' )
79+ . find ( '.sr-only' )
80+ . text ( 100 + '%' ) ;
81+ } ;
82+ fileTransfer . download (
83+ encodeURI ( fileURL ) ,
84+ fileEntry . toURL ( ) ,
85+ function ( ) {
86+ $pgb . addClass ( 'hidden' ) ;
87+
88+ $txtSuccess . removeClass ( 'hidden' ) ;
89+ } ,
90+ function ( ) {
91+ $pgb . addClass ( 'hidden' ) ;
92+
93+ $txtDanger . removeClass ( 'hidden' ) ;
94+ } ,
95+ true
96+ ) ;
97+ } , onError ) ;
98+ } , onError ) ;
2999 }
30100 } ) ;
31101
0 commit comments