api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client_vue_auth.html 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8">
  4. <meta http-equiv="x-ua-compatible" content="ie=edge">
  5. <title>Vue.js CRUD application</title>
  6. <meta name="description" content="">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.2.1/vue-router.js"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.3/axios.js"></script>
  11. <script src="../lib/php_crud_api_transform.js"></script>
  12. <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  13. <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap-theme.min.css">
  14. <style>
  15. .logo {
  16. width: 50px;
  17. float: left;
  18. margin-right: 15px;
  19. }
  20. .form-group {
  21. max-width: 500px;
  22. }
  23. .actions {
  24. padding: 10px 0;
  25. }
  26. .glyphicon-euro {
  27. font-size: 12px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="container">
  33. <header class="page-header">
  34. <div class="branding">
  35. <img src="https://vuejs.org/images/logo.png" alt="Logo" title="Home page" class="logo"/>
  36. <h1>Vue.js CRUD application</h1>
  37. </div>
  38. </header>
  39. <main id="app">
  40. <router-view></router-view>
  41. </main>
  42. </div>
  43. <template id="post-list">
  44. <div>
  45. <div class="actions">
  46. <router-link class="btn btn-default" v-bind:to="{path: '/add-post'}">
  47. <span class="glyphicon glyphicon-plus"></span>
  48. Add post
  49. </router-link>
  50. </div>
  51. <div class="filters row">
  52. <div class="form-group col-sm-3">
  53. <label for="search-element">Filter</label>
  54. <input v-model="searchKey" class="form-control" id="search-element" requred/>
  55. </div>
  56. </div>
  57. <table class="table">
  58. <thead>
  59. <tr>
  60. <th>Content</th>
  61. <th class="col-sm-2">Actions</th>
  62. </tr>
  63. </thead>
  64. <tbody>
  65. <tr v-if="posts===null">
  66. <td colspan="4">Loading...</td>
  67. </tr>
  68. <tr v-else v-for="post in filteredposts">
  69. <td>
  70. <router-link v-bind:to="{name: 'post', params: {post_id: post.id}}">{{ post.content }}</router-link>
  71. </td>
  72. <td>
  73. <router-link class="btn btn-warning btn-xs" v-bind:to="{name: 'post-edit', params: {post_id: post.id}}">Edit</router-link>
  74. <router-link class="btn btn-danger btn-xs" v-bind:to="{name: 'post-delete', params: {post_id: post.id}}">Delete</router-link>
  75. </td>
  76. </tr>
  77. </tbody>
  78. </table>
  79. </div>
  80. </template>
  81. <template id="add-post">
  82. <div>
  83. <h2>Add new post</h2>
  84. <form v-on:submit="createpost">
  85. <div class="form-group">
  86. <label for="add-content">Content</label>
  87. <textarea class="form-control" id="add-content" rows="10" v-model="post.content"></textarea>
  88. </div>
  89. <button type="submit" class="btn btn-primary">Create</button>
  90. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  91. </form>
  92. </div>
  93. </template>
  94. <template id="post">
  95. <div>
  96. <b>Content: </b>
  97. <div>{{ post.content }}</div>
  98. <br/>
  99. <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
  100. <router-link v-bind:to="'/'">Back to post list</router-link>
  101. </div>
  102. </template>
  103. <template id="post-edit">
  104. <div>
  105. <h2>Edit post</h2>
  106. <form v-on:submit="updatepost">
  107. <div class="form-group">
  108. <label for="edit-content">Content</label>
  109. <textarea class="form-control" id="edit-content" rows="3" v-model="post.content"></textarea>
  110. </div>
  111. <button type="submit" class="btn btn-primary">Save</button>
  112. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  113. </form>
  114. </div>
  115. </template>
  116. <template id="post-delete">
  117. <div>
  118. <h2>Delete post {{ post.id }}</h2>
  119. <form v-on:submit="deletepost">
  120. <p>The action cannot be undone.</p>
  121. <button type="submit" class="btn btn-danger">Delete</button>
  122. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  123. </form>
  124. </div>
  125. </template>
  126. <script>
  127. var posts = null;
  128. var api = axios.create({
  129. baseURL: '../api.php',
  130. withCredentials: true
  131. });
  132. api.interceptors.response.use(function (response) {
  133. if (response.headers['x-xsrf-token']) {
  134. document.cookie = 'XSRF-TOKEN=' + response.headers['x-xsrf-token'] + '; path=/';
  135. }
  136. return response;
  137. });
  138. function findpost (postId) {
  139. return posts[findpostKey(postId)];
  140. };
  141. function findpostKey (postId) {
  142. for (var key = 0; key < posts.length; key++) {
  143. if (posts[key].id == postId) {
  144. return key;
  145. }
  146. }
  147. };
  148. var List = Vue.extend({
  149. template: '#post-list',
  150. data: function () {
  151. return {posts: posts, searchKey: ''};
  152. },
  153. created: function () {
  154. var self = this;
  155. api.post('/',{username:"admin",password:"admin"}).then(function (response) {
  156. api.get('/posts').then(function (response) {
  157. posts = self.posts = php_crud_api_transform(response.data).posts;
  158. }).catch(function (error) {
  159. console.log(error);
  160. });
  161. }).catch(function (error) {
  162. console.log(error);
  163. });
  164. },
  165. computed: {
  166. filteredposts: function () {
  167. return this.posts.filter(function (post) {
  168. return this.searchKey=='' || post.content.indexOf(this.searchKey) !== -1;
  169. },this);
  170. }
  171. }
  172. });
  173. var post = Vue.extend({
  174. template: '#post',
  175. data: function () {
  176. return {post: findpost(this.$route.params.post_id)};
  177. }
  178. });
  179. var postEdit = Vue.extend({
  180. template: '#post-edit',
  181. data: function () {
  182. return {post: findpost(this.$route.params.post_id)};
  183. },
  184. methods: {
  185. updatepost: function () {
  186. var post = this.post;
  187. api.put('/posts/'+post.id,post).then(function (response) {
  188. console.log(response.data);
  189. }).catch(function (error) {
  190. console.log(error);
  191. });
  192. router.push('/');
  193. }
  194. }
  195. });
  196. var postDelete = Vue.extend({
  197. template: '#post-delete',
  198. data: function () {
  199. return {post: findpost(this.$route.params.post_id)};
  200. },
  201. methods: {
  202. deletepost: function () {
  203. var post = this.post;
  204. api.delete('/posts/'+post.id).then(function (response) {
  205. console.log(response.data);
  206. }).catch(function (error) {
  207. console.log(error);
  208. });
  209. router.push('/');
  210. }
  211. }
  212. });
  213. var Addpost = Vue.extend({
  214. template: '#add-post',
  215. data: function () {
  216. return {post: {content: '', user_id: 1, category_id: 1}}
  217. },
  218. methods: {
  219. createpost: function() {
  220. var post = this.post;
  221. api.post('/posts',post).then(function (response) {
  222. post.id = response.data;
  223. }).catch(function (error) {
  224. console.log(error);
  225. });
  226. router.push('/');
  227. }
  228. }
  229. });
  230. var router = new VueRouter({routes:[
  231. { path: '/', component: List},
  232. { path: '/post/:post_id', component: post, name: 'post'},
  233. { path: '/add-post', component: Addpost},
  234. { path: '/post/:post_id/edit', component: postEdit, name: 'post-edit'},
  235. { path: '/post/:post_id/delete', component: postDelete, name: 'post-delete'}
  236. ]});
  237. app = new Vue({
  238. router:router
  239. }).$mount('#app')
  240. </script>
  241. </body>
  242. </html>