Rule.vue 7.45 KB
Newer Older
1
<template>
David Foucher's avatar
David Foucher committed
2
  <div id="Rule">
3
    <div class="mb-5">
4
      <div class="row mb-3">
5
        <div  class="col-md-6 col-sm-12 col-xs-12">
6
          <h5>
7
            <span  style="vertical-align:-30%" >{{ this.displayedName }}</span>
8
          </h5>
9
          <span v-if="this.modification_count">({{ this.modification_count }} modification<span v-if="this.modification_count > 1">s</span> en cours)</span>
10
        </div>
11
        <div class="col-md-6 col-sm-12 col-xs-12">
12
          <h4 v-if="isEditMode" class="pull-right"><em>Modification de la règle</em></h4>
13
          <input  v-else @click="edit" type="button" class="main-button btn pull-right" value="Soumettre une modification"/>
14
          <!-- TODO: show gitlab link of modification if exists -->
15 16
        </div>
      </div>
17 18 19 20 21
      <div v-show="!isEditMode" class="row">
        <div class="col-md-12">
          <span v-html="printRulePath"></span>
        </div>
      </div>
22 23
    </div>
    <div v-show="!isEditMode">
24 25
      <ul>
        <TreeItem class="item" :item="this.ruleTree" :rootElement="true" :rulePath="this.rulePath"></TreeItem>
26 27 28
      </ul>
    </div>
    <div v-show="isEditMode">
29 30
      <div class="container">
        <div class="row mb-3">
31
          <label for="content"><u>Contenu de la règle</u></label>
32 33
          <textarea id="content" v-model="content" rows="15" class="rule-modification-text" :class="{editErrorClass: notModified}"></textarea>
          <span v-if="notModified" class="text-danger font-weight-light">Aucune modification n'a été renseignée</span>
34
        </div>
35 36 37 38 39
        <div class="row mb-3">
          <div class="col-md-6 pl-0">
            <input @click="closeEdit" type="button" class="btn btn-outline-danger pull-left" value="Annuler"/>
          </div>
          <div class="col-md-6 pr-0">
40
            <button v-b-modal.mail-modal class="btn btn-outline-success pull-right">Enregistrer</button>
41
          </div>
42
        </div>
43 44 45 46 47 48 49 50 51 52 53 54

        <b-modal id="mail-modal" title="Soumettre votre modification">
          <label for="comment" class="mb-2"><u>Résumé de la modification</u> * </label>
          <textarea id="comment" v-model="comment" rows="3"></textarea>
          <label for="contributor_email" class="pb-2"><u>Votre email</u> *</label><br>
          <input id="contributor_email" v-model="contributor_email" type="text" :class="{editErrorClass: badUser}" class="form-control" style="border: 1px solid #bfbfbf; border-radius: 2px;"/><br>
          <span v-if="badUser" class="text-danger font-weight-light">Cet utilisateur n'est pas autorisé à soumettre des modifications</span>
          <template v-slot:modal-footer>       
            <input @click="save" type="button" class="btn btn-outline-success pull-right" value="Enregistrer"/>
          </template>
        </b-modal>

55 56
      </div>
    </div>
57
  </div>
58

59 60
</template>
<script>
61 62 63 64 65 66 67 68
  import TreeItem from './TreeItem.vue';

  function Node(name) {
    this.name = name;
    this.parent = null;
    this.children = [];
  }

David Foucher's avatar
David Foucher committed
69
  export default {
70
    name: 'Rule',
71 72 73
    components: {
      TreeItem,
    },
74
    props: ['name', 'data', 'path', 'printRulePath', 'rulePath'],
75 76 77 78 79
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
80
        contributor_email: '',
81
        auth: '',
82 83 84 85
        isEditMode: '',
        modification_list: [],
        badUser: false,
        notModified: false,
86 87
      }
    },
88
    beforeMount: function() {
89 90
      this.loadInProgressModification();
    },
91
    computed: {
92 93 94 95
      displayedName: function () {
        return this.name.split('.')[0];
      },
      modification_count: function () {
96
        return Object.keys(this.modification_list).length;
97
      },
98 99 100 101 102 103 104
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
105
    methods: {
106 107
      loadInProgressModification: function () {
        this.$http
108
          .get('/source/modified?branch='+encodeURIComponent(this.displayedName))
109
          .then(response => {
110 111 112 113 114
            this.modification_list = response.body;
            return true;
          }, response => {
            if(response.status == 500) this.modification_list = {};
            return false;
115 116
          })
      },
117 118 119 120 121 122 123
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
124
        this.auth = "";
125 126 127 128 129
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
        this.ruleData = this.content
        const postData = {
130 131
          author_email:this.contributor_email,
          author_name:this.contributor_email.split("@")[0],
132
          title: this.displayedName,
133 134
          comment: this.comment,
          content: this.content,
135
          filename: 'trefle/config/rules/' + this.path
136 137 138 139 140
        }

        this.$http
          .post('/source/save', postData)
          .then(response => {
141 142 143 144 145
            var commit = {}
            commit.url = 'https://beta.pole-emploi.fr/open-source/trefle/commit/' + response.id
            commit.title = response.title
            this.$parent.rules[this.name]['data'] = this.data
            this.$parent.rules[this.name]['gitlab'] = {'commit': commit}
146 147
            this.$bvModal.hide("mail-modal");
            return this.isEditMode=!this.isEditMode            
148 149 150 151
          }, response => {
              if(response.status == 304){
                this.notModified = true;
                this.badUser = false;
152
                this.$bvModal.hide("mail-modal");
153 154 155 156 157 158
              } 
              else if (response.status == 401) {
                this.badUser = true;
                this.notModified = false;
              }
              return false;
159 160 161
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
162
        var root= new Node(this.name.split('.')[0]);
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
        var currentIndent=-1;
        var currentNode=root;
        // we scan the rules line by line
        for (var i=0;i<lines.length;i++) {
          var line=lines[i];
          if (line.match(/^\s*$/)) continue; // Empty line, skip
          var indent=line.search(/\S|$/); // number of indenting spaces
          var newNode= new Node(line.trim());
          if (indent>currentIndent) { // New child
            // Set the new node parent
            newNode.parent=currentNode;
            // attach the new node to its parent
            currentNode.children.push(newNode);
            // Set the new current node
            currentNode=newNode;
            currentIndent=indent;
          } else if (indent<currentIndent){
            // Move up in the tree
            var level=currentIndent-indent;
            for (var j=0;j<level/4;j++) {
              // up one level
              currentNode=currentNode.parent;
              currentIndent=currentIndent-4;
            }
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode); // Add a sibbling
            currentNode=newNode;
          } else {
            // Add as sibbling
            newNode.parent=currentNode.parent;
            currentNode.parent.children.push(newNode);
            currentNode=newNode;
          }
        }
        return root;
198
      }
199
    },
David Foucher's avatar
David Foucher committed
200 201
  }
</script>
202
<style scoped>
203 204 205
.editErrorClass {
  color: #dc3545;
  border: 1px solid #dc3545;
206 207
  border-radius:3px;
}
208 209 210
.rule-modification-text {
  font-family: 'Courier New', Courier, monospace;
}
211 212 213 214 215

textarea {
  border: 1px solid #bfbfbf;
  border-radius: 2px;
}
216
</style>