index.html 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
<html>
<head>
  <title></title>
  <script type="text/javascript" src="lib/md5/md5.js"></script>
  <script type="text/javascript" src="jio.js"></script>
  <script type="text/javascript" src="lib/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="lib/jsSha1/sha1.js"></script>
  <script type="text/javascript" src="src/jio.storage/s3storage.js"></script>
</head>
<body>

  <div class="put form ajaxWITH">
    <h2>JIO-S3 PUT form</h2>
    <form id="s3AJAXUP" action="https://s3.amazonaws.com/jiobucket/" method="post" enctype="multipart/form-data">
      <div class="text_holder">
        <p>Give a name to your attachment :</p>
        <input type="text">
        <p>Below text will be uploaded as an attachment to my_2nd_document :</p>
        <textarea name="text_area">Contenu de l'attachement</textarea>
      </div>
      <div class="submit">Put the attachment</div>
    </form>
  </div>

  <div class="ALLDOCS">
    <h2>All docs</h2>
    <div class="button">
      Get all the docs
    </div>
    <div class="content"></div>
  </div>

<style type="text/css" media="screen">
  .button {cursor: pointer;margin:4px;}
  .text-holder {width:100%;clear:both;float:left;width:140px;}
  .text-holder > input {float:left}
  .text-holder > textarea {clear:both;float:left;margin:10px;font-size:10px;}
  div.submit, .ALLDOCS .button {background:silver;border-radius: 3px;color:black;width:150px;height:auto;box-shadow:0px 0px 1px 1px black;cursor:pointer;margin:4px;padding:2px;}
</style>


<script>

var alldocsDATA = '';

$(document).ready(function(){

  //mise en cache des éléments DOM
  var alldocsAREA = $('.ALLDOCS');
  var alldocsButton = alldocsAREA.find('.button');
  var alldocsContent = alldocsAREA.find('.content');
  var metaAREA = $('.META');
  var metaContent = metaAREA.find('.content');
  var outputAREA = $('.OUTPUT');
  var outputContent = outputAREA.find('.content');

  //mise en cache des éléments DOM
  var jioform = $('#s3AJAXUP');
  var submitButton = $('div.submit');
  var textTitle = jioform.find('input[type="text"]').val();
  var textContent  = jioform.find('textarea').val();

  //Initiation JIO
  var jio_instance = jIO.newJio({
    "type":"s3",
    "AWSIdentifier":"AKIAJLNYGVLTV66RHPEQ",
    "password":"/YHoa5r2X6EUHfvP31jdYx6t75h81pAjIZ4Mt94y",
    "server":"jiobucket",
    "url":"https://jiobucket.s3.amazonaws.com"
  });

  //jio_instance.get({"_id": "my_1st_document", "title": "myDoc1"}, function(err, response){
    //console.log("GET response :"+ response);
  //});
//
 //jio_instance.getAttachment({"_id": "my_1st_document","_attachment": "1er_attachment"}, function(err, response){
    //console.log("GET Attachment response :"+ response);
  //});
//
  //jio_instance.put({"_id": "my_6th_document", "title": "myDoc6"}, function(err, response){
    //console.log('PUT response : '+JSON.stringify(response));
  //});
//

//
  jio_instance.post({"_id": 'documentONE', "title": "doc1"}, function(err, response){
    console.log('POST response :'+ JSON.stringify(response));
    //removeWrapper();
  });

    jio_instance.putAttachment({
        "_id": "documentONE",
        "_attachment": '1st_Attachment_manual',
        "_data": "Ceci est le contenu de l'attachment",
        "_mimetype": "text/plain"
      }, function(err, response){
    console.log("PUT Attachment response :"+ JSON.stringify(response));
    //removeAttachmentWrapper();
  });



//
  //function removeWrapper(){
    //jio_instance.remove({"_id":"my_5th_document","title":"myDoc5"}, function(err,response){
      //console.log('REMOVE response : '+ response);
    //});
  //};
//
  //function removeAttachmentWrapper(){
        //jio_instance.removeAttachment({
        //"_id": "my_1st_document",
        //"_attachment": '99rd_Attachment_manual'
      //}, function(err,response){
        //console.log(response);
    //});
  //};


    //jio_instance.remove({"_id":"documentONE","title":"doc1"}, function(err,response){
      //console.log('REMOVE response : '+ response);
    //});

  //Sera utile plus tard
  jioform.on('change',function(){
    textTitle = jioform.find('input[type="text"]').val();
    textContent  = jioform.find('textarea').val();
  });

  //Ajout de l'attachment
  submitButton.on('click',function(){
    //console.log("putAttachment");
    if (textTitle == ''){
      alert("Veuillez donner un titre pour l'attachment.");
    }
    else {
      jio_instance.putAttachment({
        "_id": "documentONE",
        "_attachment": textTitle,
        "_data": textContent,
        "_mimetype": "text/plain"
      }, function (err, response) {
        console.log('Put Attachment response :'+JSON.stringify(response));
      });
    }
  });

  alldocsButton.on('click',function(){
    //console.log("alldocs");
    jio_instance.allDocs({"include_docs": true},function (err, response){

      console.log(JSON.stringify(response));
      console.log(response);

      var doc_keys = $(response.rows);

      //console.log(doc_keys.length);

      doc_keys.each(function(index){
              var that = $(this);
              var filename = that[0]["id"];
              //console.log(filename);

              var DOCAttachment = that[0]["doc"]["_attachments"];

              //console.log(DOCAttachment);

              if (DOCAttachment == undefined){
                //n'a pas d'attachment
                alldocsContent.append('<a alt="click to remove" title="click to remove" href="#" rel="'+filename+'">'+filename+'</a><br>');

              }
              else {
                for ( property in that[0]["doc"]) {
                  //console.log( property ); // Outputs: foo, fiz or fiz, foo
                }

                var names = '';
                names += '<a alt="click to remove" title="click to remove" href="#" rel="'+filename+'">'+filename+'</a><br>';
                for(var key in DOCAttachment) {
                  if(DOCAttachment.hasOwnProperty(key)) {
                    
                    //names += '<a alt="click to remove" title="click to remove" type="attachment" href="#" rel="'+filename+'/'+key+'">'+filename+'/'+key+'</a><br>';
                  }
                }
               // console.log(names.join(', ')); //a, b
                //console.log(names);
                alldocsContent.append(names);
                //console.log(DOCAttachment);
              }

              //alldocsContent.append('<a alt="click to remove" title="click to remove" href="#" rel="'+filename+'">'+filename+'</a><br>');

      });
      alldocsContent.html(JSON.stringify(response));
      //addRemove();
    });
  });


  function addRemove(){
    var linksToRemove = alldocsContent.find('a');
    //console.log(linksToRemove);
    linksToRemove.on('click',function(){
      var that = $(this);
      console.log(that.attr('type'));
      if (that.attr('type') !== 'attachment'){
        var docID = that.text();
        jio_instance.remove({"_id": docID},function(err, response){
          console.log(err);
          console.log(response);
        });
      }
      else {
        var docID = that.text();
        var splitDoc = docID.split('/');
        jio_instance.removeAttachment({"_id": splitDoc[0], "_attachment": splitDoc[1]},function(err, response){
          console.log(err);
          console.log(response);
        });
      }
    })
  };






  //function addRemove(){
    //var linksToRemove = alldocsContent.find('a');
    ////console.log(linksToRemove);
    //linksToRemove.on('click',function(){
      //var that = $(this);
      //console.log(that.attr('type'));
      //if (that.attr('type') == 'attachment'){
        //var docID = that.text();
        //jio_instance.remove({"_id": docID},function(){});
      //}
      //else {
        //var docID = that.text();
        //jio_instance.removeAttachment({"_id": "my_document", "_attachment": "its_attachment"},function(){});
      //}
//
    //})
  //};



  /*fin document ready*/
});







</script>
</body>
</html>