Rule.vue 4.88 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 7 8
          <h5>
            <span  style="vertical-align:-30%" >{{ this.name }}</span>
          </h5>
9
        </div>
10
        <div class="col-md-6 col-sm-12 col-xs-12">
11 12
          <h4 v-if="isEditMode" class="pull-right"><em>Modification de la règle</em></h4>
          <input  v-else @click="edit" type="button" class="btn btn-outline-success pull-right" value="Soumettre une modification"/>
13
          <!-- TODO: show gitlab link of modification if exists -->
14 15
        </div>
      </div>
16 17 18 19 20
      <div v-show="!isEditMode" class="row">
        <div class="col-md-12">
          <span v-html="printRulePath"></span>
        </div>
      </div>
21 22
    </div>
    <div v-show="!isEditMode">
23 24
      <ul>
        <TreeItem class="item" :item="this.ruleTree" :rootElement="true" :rulePath="this.rulePath"></TreeItem>
25 26 27
      </ul>
    </div>
    <div v-show="isEditMode">
28 29 30 31
      <div class="container">
        <div class="row mb-3">
          <label for="comment" class="mb-2"><u>Résumé de la modification</u></label>
          <textarea v-model="comment"></textarea>
32
        </div>
33 34 35 36 37 38 39 40 41 42 43
        <div class="row mb-3">
          <label for="content"><u>Contenu de la règle</u></label>
          <textarea v-model="content" class="mb-3"></textarea>
        </div>
        <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>
44 45 46
        </div>
      </div>
    </div>
47
  </div>
48

49 50
</template>
<script>
51 52 53 54 55 56 57 58
  import TreeItem from './TreeItem.vue';

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

David Foucher's avatar
David Foucher committed
59
  export default {
60
    name: 'Rule',
61 62 63
    components: {
      TreeItem,
    },
64
    props: ['name', 'data', 'printRulePath', 'rulePath'],
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    data: function(){
      return {
        ruleData: this.data,
        content: '',
        comment: '',
        isEditMode: false,
      }
    },
    computed: {
      ruleTree: function() {
        return this.toTree(this.ruleData.split('\n'));
      },
      ruleToEdit: function() {
        return this.ruleData;
      },
    },
81
    methods: {
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
      edit: function () {
        this.content = this.ruleToEdit;
        this.isEditMode=!this.isEditMode;
      },
      closeEdit: function () {
        this.content = this.ruleToEdit;
        this.comment = "";
        this.isEditMode=!this.isEditMode;
      },
      save: function() {
        this.ruleData = this.content
        const postData = {
          title: this.name,
          comment: this.comment,
          content: this.content,
          filename: 'trefle/config/rules/' + this.data.path
        }

        this.$http
          .post('/source/save', postData)
          .then(response => {
              var commit = {}
              commit.url = 'https://beta.pole-emploi.fr/open-source/trefle/commit/' + response.id
              commit.title = response.title
              this.rules[this.activeRuleName]['data'] = this.data
              this.rules[this.activeRuleName]['gitlab'] = {'commit': commit}
              return this.isEditMode=!this.isEditMode
          });
      },
      toTree: function (lines) { // eslint-disable-line no-unused-vars
112
        var root= new Node(this.name.split('.')[0]);
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
        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;
148
      }
149
    },
David Foucher's avatar
David Foucher committed
150 151
  }
</script>
152 153
<style scoped>
</style>