@@ -2302,46 +2302,75 @@ function initNavbarContentToggle() {
23022302}
23032303
23042304function 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 ;
23092312
23102313 mgrBtn . click ( function ( ) {
23112314 viewDiv . hide ( ) ;
23122315 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+ }
23142327
23152328 saveBtn . click ( function ( ) {
23162329 var topics = $ ( "input[name=topics]" ) . val ( ) ;
23172330
2318- $ . post ( $ ( this ) . data ( 'link' ) , {
2331+ $ . post ( saveBtn . data ( 'link' ) , {
23192332 "_csrf" : csrf ,
23202333 "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' ) {
23252336 viewDiv . children ( ".topic" ) . remove ( ) ;
2326- if ( topics . length == 0 ) {
2337+ if ( topics . length === 0 ) {
23272338 return
23282339 }
23292340 var topicArray = topics . split ( "," ) ;
23302341
23312342 var last = viewDiv . children ( "a" ) . last ( ) ;
2332- for ( var i = 0 ; i < topicArray . length ; i ++ ) {
2343+ for ( var i = 0 ; i < topicArray . length ; i ++ ) {
23332344 $ ( '<div class="ui green basic label topic" style="cursor:pointer;">' + topicArray [ i ] + '</div>' ) . insertBefore ( last )
23342345 }
2346+ editDiv . hide ( ) ;
2347+ viewDiv . show ( ) ;
23352348 }
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+ } ) ;
23422371 } ) ;
23432372
2344- $ ( '#topic_edit .dropdown' ) . dropdown ( {
2373+ topicDropdown . dropdown ( {
23452374 allowAdditions : true ,
23462375 fields : { name : "description" , value : "data-value" } ,
23472376 saveRemoteData : false ,
@@ -2362,7 +2391,7 @@ function initTopicbar() {
23622391 onResponse : function ( res ) {
23632392 var formattedResponse = {
23642393 success : false ,
2365- results : new Array ( ) ,
2394+ results : [ ] ,
23662395 } ;
23672396
23682397 if ( res . topics ) {
@@ -2375,7 +2404,48 @@ function initTopicbar() {
23752404 return formattedResponse ;
23762405 } ,
23772406 } ,
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+ }
23782417 } ) ;
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+ } ) ;
23792449}
23802450function toggleDuedateForm ( ) {
23812451 $ ( '#add_deadline_form' ) . fadeToggle ( 150 ) ;
0 commit comments