project_property_decimal_view.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Created by Zhong on 2017/11/13.
  3. */
  4. //default setting
  5. let defaultDecimal = {
  6. min: 0,
  7. max: 6,
  8. _def: {//editable: 开放给用户编辑的
  9. bills: {editable: true, data: {unitPrice: 2, totalPrice: 2}},
  10. ration: {editable: true, data: {quantity: 3, unitPrice: 2, totalPrice: 2}},
  11. glj: {editable: true, data: {quantity: 3, unitPrice: 2}},
  12. feeRate: {editable: true, data: 2},
  13. quantity_detail: {editable: false, data: 4},
  14. process: {editable: false, data: 6}
  15. }
  16. };
  17. let decimalObj = Object.create(null);
  18. decimalObj.decimal = function (field, node) {
  19. if(isDef(field)){
  20. if(field === 'feeRate'){
  21. return this[field];
  22. }
  23. if(isDef(node)){
  24. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  25. if(field === 'quantity'){
  26. return billsQuanDecimal.decimal(node.data.unit);
  27. }
  28. else {
  29. returnV(this['bills'][field], this.process);
  30. }
  31. }
  32. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  33. returnV(this['ration'][field], this.process);
  34. }
  35. else if(node.sourceType === projectObj.project.GLJ.getSourceType()){
  36. returnV(this['glj'][field], this.process);
  37. }
  38. }
  39. }
  40. return this.process;
  41. };
  42. function returnV(v, r){
  43. if(isDef(v)){
  44. return v;
  45. }
  46. return r;
  47. }
  48. function isUndef(v) {
  49. return v === undefined || v === null;
  50. }
  51. function isDef(v){
  52. return v !== undefined && v !== null;
  53. }
  54. function isObj(v){
  55. return isDef(v) && typeof v === 'object';
  56. }
  57. function isNum(v){
  58. return isDef(v) && !isNaN(v);
  59. }
  60. function isInt(v){
  61. return isNum(v) && v % 1 === 0;
  62. }
  63. function isValidDigit(v){
  64. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  65. }
  66. //newV用户可编辑数据
  67. function toUpdateDecimal(orgV, newV){
  68. let rst = false;
  69. let len = Object.keys(newV).length;
  70. reCompare(orgV, newV);
  71. function reCompare(orgV, newV){
  72. for(let attr in newV){
  73. if(Object.keys(newV[attr]).length > 0){
  74. if(isUndef(orgV[attr])){
  75. rst = true;
  76. return;
  77. }
  78. else{
  79. reCompare(orgV[attr], newV[attr]);
  80. }
  81. }
  82. else {
  83. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  84. rst = true;
  85. return;
  86. }
  87. }
  88. }
  89. }
  90. return rst;
  91. }
  92. function setDecimal(_digits, data){
  93. if(isDef(data)){
  94. for(let attr in data){
  95. _digits[attr] = data[attr] || defaultDecimal['_def'][attr]['data'];
  96. }
  97. }
  98. else {
  99. for(let attr in defaultDecimal['_def']){
  100. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  101. }
  102. }
  103. }
  104. //获取decimalPanel中要展示的数据
  105. function m_getInitData(data){
  106. let rst = Object.create(null);
  107. for(let attr in defaultDecimal['_def']){
  108. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  109. rst[attr] = data[attr];
  110. }
  111. }
  112. return rst;
  113. }
  114. //获取小数位数panel里的数据
  115. function m_getDecimalData(inputs){
  116. let rst = Object.create(null);
  117. for(let i = 0, len = inputs.length ; i < len; i++){
  118. let name = $(inputs[i]).attr('name');
  119. let attrs = name.split('-');
  120. if(attrs.length > 1){
  121. if(isUndef(rst[attrs[0]])){
  122. rst[attrs[0]] = Object.create(null);
  123. }
  124. if(isDef(rst[attrs[0]])) {
  125. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  126. }
  127. }
  128. else {
  129. rst[attrs[0]] = parseInt($(inputs[i]).val());
  130. }
  131. }
  132. for(let attr in defaultDecimal['_def']){
  133. if(!defaultDecimal['_def'][attr]['editable']){
  134. rst[attr] = defaultDecimal['_def'][attr]['data'];
  135. }
  136. }
  137. return rst;
  138. }
  139. function v_initPanel(data){
  140. if(this.isDef(data)){
  141. for(let attr in data){
  142. if(this.isObj(data[attr])){
  143. for(let subAttr in data[attr]){
  144. let str = attr + '-' + subAttr;
  145. let jqs = 'input[name="' + str + '"]';
  146. $(jqs).val(data[attr][subAttr]);
  147. $(jqs).attr('min', defaultDecimal.min);
  148. $(jqs).attr('max', defaultDecimal.max);
  149. }
  150. }
  151. else {
  152. let str = attr + '';
  153. let jqs = 'input[name="' + str + '"]';
  154. $(jqs).val(data[attr]);
  155. $(jqs).attr('min', defaultDecimal.min);
  156. $(jqs).attr('max', defaultDecimal.max);
  157. }
  158. }
  159. }
  160. }
  161. function e_validIn(inputs){
  162. for(let i = 0, len = inputs.length; i < len; i++){
  163. let orgV = $(inputs[i]).val();
  164. $(inputs[i]).keydown(function () {
  165. let v = $(this).val();
  166. if(v.trim().length > 0 && isValidDigit(v)){
  167. orgV = v;
  168. }
  169. });
  170. $(inputs[i]).keyup(function () {
  171. let v = $(this).val();
  172. if(v.trim().length === 0 || !isValidDigit(v)){
  173. alert('小数位数范围在0-6!');
  174. $(this).val(orgV);
  175. }
  176. else{
  177. //let newV = parseInt(v);
  178. }
  179. });
  180. }
  181. }
  182. function e_bindCof(btn){
  183. btn.bind('click', function () {
  184. //获取更新的数据
  185. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  186. if(toUpdateDecimal(decimalObj, updateDecimal)){
  187. a_updateDigits(updateDecimal);
  188. }
  189. });
  190. }
  191. function e_unbindCof(btn){
  192. btn.unbind();
  193. }
  194. function a_updateDigits(updateDecimal){
  195. let url = '/pm/api/updateProjects';
  196. let data = {
  197. updateType: 'update',
  198. updateData: {
  199. ID: parseInt(scUrlUtil.GetQueryString('project')),
  200. 'property.decimal': updateDecimal
  201. }
  202. };
  203. let postData = {
  204. user_id: userID,
  205. updateData: [data]
  206. };
  207. let scCaller = function (resultData) {
  208. //update data
  209. setDecimal(decimalObj, updateDecimal);
  210. let v_data = m_getInitData(decimalObj);
  211. v_initPanel(v_data);
  212. };
  213. let errCaller = function () {
  214. alert('更新小数位数失败!');
  215. let v_data = m_getInitData(decimalObj);
  216. v_initPanel(v_data);
  217. };
  218. CommonAjax.post(url, postData, scCaller, errCaller);
  219. }
  220. $(document).ready(function () {
  221. $('#poj-set').on('shown.bs.modal', function (e) {
  222. let v_data = m_getInitData(decimalObj);
  223. v_initPanel(v_data);
  224. });
  225. //绑定确定按钮
  226. e_bindCof($('#property_ok'));
  227. //绑定小数位数输入控制
  228. e_validIn($('input', '#poj-settings-decimal'));
  229. });