@@ -2302,46 +2302,75 @@ function initNavbarContentToggle() {
2302
2302
}
2303
2303
2304
2304
function initTopicbar ( ) {
2305
- var mgrBtn = $ ( "#manage_topic" )
2306
- var editDiv = $ ( "#topic_edit" )
2307
- var viewDiv = $ ( "#repo-topic" )
2308
- var saveBtn = $ ( "#save_topic" )
2305
+ var mgrBtn = $ ( "#manage_topic" ) ,
2306
+ editDiv = $ ( "#topic_edit" ) ,
2307
+ viewDiv = $ ( "#repo-topic" ) ,
2308
+ saveBtn = $ ( "#save_topic" ) ,
2309
+ topicDropdown = $ ( '#topic_edit .dropdown' ) ,
2310
+ topicForm = $ ( '#topic_edit.ui.form' ) ,
2311
+ topicPrompts ;
2309
2312
2310
2313
mgrBtn . click ( function ( ) {
2311
2314
viewDiv . hide ( ) ;
2312
2315
editDiv . show ( ) ;
2313
- } )
2316
+ } ) ;
2317
+
2318
+ function getPrompts ( ) {
2319
+ var hidePrompt = $ ( "div.hide#validate_prompt" ) ,
2320
+ prompts = {
2321
+ countPrompt : hidePrompt . children ( '#count_prompt' ) . text ( ) ,
2322
+ formatPrompt : hidePrompt . children ( '#format_prompt' ) . text ( )
2323
+ } ;
2324
+ hidePrompt . remove ( ) ;
2325
+ return prompts ;
2326
+ }
2314
2327
2315
2328
saveBtn . click ( function ( ) {
2316
2329
var topics = $ ( "input[name=topics]" ) . val ( ) ;
2317
2330
2318
- $ . post ( $ ( this ) . data ( 'link' ) , {
2331
+ $ . post ( saveBtn . data ( 'link' ) , {
2319
2332
"_csrf" : csrf ,
2320
2333
"topics" : topics
2321
- } ) . success ( function ( res ) {
2322
- if ( res [ "status" ] != "ok" ) {
2323
- alert ( res . message ) ;
2324
- } else {
2334
+ } , function ( data , textStatus , xhr ) {
2335
+ if ( xhr . responseJSON . status === 'ok' ) {
2325
2336
viewDiv . children ( ".topic" ) . remove ( ) ;
2326
- if ( topics . length == 0 ) {
2337
+ if ( topics . length === 0 ) {
2327
2338
return
2328
2339
}
2329
2340
var topicArray = topics . split ( "," ) ;
2330
2341
2331
2342
var last = viewDiv . children ( "a" ) . last ( ) ;
2332
- for ( var i = 0 ; i < topicArray . length ; i ++ ) {
2343
+ for ( var i = 0 ; i < topicArray . length ; i ++ ) {
2333
2344
$ ( '<div class="ui green basic label topic" style="cursor:pointer;">' + topicArray [ i ] + '</div>' ) . insertBefore ( last )
2334
2345
}
2346
+ editDiv . hide ( ) ;
2347
+ viewDiv . show ( ) ;
2335
2348
}
2336
- } ) . done ( function ( ) {
2337
- editDiv . hide ( ) ;
2338
- viewDiv . show ( ) ;
2339
- } ) . fail ( function ( xhr ) {
2340
- alert ( xhr . responseJSON . message )
2341
- } )
2349
+ } ) . fail ( function ( xhr ) {
2350
+ if ( xhr . status === 422 ) {
2351
+ if ( xhr . responseJSON . invalidTopics . length > 0 ) {
2352
+ topicPrompts . formatPrompt = xhr . responseJSON . message ;
2353
+
2354
+ var invalidTopics = xhr . responseJSON . invalidTopics ,
2355
+ topicLables = topicDropdown . children ( 'a.ui.label' ) ;
2356
+
2357
+ topics . split ( ',' ) . forEach ( function ( value , index ) {
2358
+ for ( var i = 0 ; i < invalidTopics . length ; i ++ ) {
2359
+ if ( invalidTopics [ i ] === value ) {
2360
+ topicLables . eq ( index ) . removeClass ( "green" ) . addClass ( "red" ) ;
2361
+ }
2362
+ }
2363
+ } ) ;
2364
+ } else {
2365
+ topicPrompts . countPrompt = xhr . responseJSON . message ;
2366
+ }
2367
+ }
2368
+ } ) . always ( function ( ) {
2369
+ topicForm . form ( 'validate form' ) ;
2370
+ } ) ;
2342
2371
} ) ;
2343
2372
2344
- $ ( '#topic_edit .dropdown' ) . dropdown ( {
2373
+ topicDropdown . dropdown ( {
2345
2374
allowAdditions : true ,
2346
2375
fields : { name : "description" , value : "data-value" } ,
2347
2376
saveRemoteData : false ,
@@ -2362,7 +2391,7 @@ function initTopicbar() {
2362
2391
onResponse : function ( res ) {
2363
2392
var formattedResponse = {
2364
2393
success : false ,
2365
- results : new Array ( ) ,
2394
+ results : [ ] ,
2366
2395
} ;
2367
2396
2368
2397
if ( res . topics ) {
@@ -2375,7 +2404,48 @@ function initTopicbar() {
2375
2404
return formattedResponse ;
2376
2405
} ,
2377
2406
} ,
2407
+ onLabelCreate : function ( value ) {
2408
+ value = value . toLowerCase ( ) . trim ( ) ;
2409
+ this . attr ( "data-value" , value ) . contents ( ) . first ( ) . replaceWith ( value ) ;
2410
+ return $ ( this ) ;
2411
+ } ,
2412
+ onAdd : function ( addedValue , addedText , $addedChoice ) {
2413
+ addedValue = addedValue . toLowerCase ( ) . trim ( ) ;
2414
+ $ ( $addedChoice ) . attr ( 'data-value' , addedValue ) ;
2415
+ $ ( $addedChoice ) . attr ( 'data-text' , addedValue ) ;
2416
+ }
2378
2417
} ) ;
2418
+
2419
+ $ . fn . form . settings . rules . validateTopic = function ( values , regExp ) {
2420
+ var topics = topicDropdown . children ( 'a.ui.label' ) ,
2421
+ status = topics . length === 0 || topics . last ( ) . attr ( "data-value" ) . match ( regExp ) ;
2422
+ if ( ! status ) {
2423
+ topics . last ( ) . removeClass ( "green" ) . addClass ( "red" ) ;
2424
+ }
2425
+ return status && topicDropdown . children ( 'a.ui.label.red' ) . length === 0 ;
2426
+ } ;
2427
+
2428
+ topicPrompts = getPrompts ( ) ;
2429
+ topicForm . form ( {
2430
+ on : 'change' ,
2431
+ inline : true ,
2432
+ fields : {
2433
+ topics : {
2434
+ identifier : 'topics' ,
2435
+ rules : [
2436
+ {
2437
+ type : 'validateTopic' ,
2438
+ value : / ^ [ a - z 0 - 9 ] [ a - z 0 - 9 - ] { 1 , 35 } $ / ,
2439
+ prompt : topicPrompts . formatPrompt
2440
+ } ,
2441
+ {
2442
+ type : 'maxCount[25]' ,
2443
+ prompt : topicPrompts . countPrompt
2444
+ }
2445
+ ]
2446
+ } ,
2447
+ }
2448
+ } ) ;
2379
2449
}
2380
2450
function toggleDuedateForm ( ) {
2381
2451
$ ( '#add_deadline_form' ) . fadeToggle ( 150 ) ;
0 commit comments