1- define ( [ 'connectionManager' , 'confirm' , 'appRouter' , 'globalize' ] , function ( connectionManager , confirm , appRouter , globalize ) {
1+ define ( [ 'connectionManager' , 'confirm' , 'dialog' , ' appRouter', 'globalize' ] , function ( connectionManager , confirm , dialog , appRouter , globalize ) {
22 'use strict' ;
33
44 function alertText ( options ) {
@@ -126,36 +126,73 @@ define(['connectionManager', 'confirm', 'appRouter', 'globalize'], function (con
126126 } ) ;
127127 }
128128
129- function deleteItem ( options ) {
129+ function getRejectPromise ( ) {
130+ return Promise . reject ( ) ;
131+ }
130132
131- var item = options . item ;
132- var apiClient = connectionManager . getApiClient ( item ) ;
133+ function deleteSeries ( item , apiClient , options ) {
133134
134- if ( item . Type === 'Device' ) {
135- return deleteDevice ( item , apiClient , options ) ;
136- }
135+ return apiClient . getEpisodes ( item . Id , {
137136
138- if ( item . Type === 'Server' ) {
139- return deleteServer ( item , apiClient , options ) ;
140- }
137+ Limit : 1 ,
138+ SortBy : 'DatePlayed' ,
139+ SortOrder : 'Descending' ,
140+ IsPlayed : true ,
141+ UserId : apiClient . getCurrentUserId ( ) ,
142+ ExcludeLocationTypes : 'Virtual'
141143
142- if ( item . Type === 'User' ) {
143- return deleteUser ( item , apiClient , options ) ;
144- }
144+ } ) . then ( function ( result ) {
145145
146- if ( item . Type === 'Plugin' ) {
147- return uninstallPlugin ( item , apiClient , options ) ;
148- }
146+ if ( ! result . Items . length ) {
147+ return deleteItemInternal ( item , apiClient , options ) ;
148+ }
149149
150- var itemId = item . Id ;
151- var parentId = item . SeasonId || item . SeriesId || item . ParentId ;
152- var serverId = item . ServerId ;
150+ return dialog ( {
151+
152+ title : globalize . translate ( 'HeaderDeleteSeries' ) ,
153+ text : '' ,
154+ buttons : [
155+
156+ {
157+ name : globalize . translate ( 'Cancel' ) ,
158+ id : 'cancel' ,
159+ type : 'submit'
160+ } ,
161+ {
162+ name : globalize . translate ( 'HeaderDeleteLastPlayedEpisode' ) ,
163+ id : 'deletelastplayed' ,
164+ type : 'cancel'
165+ } ,
166+ {
167+ name : globalize . translate ( 'HeaderDeleteSeries' ) ,
168+ id : 'deleteseries' ,
169+ type : 'cancel'
170+ }
171+ ]
172+
173+ } ) . then ( function ( id ) {
174+
175+ if ( id === 'deleteseries' ) {
176+ return deleteItemInternal ( item , apiClient , options ) ;
177+ }
178+
179+ if ( id === 'deletelastplayed' ) {
180+ return deleteItemInternal ( result . Items [ 0 ] , apiClient , options ) ;
181+ }
182+
183+ return Promise . reject ( ) ;
184+ } ) ;
185+ } ) ;
186+ }
187+
188+ function deleteItemInternal ( item , apiClient , options ) {
153189
154- var msg = globalize . translate ( 'ConfirmDeleteItem' ) ;
155- var title = globalize . translate ( 'HeaderDeleteItem' ) ;
190+ var itemId = item . Id ;
156191
157192 return apiClient . getDeleteInfo ( itemId ) . then ( function ( deleteInfo ) {
158193
194+ var msg = globalize . translate ( 'ConfirmDeleteItem' ) ;
195+
159196 if ( deleteInfo . Paths . length ) {
160197
161198 msg += '\n\n' + globalize . translate ( 'FollowingFilesWillBeDeleted' ) + '\n' + deleteInfo . Paths . join ( '\n' ) ;
@@ -165,13 +202,16 @@ define(['connectionManager', 'confirm', 'appRouter', 'globalize'], function (con
165202
166203 return confirm ( {
167204
168- title : title ,
205+ title : globalize . translate ( 'HeaderDeleteItem' ) ,
169206 text : msg ,
170207 confirmText : globalize . translate ( 'Delete' ) ,
171208 primary : 'cancel'
172209
173210 } ) . then ( function ( ) {
174211
212+ var parentId = item . SeasonId || item . SeriesId || item . ParentId ;
213+ var serverId = item . ServerId ;
214+
175215 return apiClient . deleteItem ( itemId ) . then ( function ( ) {
176216
177217 return onItemDeleted ( options , serverId , parentId ) ;
@@ -188,6 +228,34 @@ define(['connectionManager', 'confirm', 'appRouter', 'globalize'], function (con
188228 } ) ;
189229 }
190230
231+ function deleteItem ( options ) {
232+
233+ var item = options . item ;
234+ var apiClient = connectionManager . getApiClient ( item ) ;
235+
236+ if ( item . Type === 'Device' ) {
237+ return deleteDevice ( item , apiClient , options ) ;
238+ }
239+
240+ if ( item . Type === 'Server' ) {
241+ return deleteServer ( item , apiClient , options ) ;
242+ }
243+
244+ if ( item . Type === 'User' ) {
245+ return deleteUser ( item , apiClient , options ) ;
246+ }
247+
248+ if ( item . Type === 'Plugin' ) {
249+ return uninstallPlugin ( item , apiClient , options ) ;
250+ }
251+
252+ if ( item . Type === 'Series' && apiClient . isMinServerVersion ( '4.5.0.3' ) ) {
253+ return deleteSeries ( item , apiClient , options ) ;
254+ }
255+
256+ return deleteItemInternal ( item , apiClient , options ) ;
257+ }
258+
191259 return {
192260 deleteItem : deleteItem
193261 } ;
0 commit comments