client/app/slideshow/slideshow_controller.js
author rougeronj
Tue, 17 Mar 2015 16:34:23 +0100
changeset 14 4d27fbc3f9df
parent 9 962604899225
child 39 b714bcbe915c
permissions -rw-r--r--
Succed to get the books from our api server and print them dynamically
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('slideshowModel', function(searchApi) {
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    13
      this.slideshow = searchApi.searchResource({q:'stop_email=toto@gmail.com', of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}).getJsonp();
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    })
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    15
    .controller('slideshowCtrl', function($scope, $filter, $location, $routeParams, $timeout, searchApi, slideshowModel){
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
      $scope.slideshow = slideshowModel.slideshow;
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    17
      
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    18
      $scope.iSlide = parseInt($routeParams.iSlide) || 0;
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    19
      if(typeof $routeParams.searched !== 'undefined'){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    20
          var a = $routeParams.searched.split(',');
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    21
          var q = a[0], i = parseInt(a[1]);
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    22
          if(a.length===2 && !isNaN(i)){
9
962604899225 search add works again
cavaliet
parents: 8
diff changeset
    23
              searchApi.searchResource({q:q, of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}).getJsonp().$promise.then(
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    24
                  //success
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    25
                  function( data ){
9
962604899225 search add works again
cavaliet
parents: 8
diff changeset
    26
                      $scope.slideshow.hits.push(data.hits[i]);
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    27
                      // timeout because
9
962604899225 search add works again
cavaliet
parents: 8
diff changeset
    28
                      $timeout(function(){$scope.slideshow.hits[$scope.slideshow.hits.length-1].active = true;}, 500);
2
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    29
                  },
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    30
                  //error
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    31
                  function( error ){
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    32
                      alert('Erreur avec la requĂȘte (dans slideshow)', error);
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    33
                  }
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    34
              );
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    35
          }
36ccc573af9a clean search resource
cavaliet
parents: 1
diff changeset
    36
      }
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    37
      $timeout(function(){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    38
          if($scope.iSlide>=0 && $scope.iSlide<$scope.slideshow.hits.length){
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    39
              $scope.slideshow.hits[$scope.iSlide].active = true;
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    40
              var l = $location.path();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    41
              if(l.substr(0,16)==='/slideshow/edit/'){
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    42
                  $scope.slideshow.hits[$scope.iSlide].editMode = true;
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    43
              }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    44
          }
8
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    45
      }, 500);
824c87a9084c (really) better management of slideshowModel with new api.
cavaliet
parents: 4
diff changeset
    46
      
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
      $scope.save = function(){
1
74bbdd739878 views, path, search...
cavaliet
parents: 0
diff changeset
    48
          $scope.slideshow
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
          .$save()
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
          .then(
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
              function(response) {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
                  console.log('NICE SAVING!', response);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
              },
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
              function(reason){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
                  alert('An error occured while saving : ' + reason);
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
              }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
          );
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    })
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    .directive('slideEditor', function() {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
      return {
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
          restrict: 'AE',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
          replace: true,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
          scope: false,
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
          templateUrl: 'slideshow/dataEditor.html',
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
          //controller: function($scope, $element, $attrs){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
          controller: function($scope){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
              $scope.slidesave = function(){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                  if(typeof $scope.slide.tags === 'string'){
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
                      $scope.slide.tags = $scope.slide.tags.split(',');
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
                  }
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                  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
    73
                      $scope.slide.tags[i] = $scope.slide.tags[i].trim();
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                  }
4
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    75
                  $scope.slide.editMode = false;
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    76
                  $scope.save();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    77
              };
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    78
              $scope.editSlidesave = function(){
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    79
                  if(typeof $scope.slide.tags === 'string'){
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    80
                      $scope.slide.tags = $scope.slide.tags.split(',');
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    81
                  }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    82
                  for (var i = $scope.slide.tags.length - 1; i >= 0; i--) {
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    83
                      $scope.slide.tags[i] = $scope.slide.tags[i].trim();
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    84
                  }
28208a0ad8b9 edit slide by url
cavaliet
parents: 2
diff changeset
    85
                  $scope.slide.editMode = false;
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                  $scope.save();
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
      };
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
  });
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
})();