Created
June 2, 2016 08:20
-
-
Save Panchev/02cc6a9b32c02eda15c70aefa44b39b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Renders a list of segments. | |
* | |
* @memberOf CourseBuilder.segments | |
* @fileOverview segments/components/segments/segments.directive.js | |
*/ | |
;(function() { | |
'use strict'; | |
angular | |
.module('CourseBuilder.segments') | |
.directive('cbSegments', directive); | |
/** | |
* @return {Object} | |
*/ | |
function directive() { | |
/** | |
* The host object. | |
* | |
* @type {Object} | |
*/ | |
var directive = { | |
scope : {}, | |
restrict : 'E', | |
replace : true, | |
templateUrl : template(), | |
controller : controller, | |
controllerAs : 'vm', | |
bindToController: { | |
segments : '=' | |
} | |
}; | |
return directive; | |
/** | |
* Inject the needed dependencies. | |
* | |
* @type {Array} | |
*/ | |
controller.$inject = [ | |
'$rootScope', | |
'$state', | |
'SegmentsSrv', | |
'_', | |
'SlidesSrv', | |
'SEGMENT' | |
]; | |
/** | |
* The controller of this directive. | |
* | |
* @param {Object} $rootScope | |
* @param {Object} $state | |
* @param {Object} SegmentsSrv | |
* @param {Object} _ | |
* @param {Object} SlidesSrv | |
* @param {Object} SEGMENT | |
* @return {void} | |
*/ | |
function controller($rootScope, $state, SegmentsSrv, _, SlidesSrv, SEGMENT) { | |
/** | |
* vm stands for ViewModel. | |
*/ | |
var vm = this; | |
/** | |
* Scope variables and methods. | |
* | |
* @type {mixed} | |
*/ | |
vm.sortableOptions = getSortableOptions(); | |
vm.onChildSort = onChildSort; | |
/** | |
* Return the configuration object for jQuery UI Sortable. | |
* | |
* @return {Object} | |
*/ | |
function getSortableOptions() { | |
var changed = false; | |
return { | |
cursor: 'move', | |
revert: true, | |
axis: 'y', | |
placeholder: 'cb-menu-item ui-sortable-placeholder', | |
update: function() { | |
changed = true; | |
}, | |
stop: function() { | |
if (!changed) { | |
return; | |
} | |
changed = false; | |
SegmentsSrv | |
.order($state.params.courseId, _.pluck(vm.segments, 'id')) | |
.then(function() { | |
$rootScope.$broadcast(SEGMENT.EVENTS.ORDERED); | |
}); | |
} | |
}; | |
} | |
/** | |
* Save the children sort. | |
* | |
* @return {void} | |
*/ | |
function onChildSort() { | |
var sorted = {}; | |
_.forEach(vm.segments, function(segment) { | |
if (segment.children) { | |
sorted[segment.id] = []; | |
_.forEach(segment.children, function(child) { | |
sorted[segment.id].push(child.id); | |
}); | |
}; | |
}); | |
SlidesSrv | |
.order(sorted) | |
.then(function() { | |
$rootScope.$broadcast(SEGMENT.EVENTS.ORDERED); | |
}); | |
} | |
} | |
/** | |
* Returns the template used by directive. | |
* | |
* @return {String} | |
*/ | |
function template() { | |
return 'segments/components/segments/segments.html'; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment