@@ -22,7 +22,6 @@ export default class SitemapController {
2222 this . store = store ;
2323 this . templateDir = templateDir ;
2424 this . contentScript = getContentScript ( 'DevTools' ) ;
25-
2625 this . selectorTypes = [
2726 {
2827 type : 'SelectorText' ,
@@ -191,6 +190,16 @@ export default class SitemapController {
191190 '#sitemap-export-data-nav-button' : {
192191 click : this . showSitemapExportDataPanel ,
193192 } ,
193+ '.delete_selector_submit' : {
194+ click : this . confirmDeleteSelector ,
195+ } ,
196+ '.delete_sitemap_submit' : {
197+ click : this . confirmDeleteSitemap ,
198+ } ,
199+ '.copy_sitemap_submit' : {
200+ click : this . confirmCopySitemap ,
201+ } ,
202+
194203 '#submit-create-sitemap' : {
195204 click : this . createSitemap ,
196205 } ,
@@ -221,6 +230,9 @@ export default class SitemapController {
221230 '#sitemaps button[action=delete-sitemap]' : {
222231 click : this . deleteSitemap ,
223232 } ,
233+ '#sitemaps button[action=create-copy-sitemap]' : {
234+ click : this . copySitemap ,
235+ } ,
224236 '#sitemap-scrape-nav-button' : {
225237 click : this . showScrapeSitemapConfigPanel ,
226238 } ,
@@ -340,16 +352,16 @@ export default class SitemapController {
340352 /**
341353 * Returns bootstrapValidator object for current form in viewport
342354 */
343- getFormValidator ( ) {
344- return $ ( '#viewport form' ) . data ( 'bootstrapValidator' ) ;
355+ getFormValidator ( selector = '#viewport form' ) {
356+ return $ ( selector ) . data ( 'bootstrapValidator' ) ;
345357 }
346358
347359 /**
348360 * Returns whether current form in the viewport is valid
349361 * @returns {Boolean }
350362 */
351- isValidForm ( ) {
352- const validator = this . getFormValidator ( ) ;
363+ isValidForm ( selector = '#viewport form' ) {
364+ const validator = this . getFormValidator ( selector ) ;
353365 // validator.validate();
354366 // validate method calls submit which is not needed in this case.
355367 for ( const field in validator . options . fields ) {
@@ -439,6 +451,34 @@ export default class SitemapController {
439451 return true ;
440452 }
441453
454+ initCopySitemapValidation ( ) {
455+ $ ( '#confirm-action-modal' ) . bootstrapValidator ( {
456+ fields : {
457+ modal_confirm_action_input_copy_sitemap : {
458+ validators : {
459+ notEmpty : {
460+ message : Translator . getTranslationByKey ( 'sitemapid_empty_message' ) ,
461+ } ,
462+ stringLength : {
463+ min : 3 ,
464+ message : Translator . getTranslationByKey ( 'sitemapid_short_message' ) ,
465+ } ,
466+ regexp : {
467+ regexp : / ^ [ a - z ] [ a - z 0 - 9 _ \$ \( \) \+ \- / ] + $ / ,
468+ message : Translator . getTranslationByKey ( 'sitemapid_invalid_char' ) ,
469+ } ,
470+ callback : {
471+ message : Translator . getTranslationByKey ( 'sitemapid_repeated_id' ) ,
472+ callback ( value , validator ) {
473+ return true ;
474+ } ,
475+ } ,
476+ } ,
477+ } ,
478+ } ,
479+ } ) ;
480+ }
481+
442482 initImportSitemapValidation ( ) {
443483 $ ( '#viewport form' ) . bootstrapValidator ( {
444484 fields : {
@@ -1203,16 +1243,40 @@ export default class SitemapController {
12031243 initConfirmActionPanel ( action ) {
12041244 $ ( '#confirm-action-modal' ) . remove ( ) ; // remove old panel
12051245 $ ( '#viewport' ) . after ( ich . ActionConfirm ( action ) ) ;
1246+ $ ( '#confirm-action-modal' ) . modal ( 'show' ) ;
12061247 Translator . translatePage ( ) ;
12071248 }
12081249
1209- showConfirmActionPanel ( onConfirm ) {
1210- const $confirmActionModal = $ ( '#confirm-action-modal' ) ;
1211- $ ( '#modal-submit' ) . click ( ( ) => {
1212- $confirmActionModal . modal ( 'hide' ) ;
1213- onConfirm ( ) ;
1214- } ) ;
1215- $confirmActionModal . modal ( 'show' ) ;
1250+ async copySitemap ( button ) {
1251+ let sitemap = $ ( button ) . closest ( 'tr' ) . data ( 'sitemap' ) ;
1252+ this . initConfirmActionPanel ( { action : 'copy_sitemap' } ) ;
1253+ this . initCopySitemapValidation ( ) ;
1254+ $ ( '#modal-message' ) . show ( ) ;
1255+ $ ( '#modal-sitemap-id' ) . text ( sitemap . _id ) ;
1256+ this . state . currentSitemap = sitemap ;
1257+ $ ( '#modal_confirm_action_input_copy_sitemap' ) . show ( ) ;
1258+ }
1259+
1260+ async confirmCopySitemap ( button ) {
1261+ const id = $ ( '#modal_confirm_action_input_copy_sitemap' ) . val ( ) ;
1262+ let sitemap = this . state . currentSitemap ;
1263+ if ( ! this . isValidForm ( '#confirm-action-modal' ) ) {
1264+ return false ;
1265+ }
1266+ const sitemapExist = await this . store . sitemapExists ( id ) ;
1267+ if ( sitemapExist ) {
1268+ const validator = $ ( '#confirm-action-modal' ) . data ( 'bootstrapValidator' ) ;
1269+ validator . updateStatus (
1270+ 'modal_confirm_action_input_copy_sitemap' ,
1271+ 'INVALID' ,
1272+ 'callback'
1273+ ) ;
1274+ return false ;
1275+ }
1276+ sitemap = new Sitemap ( id , sitemap . startUrls , sitemap . model , sitemap . selectors ) ;
1277+ sitemap = await this . store . createSitemap ( sitemap ) ;
1278+ this . _editSitemap ( sitemap , [ '_root' ] ) ;
1279+ $ ( '#confirm-action-modal' ) . modal ( 'hide' ) ;
12161280 }
12171281
12181282 async deleteSelector ( button ) {
@@ -1225,21 +1289,30 @@ export default class SitemapController {
12251289 $ ( '#modal-child-count' ) . text ( childCount ) ;
12261290 $ ( '#modal-message' ) . show ( ) ;
12271291 }
1228- this . showConfirmActionPanel ( async ( ) => {
1229- sitemap . deleteSelector ( selector ) ;
1230- await this . store . saveSitemap ( sitemap ) ;
1231- this . showSitemapSelectorList ( ) ;
1232- } ) ;
1292+ this . state . currentSelector = selector ;
1293+ this . state . currentSitemap = sitemap ;
1294+ }
1295+
1296+ async confirmDeleteSelector ( button ) {
1297+ const selector = this . state . currentSelector ;
1298+ const sitemap = this . state . currentSitemap ;
1299+ sitemap . deleteSelector ( selector ) ;
1300+ await this . store . saveSitemap ( sitemap ) ;
1301+ this . showSitemapSelectorList ( ) ;
1302+ $ ( '#confirm-action-modal' ) . modal ( 'hide' ) ;
12331303 }
12341304
12351305 async deleteSitemap ( button ) {
12361306 const sitemap = $ ( button ) . closest ( 'tr' ) . data ( 'sitemap' ) ;
12371307 this . initConfirmActionPanel ( { action : 'delete_sitemap' } ) ;
12381308 $ ( '#modal-sitemap-id' ) . text ( sitemap . _id ) ;
1239- this . showConfirmActionPanel ( async ( ) => {
1240- await this . store . deleteSitemap ( sitemap ) ;
1241- await this . showSitemaps ( ) ;
1242- } ) ;
1309+ this . state . currentSitemap = sitemap ;
1310+ }
1311+
1312+ async confirmDeleteSitemap ( button ) {
1313+ await this . store . deleteSitemap ( this . state . currentSitemap ) ;
1314+ await this . showSitemaps ( ) ;
1315+ $ ( '#confirm-action-modal' ) . modal ( 'hide' ) ;
12431316 }
12441317
12451318 initScrapeSitemapConfigValidation ( ) {
0 commit comments