client/app/slideshow/slideshow_controller.js
author cavaliet
Mon, 06 Oct 2014 22:52:30 +0200
changeset 8 824c87a9084c
parent 4 28208a0ad8b9
child 9 962604899225
permissions -rw-r--r--
(really) better management of slideshowModel with new api.
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
    })
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
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);
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    15
    })*/
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    16
    .service('slideshowModel', function(searchApi) {
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    17
      console.log('slideshow 6',this);
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    18
      /*this.slideshow = function(myVarForData){
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    19
          searchApi.searchResource({q:'stop_email=toto@gmail.com', of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}, myVarForData);
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    20
      };*/
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    21
      this.slideshow = searchApi.searchResource({q:'stop_email=toto@gmail.com', of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}).getJsonp();
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    22
      console.log('slideshow 6-2',this);
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    })
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    24
    .controller('slideshowCtrl', function($scope, $filter, $location, $routeParams, $timeout, searchApi, slideshowModel){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    25
      //console.log('slideshow 7', $location, $routeParams, searchApi);
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    26
      console.log('slideshow 7');
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
      $scope.slideshow = slideshowModel.slideshow;
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    28
      
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    29
      $scope.iSlide = parseInt($routeParams.iSlide) || 0;
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    30
      //console.log('$routeParams',$routeParams);
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
      }
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    52
      $timeout(function(){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    53
          if($scope.iSlide>=0 && $scope.iSlide<$scope.slideshow.hits.length){
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    54
              $scope.slideshow.hits[$scope.iSlide].active = true;
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    55
              var l = $location.path();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    56
              if(l.substr(0,16)==='/slideshow/edit/'){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    57
                  $scope.slideshow.hits[$scope.iSlide].editMode = true;
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    58
              }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    59
          }
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    60
      }, 500);
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    61
      
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
      $scope.save = function(){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    63
          //console.log('8 call save POST',$scope.slideshow);
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    64
          $scope.slideshow
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
          .$save()
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
          .then(
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
              function(response) {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                  console.log('NICE SAVING!', response);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
              },
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
              function(reason){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
                  alert('An error occured while saving : ' + reason);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
              }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
          );
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    })
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    .directive('slideEditor', function() {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
      return {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
          restrict: 'AE',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
          replace: true,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
          scope: false,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
          templateUrl: 'slideshow/dataEditor.html',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
          //controller: function($scope, $element, $attrs){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
          controller: function($scope){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
              $scope.slidesave = function(){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                  if(typeof $scope.slide.tags === 'string'){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                      $scope.slide.tags = $scope.slide.tags.split(',');
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
                  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
    89
                      $scope.slide.tags[i] = $scope.slide.tags[i].trim();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                  }
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    91
                  $scope.slide.editMode = false;
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    92
                  $scope.save();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    93
              };
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    94
              $scope.editSlidesave = function(){
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    95
                  if(typeof $scope.slide.tags === 'string'){
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    96
                      $scope.slide.tags = $scope.slide.tags.split(',');
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    97
                  }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    98
                  for (var i = $scope.slide.tags.length - 1; i >= 0; i--) {
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    99
                      $scope.slide.tags[i] = $scope.slide.tags[i].trim();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
   100
                  }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
   101
                  $scope.slide.editMode = false;
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
                  $scope.save();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
              };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
          }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
  });
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
})();