1
+ "use strict" ;
2
+ ( function ( ) {
3
+ var module = angular . module ( "ui.openseadragon" , [ ] ) ;
4
+ module . directive ( "seadragon" , [ '$timeout' , function ( $timeout ) {
5
+ return {
6
+ restrict : "E" ,
7
+ scope : {
8
+ options : "=" ,
9
+ name : "=" ,
10
+ tilesource : "=" ,
11
+ prefixUrl : "="
12
+ } ,
13
+ controller : [ "$scope" , function ( $scope ) {
14
+ $scope . osd = null ;
15
+ } ] ,
16
+ link : function ( scope , element , attrs ) {
17
+
18
+
19
+
20
+ if ( attrs . tilesource ) {
21
+ opts . tileSources = [ attrs . tilesource ] ;
22
+ }
23
+ if ( attrs . prefixUrl ) {
24
+ opts . prefixUrl = attrs . prefixUrl ;
25
+ }
26
+
27
+ function _bootstrap ( ) {
28
+ if ( scope . osd ) {
29
+ scope . osd . destroy ( ) ;
30
+ scope . osd = null ;
31
+ }
32
+ //Create options object
33
+ var opts = angular . extend ( { } , scope . options , {
34
+ id : "openseadragon-" + Math . random ( ) ,
35
+ element : element [ 0 ] ,
36
+ } ) ;
37
+ //Create the viewer
38
+ scope . osd = OpenSeadragon ( opts ) ;
39
+ //Create a wrapper
40
+ var wrapper = {
41
+ mouse : {
42
+ position : null ,
43
+ imageCoord : null ,
44
+ viewportCoord : null ,
45
+ } ,
46
+ zoom : 0 ,
47
+ viewport : { }
48
+ }
49
+
50
+ for ( var key in scope . osd ) {
51
+ wrapper [ key ] = scope . osd [ key ] ;
52
+ }
53
+
54
+ if ( attrs . name ) {
55
+ //Make the OSD available to parent scope
56
+ scope . $parent [ attrs . name ] = wrapper ;
57
+ //Define event handlers
58
+ zoomHandler = function ( e ) {
59
+ $timeout ( function ( ) {
60
+ scope . $apply ( function ( ) {
61
+ wrapper . zoom = e . zoom ;
62
+ } ) ;
63
+ } , 0 ) ;
64
+ }
65
+ updateViewportHandler = function ( e ) {
66
+ scope . $apply ( function ( ) {
67
+ wrapper . viewportInfo = {
68
+ bounds : scope . osd . viewport . getBounds ( false ) ,
69
+ center : scope . osd . viewport . getCenter ( false ) ,
70
+ rotation : scope . osd . viewport . getRotation ( ) ,
71
+ zoom : scope . osd . viewport . getZoom ( false ) ,
72
+ } ;
73
+ } ) ;
74
+ }
75
+
76
+ //Assign event handlers
77
+ scope . osd . addHandler ( "zoom" , zoomHandler ) ;
78
+ scope . osd . addHandler ( "update-viewport" , updateViewportHandler ) ;
79
+
80
+ //Add a mouse handler
81
+ scope . mouse = new OpenSeadragon . MouseTracker ( {
82
+ element : scope . osd . canvas ,
83
+ enterHandler : function ( e ) {
84
+ if ( scope . osd . viewport ) {
85
+ var coord = OpenSeadragon . getElementPosition ( scope . osd . canvas ) ;
86
+ var pos = e . position . plus ( coord ) ;
87
+ var mouse = {
88
+ position : pos ,
89
+ imageCoord : scope . osd . viewport . windowToImageCoordinates ( pos ) ,
90
+ viewportCoord : scope . osd . viewport . windowToViewportCoordinates ( pos ) ,
91
+ }
92
+ scope . $apply ( function ( ) {
93
+ wrapper . mouse = mouse ;
94
+ } ) ;
95
+ }
96
+ } ,
97
+ moveHandler : function ( e ) {
98
+ if ( scope . osd . viewport ) {
99
+ var coord = OpenSeadragon . getElementPosition ( scope . osd . canvas ) ;
100
+ var pos = e . position . plus ( coord ) ;
101
+ var mouse = {
102
+ position : pos ,
103
+ imageCoord : scope . osd . viewport . windowToImageCoordinates ( pos ) ,
104
+ viewportCoord : scope . osd . viewport . windowToViewportCoordinates ( pos ) ,
105
+ }
106
+ scope . $apply ( function ( ) {
107
+ wrapper . mouse = mouse ;
108
+ } ) ;
109
+ }
110
+ } ,
111
+ exitHandler : function ( e ) {
112
+ scope . $apply ( function ( ) {
113
+ wrapper . mouse . position = null ;
114
+ wrapper . mouse . imageCoord = null ;
115
+ wrapper . mouse . viewportCoord = null ;
116
+ } ) ;
117
+ } ,
118
+ } ) ;
119
+ scope . mouse . setTracking ( true ) ;
120
+ }
121
+
122
+ }
123
+ _bootstrap ( ) ;
124
+ var optionsWatcher = scope . $watch ( 'options' , _bootstrap ) ;
125
+
126
+ //if @name is set, put the wrapper in the scope and handle the events
127
+ var zoomHandler = null ;
128
+ var updateViewportHandler = null ;
129
+
130
+
131
+ //When element is destroyed, destroy the viewer
132
+ element . on ( '$destroy' , function ( ) {
133
+ //if @nam eis set, remove it from parent scope, and remove event handlers
134
+ if ( attrs . name ) {
135
+ //Remove from parent scope
136
+ scope . $parent [ attrs . name ] = null ;
137
+
138
+ //Destroy mouse handler
139
+ scope . mouse . destroy ( ) ;
140
+ optionsWatcher ( ) ;
141
+ //Remove event handlers
142
+ scope . osd . removeHandler ( "zoom" , zoomHandler ) ;
143
+ scope . osd . removeHandler ( "update-viewport" , updateViewportHandler ) ;
144
+ }
145
+
146
+ //Destroy the viewer
147
+ scope . osd . destroy ( ) ;
148
+ } ) ;
149
+ } ,
150
+ } ;
151
+ } ] ) ;
152
+ } ) ( ) ;
0 commit comments