| 1 |
const CourseCurriculum = { |
| 2 |
SORT_SECTION( state, orders ) { |
| 3 |
state.sections = state.sections.map( function( section ) { |
| 4 |
section.order = orders[ section.id ]; |
| 5 |
|
| 6 |
return section; |
| 7 |
} ); |
| 8 |
}, |
| 9 |
SET_SECTIONS( state, sections ) { |
| 10 |
state.sections = sections; |
| 11 |
}, |
| 12 |
ADD_NEW_SECTION( state, newSection ) { |
| 13 |
if ( newSection.open === undefined ) { |
| 14 |
newSection.open = true; |
| 15 |
} |
| 16 |
let pos; |
| 17 |
|
| 18 |
if ( newSection.temp_id ) { |
| 19 |
state.sections.map( function( section, i ) { |
| 20 |
if ( newSection.temp_id == section.id ) { |
| 21 |
pos = i; |
| 22 |
return false; |
| 23 |
} |
| 24 |
} ); |
| 25 |
} |
| 26 |
|
| 27 |
if ( pos !== undefined ) { |
| 28 |
$Vue.set( state.sections, pos, newSection ); |
| 29 |
} else { |
| 30 |
state.sections.push( newSection ); |
| 31 |
} |
| 32 |
}, |
| 33 |
ADD_EMPTY_SECTION( state, section ) { |
| 34 |
section.open = true; |
| 35 |
state.sections.push( section ); |
| 36 |
}, |
| 37 |
REMOVE_SECTION( state, index ) { |
| 38 |
state.sections.splice( index, 1 ); |
| 39 |
}, |
| 40 |
REMOVE_SECTION_ITEM( state, payload ) { |
| 41 |
const section = state.sections.find( function( section ) { |
| 42 |
return ( section.id === payload.section_id ); |
| 43 |
} ); |
| 44 |
|
| 45 |
let items = section.items || [], |
| 46 |
item = payload.item, |
| 47 |
index = -1; |
| 48 |
items.forEach( function( it, i ) { |
| 49 |
if ( it.id === item.id ) { |
| 50 |
index = i; |
| 51 |
} |
| 52 |
} ); |
| 53 |
|
| 54 |
if ( index !== -1 ) { |
| 55 |
if ( payload.oldId !== undefined ) { |
| 56 |
items[ index ].id = payload.oldId; |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if ( item.temp_id ) { |
| 61 |
items[ index ].id = item.temp_id; |
| 62 |
} else { |
| 63 |
items.splice( index, 1 ); |
| 64 |
} |
| 65 |
} |
| 66 |
}, |
| 67 |
UPDATE_SECTION_ITEMS( state, payload ) { |
| 68 |
const section = state.sections.find( function( section ) { |
| 69 |
return parseInt( section.id ) === parseInt( payload.section_id ); |
| 70 |
} ); |
| 71 |
|
| 72 |
if ( ! section ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
section.items = payload.items; |
| 76 |
}, |
| 77 |
UPDATE_SECTION_ITEM( state, payload ) { |
| 78 |
|
| 79 |
}, |
| 80 |
|
| 81 |
CLOSE_SECTION( state, section ) { |
| 82 |
state.sections.forEach( function( _section, index ) { |
| 83 |
if ( section.id === _section.id ) { |
| 84 |
state.sections[ index ].open = false; |
| 85 |
} |
| 86 |
} ); |
| 87 |
}, |
| 88 |
|
| 89 |
OPEN_SECTION( state, section ) { |
| 90 |
state.sections.forEach( function( _section, index ) { |
| 91 |
if ( section.id === _section.id ) { |
| 92 |
state.sections[ index ].open = true; |
| 93 |
} |
| 94 |
} ); |
| 95 |
}, |
| 96 |
|
| 97 |
OPEN_ALL_SECTIONS( state ) { |
| 98 |
state.sections = state.sections.map( function( _section ) { |
| 99 |
_section.open = true; |
| 100 |
|
| 101 |
return _section; |
| 102 |
} ); |
| 103 |
}, |
| 104 |
|
| 105 |
CLOSE_ALL_SECTIONS( state ) { |
| 106 |
state.sections = state.sections.map( function( _section ) { |
| 107 |
_section.open = false; |
| 108 |
|
| 109 |
return _section; |
| 110 |
} ); |
| 111 |
}, |
| 112 |
|
| 113 |
UPDATE_SECTION_REQUEST( state, sectionId ) { |
| 114 |
$Vue.set( state.statusUpdateSection, sectionId, 'updating' ); |
| 115 |
}, |
| 116 |
|
| 117 |
UPDATE_SECTION_SUCCESS( state, sectionId ) { |
| 118 |
$Vue.set( state.statusUpdateSection, sectionId, 'successful' ); |
| 119 |
}, |
| 120 |
|
| 121 |
UPDATE_SECTION_FAILURE( state, sectionId ) { |
| 122 |
$Vue.set( state.statusUpdateSection, sectionId, 'failed' ); |
| 123 |
}, |
| 124 |
|
| 125 |
UPDATE_SECTION_ITEM_REQUEST( state, itemId ) { |
| 126 |
$Vue.set( state.statusUpdateSectionItem, itemId, 'updating' ); |
| 127 |
}, |
| 128 |
|
| 129 |
UPDATE_SECTION_ITEM_SUCCESS( state, itemId ) { |
| 130 |
$Vue.set( state.statusUpdateSectionItem, itemId, 'successful' ); |
| 131 |
}, |
| 132 |
|
| 133 |
UPDATE_SECTION_ITEM_FAILURE( state, itemId ) { |
| 134 |
$Vue.set( state.statusUpdateSectionItem, itemId, 'failed' ); |
| 135 |
}, |
| 136 |
APPEND_EMPTY_ITEM_TO_SECTION( state, data ) { |
| 137 |
const section = state.sections.find( function( section ) { |
| 138 |
return parseInt( section.id ) === parseInt( data.section_id ); |
| 139 |
} ); |
| 140 |
|
| 141 |
if ( ! section ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
section.items.push( { id: data.item.id, title: data.item.title, type: 'empty-item' } ); |
| 146 |
}, |
| 147 |
UPDATE_ITEM_SECTION_BY_ID( state, data ) { |
| 148 |
const section = state.sections.find( function( section ) { |
| 149 |
return parseInt( section.id ) === parseInt( data.section_id ); |
| 150 |
} ); |
| 151 |
|
| 152 |
if ( ! section ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
for ( let i = 0; i < section.items.length; i++ ) { |
| 157 |
try { |
| 158 |
if ( ! section.items[ i ] ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
const item_id = section.items[ i ].id; |
| 163 |
|
| 164 |
if ( item_id ) { |
| 165 |
if ( data.items[ item_id ] ) { |
| 166 |
$Vue.set( section.items, i, data.items[ item_id ] ); |
| 167 |
} |
| 168 |
} |
| 169 |
} catch ( ex ) { |
| 170 |
console.log( ex ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
//section.items.push({id: data.item.id, title: data.item.title, type: 'empty-item'}); |
| 175 |
}, |
| 176 |
}; |
| 177 |
|
| 178 |
export default CourseCurriculum; |
| 179 |
|