client/app/slideshow/slideshow_controller.js
author cavaliet
Fri, 03 Oct 2014 10:38:50 +0200
changeset 2 36ccc573af9a
parent 1 74bbdd739878
child 4 28208a0ad8b9
permissions -rw-r--r--
clean search resource
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
(function(){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
  'use strict';
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
  angular.module('ammicoSlideshow',['ngResource', 'ngRoute', 'ui.bootstrap'])
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
    .config(function ($routeProvider) {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
      $routeProvider
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
        .when('/', {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
          templateUrl: 'slideshow/slideshow.html',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
          controller: 'slideshowCtrl'
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
        });
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    })
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    .service('slideshowApi', function($resource, context) {
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    13
      console.log('slideshow 5',this);
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    14
      this.slideshow = $resource(context.urls.slideshowUrl);
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    })
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    .service('slideshowModel', function(slideshowApi, context) {
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    17
      console.log('slideshow 6',this);
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
      if(typeof context.slideshow === 'undefined') {
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    19
          console.log('slideshow 6-1');
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
          this.slideshow = slideshowApi.slideshow.get();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
      }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
      else {
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    23
          console.log('slideshow 6-2');
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
          this.slideshow = new slideshowApi.slideshow(context.slideshow);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
      }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    })
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    27
    .controller('slideshowCtrl', function($scope, $filter, $location, $routeParams, $timeout, searchApi, slideshowModel){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    28
      console.log('slideshow 7', $filter, $routeParams, searchApi, slideshowModel);
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
      $scope.slideshow = slideshowModel.slideshow;
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    30
      $scope.iSlide = parseInt($routeParams.iSlide) || 0;
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    31
      if(typeof $routeParams.searched !== 'undefined'){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    32
          var a = $routeParams.searched.split(',');
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    33
          var q = a[0], i = parseInt(a[1]);
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    34
          if(a.length===2 && !isNaN(i)){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    35
              searchApi.searchResource({q:q, of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}).$promise.then(
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    36
                  //success
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    37
                  function( data ){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    38
                      var hit = data.hits[i];
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    39
                      $scope.slideshow.images.push({url: $filter('meta')(hit.metas, 'url'), 
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    40
                                                    title: $filter('meta')(hit.metas, 'name'), 
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    41
                                                    description: $filter('meta')(hit.metas, 'description'), tags: [], user_title:'', user_description:''});
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    42
                      // timeout because
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    43
                      $timeout(function(){$scope.slideshow.images[$scope.slideshow.images.length-1].active = true;}, 200);
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    44
                  },
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    45
                  //error
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    46
                  function( error ){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    47
                      alert('Erreur avec la requĂȘte (dans slideshow)', error);
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    48
                  }
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    49
              );
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    50
          }
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    51
      }
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
      $scope.save = function(){
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    54
          console.log('8 call save POST',$scope.slideshow);
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    55
          $scope.slideshow
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
          .$save()
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
          .then(
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
              function(response) {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                  console.log('NICE SAVING!', response);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
              },
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
              function(reason){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
                  alert('An error occured while saving : ' + reason);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
              }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
          );
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    })
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    .directive('slideEditor', function() {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
      return {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
          restrict: 'AE',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
          replace: true,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
          scope: false,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
          templateUrl: 'slideshow/dataEditor.html',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
          //controller: function($scope, $element, $attrs){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
          controller: function($scope){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
              $scope.slidesave = function(){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                  if(typeof $scope.slide.tags === 'string'){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                      $scope.slide.tags = $scope.slide.tags.split(',');
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
                  }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                  for (var i = $scope.slide.tags.length - 1; i >= 0; i--) {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                      $scope.slide.tags[i] = $scope.slide.tags[i].trim();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                  }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                  $scope.editMode = false;
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                  $scope.save();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
              };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
          }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
  });
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
})();