'use strict' bootstrapAngular = -> angular.bootstrap(document.body, ['coursesApp']) window.coursesApp = angular .module 'coursesApp', ['ngResource', 'ngSanitize', 'yaru22.angular-timeago', 'ngDialog'], -> console.log 'Angular Course Module' .factory 'CoursesIO', [ '$resource', ($resource) -> args = {} methods = query: method: 'GET', isArray: true update: method: 'PUT' CoursesIO = $resource 'https://enroll.oplerno.com/courses/introduction-to-existentialism.json', args, methods return CoursesIO ] .service 'CoursesModel', [ 'CoursesIO', 'timeAgo', (CoursesIO, timeAgo) -> timeAgo.settings.allowFuture = true CoursesSession = -> this.data = {} this.created = Date.NOW CoursesSession.prototype.fetch = (query) -> self = this CoursesIO.query query, (result) -> console.log "called get" self.data = result console.log result.result new CoursesSession() ] .controller 'CoursesController', ['$scope', 'CoursesIO', 'CoursesModel', '$http', '$compile', ($scope, CoursesIO, CoursesModel, $http, $compile) -> console.log 'Angular Courses' $scope.editShelf = (course) -> course.edit = true $scope.saveShelf = (course, field, $event) -> course.edit = false course[field] = ( if typeof course[field] == 'number' parseInt($event.target.innerText) else $event.target.innerHTML ) $scope.courses_list = [course] console.log 'send trans' angular.element(document.getElementById('angular_courses_list')) console.log CoursesModel $scope.courses_list = CoursesModel.fetch() $scope.courses = -> #$('[data-toggle="tooltip"]').tooltip() $scope.courses_list 0 # DON'T REMOVE ] .controller 'TopBarController', [ '$scope', '$http', '$compile', ($scope, $http, $compile) -> console.log 'Angular TopBar' $scope.scroll = 0 $scope.menu_item = true ] .directive 'backImg', -> console.log 'Directive' return (scope, element, attrs) -> if attrs.backImg.lastIndexOf('/') == attrs.backImg.length-1 attrs.backImg += '../../../../../../../../assets/medium/missing.png' url = 'url(' url += attrs.backImg url += ')' style = 'background-image': url element.css(style) .directive 'scrollPosition', ($window) -> console.log('directive') return { scope: { scroll: '=scrollPosition' }, link: (scope, element, attrs) -> handler = -> scope.scroll = document.body.scrollTop angular.element($window).on('scroll', scope.$apply.bind(scope, handler )) handler() } .controller 'CartFormController', [ '$scope', '$http', 'ngDialog', ($scope, $http, ngDialog) -> $scope.formData = course : '', authenticity_token : '' $scope.clickToOpen = -> request = method : 'POST', url : '/carts/', data : $.param($scope.formData) headers : { 'Content-Type': 'application/x-www-form-urlencoded' } $http(request).success (data) -> $scope.message = '' ngDialog.open({ template: 'putInCartDialog', scope: $scope }) .error -> console.log 'Error' $scope.message = 'NOT' ngDialog.open({ template: 'putInCartDialog', scope: $scope }) ] .factory 'CartIO', [ '$resource', ($resource) -> args = {} methods = query: method: 'GET', isArray: true CartIO = $resource 'https://enroll.oplerno.com/carts/mycart.json', args, methods return CartIO ] .service 'CartModel', [ 'CartIO', 'timeAgo', (CartIO, timeAgo) -> timeAgo.settings.allowFuture = true CartSession = -> this.data = {} this.created = Date.NOW CartSession.prototype.fetch = (query) -> self = this CartIO.query query, (result) -> console.log "called get" self.data = result console.log result.result new CartSession() ] .controller 'CartButtonController', ['$scope', 'CartModel', ($scope, CartModel) -> $scope.cart = CartModel.fetch() $scope.items = $scope.cart.length $scope.price = $scope.cart.sum (item) -> item.price ] String.prototype.trunc = String.prototype.trunc || (n, useWord) -> toLong = this.length>n s_ = if toLong then this.substr(0,n-1) else this s_ = if useWord && toLong then s_.substr(0,s_.lastIndexOf(' ')) else s_ if toLong then s_ + '...' else s_ String.prototype.stripHTML = String.prototype.stripHTML || -> this.replace(/<[^>]+>/gm, '').replace(/&[^;]+;/gm, '') Array::sum = (fn = (x) -> x) -> @reduce ((a, b) -> a + fn b), 0 bootstrapAngular()