project_property_decimal_view.js 6.8 KB

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