angular .module('EssayApp.controllers') .controller('LanguagesController', ['$scope', 'dataService', '$window', 'utils', 'ngProgress', ($scope, dataService, $window, utils, ngProgress) -> ngProgress.start() $scope.ready = false $scope.forms = {} $scope.customer = src_language: undefined dst_language: undefined $scope.data = language_from_groups: [] state: undefined dst_language_title: '' $scope.STATES = SRC: 'src_language' DST: 'dst_language' $scope.src_languages = [] $scope.dst_languages = [] $scope.src_languages_groups = [] $scope.dst_languages_groups = [] # DEV $window.ctrls ||= {} $window.ctrls.languages = $scope # Load data from server dataService.get_languages().then (responce) -> $scope.data = responce?.data return unless $scope.data?.languages?.length > 0 $scope.src_languages = $scope.magicSort($scope.filterAvailableSrcLanguages()) $scope.src_languages_groups = utils.inGroups($scope.src_languages, 4) $scope.data.state = $scope.STATES.SRC $scope.ready = true ngProgress.complete() $scope.filterAvailableSrcLanguages = ()-> $scope.data.languages.filter (lang)-> $scope.data.rates.languages.filter( (pair)-> pair.language_from_id == lang.id ).length > 0 $scope.filterAvailableDstLanguages = (from_lang)-> $scope.data.languages.filter (to_lang)-> $scope.data.rates.languages.filter( (pair)-> (pair.language_from_id == from_lang.id) && (pair.language_to_id == to_lang.id) ).length > 0 $scope.in_state = (state)-> if angular.isArray(state) state.filter( (item)-> $scope.data.state == item ).length > 0 else $scope.data.state == state $scope.resetConfirm = (force_reset = false)-> if force_reset $scope.customer.src_language = undefined else $scope.data.state = $scope.STATES.DST $scope.isPairAvailable = -> $scope.customer.src_language? && $scope.customer.dst_languages?.length > 0 $scope.removeSrcLanguage = (lang) -> if $scope.customer.dst_languages?.length > 0 $scope.data.state = $scope.STATES.RESET else $scope.resetConfirm(true) $scope.removeDstLanguage = (lang)-> if lang?.id? $scope.customer.dst_languages = $scope.customer.dst_languages.filter (item) -> # item.selected = false if item.id == lang.id item.id != lang.id $scope.magicSort = (languages)-> top = [] common = [] for lang in languages if lang['top5'] top.push(lang) else common.push(lang) top = top.sort (a, b)-> if a.sort_order < b.sort_order then -1 else if a.sort_order > b.sort_order then 1 else 0 common = common.concat(top.splice(5)) common = common.sort (a, b)-> if a.title < b.title then -1 else if a.title > b.title then 1 else 0 top[top.length-1]['last_of_group'] = true for i in [0...(common.length - 1)] common[i]['last_of_group'] = common[i+1].title[0] != common[i].title[0] top.concat(common) $scope.splitByAlphabet = (languages)-> return [] unless languages?.length > 0 ans = [] _curr = [] _letter = undefined languages = $scope.magicSort(languages) for lang in languages if _letter == lang.title[0] _curr.push(lang) else ans.push(_curr) _letter = lang.title[0] _curr = [lang] _ans $scope.$watch 'customer.src_language', (lang, old_lang) -> if lang?.id? if !old_lang?.id? || old_lang.id != lang.id $scope.dst_languages = $scope.magicSort($scope.filterAvailableDstLanguages($scope.customer.src_language)) $scope.dst_languages_groups = utils.inGroups($scope.dst_languages, 4) $scope.data.state = $scope.STATES.DST $scope.customer.dst_languages = [] else $scope.data.state = $scope.STATES.SRC $scope.orderParams = () -> if $scope.customer? params = {} params['from'] = $scope.customer.src_language.id if $scope.customer.src_language?.id? if $scope.customer.dst_languages?.length > 0 params['to'] = $scope.customer.dst_languages.map((lang)-> lang.id ).join(',') _params = $.param(params) "?" + _params if _params?.length > 0 return $scope ])