Rule.vue 6.79 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
        <div class="row mb-3">
36 37 38 39 40 41 42 43
          <label for="comment" class="mb-2"><u>Résumé de la modification</u> * </label>
          <textarea id="comment" v-model="comment" rows="3"></textarea>
        </div>
        <div class="row mb-3">
          <div class="col-md-12 pl-0">
            <label for="contributor_email" class="pb-2"><u>Votre email</u> *</label>
          </div>
          <div class="col-md-12 pl-0">
44 45
            <input id="contributor_email" v-model="contributor_email" type="text" :class="{editErrorClass: badUser}"/><br>
            <span v-if="badUser" class="text-danger font-weight-light">Cet utilisateur n'est pas autorisé à soumettre des modifications</span>
46 47
          </div>
        </div>
48 49 50 51 52 53 54
        <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">
            <input @click="save" type="button" class="btn btn-outline-success pull-right" value="Enregistrer"/>
          </div>
55 56 57
        </div>
      </div>
    </div>
58
  </div>
59

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

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

David Foucher's avatar
David Foucher committed
70
  export default {
71
    name: 'Rule',
72 73 74
    components: {
      TreeItem,
    },
75
    props: ['name', 'data', 'path', 'printRulePath', 'rulePath'],
76 77 78 79 80
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
81
        contributor_email: '',
82
        auth: '',
83 84 85 86
        isEditMode: '',
        modification_list: [],
        badUser: false,
        notModified: false,
87 88 89
      }
    },
    computed: {
90 91 92 93 94 95
      displayedName: function () {
        return this.name.split('.')[0];
      },
      modification_count: function () {
        return this.modification_list.length;
      },
96 97 98 99
      modification_key: function() {
        // TODO récupération de la clé niveau serveur
        return "clé";
      },
100 101 102 103 104 105 106
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
107
    methods: {
108 109 110 111 112 113 114
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
115
        this.auth = "";
116 117 118 119 120
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
        this.ruleData = this.content
        const postData = {
121 122
          author_email:this.contributor_email,
          author_name:this.contributor_email.split("@")[0],
123
          title: this.displayedName,
124 125
          comment: this.comment,
          content: this.content,
126
          filename: 'trefle/config/rules/' + this.path
127 128 129 130 131
        }

        this.$http
          .post('/source/save', postData)
          .then(response => {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            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}
            return this.isEditMode=!this.isEditMode
          }, response => {
              if(response.status == 304){
                this.notModified = true;
                this.badUser = false;
              } 
              else if (response.status == 401) {
                this.badUser = true;
                this.notModified = false;
              }
              return false;
148 149 150
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
151
        var root= new Node(this.name.split('.')[0]);
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
        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;
187
      }
188
    },
David Foucher's avatar
David Foucher committed
189 190
  }
</script>
191
<style scoped>
192 193 194
.editErrorClass {
  color: #dc3545;
  border: 1px solid #dc3545;
195 196
  border-radius:3px;
}
197 198 199
.rule-modification-text {
  font-family: 'Courier New', Courier, monospace;
}
200
</style>