Guest User

Untitled

a guest
Nov 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.78 KB | None | 0 0
  1. // Provide HUD functionality for cloning a group of fields
  2. Craft.FieldManager.CloneGroup = Garnish.Base.extend({
  3. $element: null,
  4. groupId: null,
  5.  
  6. $form: null,
  7. $spinner: null,
  8.  
  9. hud: null,
  10.  
  11. init: function($element, $data) {
  12. this.$element = $element;
  13. this.groupId = $data.data('groupid');
  14.  
  15. this.$element.addClass('loading');
  16.  
  17. var data = {
  18. groupId: this.groupId,
  19. clone: true,
  20. };
  21.  
  22. Craft.postActionRequest('field-manager/base/get-group-modal-body', data, $.proxy(this, 'showHud'));
  23. },
  24.  
  25. showHud: function(response, textStatus) {
  26. this.$element.removeClass('loading');
  27.  
  28. if (response.success) {
  29. var $hudContents = $();
  30.  
  31. this.$form = $('<form/>');
  32. $('<input type="hidden" name="groupId" value="' + this.groupId + '">').appendTo(this.$form);
  33. $fieldsContainer = $('<div class="fields"/>').appendTo(this.$form);
  34.  
  35. $fieldsContainer.html(response.modalHtml);
  36. Craft.initUiElements($fieldsContainer);
  37.  
  38. var $buttonsOuterContainer = $('<div class="footer"/>').appendTo(this.$form);
  39.  
  40. this.$spinner = $('<div class="spinner hidden"/>').appendTo($buttonsOuterContainer);
  41.  
  42. var $buttonsContainer = $('<div class="buttons right"/>').appendTo($buttonsOuterContainer);
  43. $cancelBtn = $('<div class="btn">' + Craft.t('field-manager', 'Cancel') + '</div>').appendTo($buttonsContainer);
  44. $saveBtn = $('<input class="btn submit" type="submit" value="' + Craft.t('field-manager', 'Clone') + '"/>').appendTo($buttonsContainer);
  45.  
  46. $hudContents = $hudContents.add(this.$form);
  47.  
  48. this.hud = new Garnish.HUD(this.$element, $hudContents, {
  49. bodyClass: 'body',
  50. closeOtherHUDs: false
  51. });
  52.  
  53. this.hud.on('hide', $.proxy(function() {
  54. delete this.hud;
  55. }, this));
  56.  
  57. this.addListener($saveBtn, 'activate', 'saveGroupField');
  58. this.addListener($cancelBtn, 'activate', 'closeHud');
  59.  
  60. // new Craft.FieldManager.HandleGeneratorWithSuffix('#name', '#prefix');
  61. new Craft.HandleGenerator('#name', '#prefix');
  62. }
  63. },
  64.  
  65. saveGroupField: function(ev) {
  66. ev.preventDefault();
  67.  
  68. this.$spinner.removeClass('hidden');
  69.  
  70. var data = this.$form.serialize();
  71.  
  72. Craft.postActionRequest('field-manager/base/clone-group', data, $.proxy(function(response, textStatus) {
  73. this.$spinner.addClass('hidden');
  74.  
  75. if (response.error) {
  76. Garnish.shake(this.hud.$hud);
  77.  
  78. $.each(response.error, function(index, value) {
  79. Craft.cp.displayError(value);
  80. });
  81. } else if (response.success) {
  82. Craft.cp.displayNotice(Craft.t('field-manager', 'Group cloned.'));
  83. location.href = Craft.getUrl('field-manager');
  84.  
  85. this.onFadeOut();
  86. } else {
  87. Craft.cp.displayError(Craft.t('field-manager', 'Could not clone group'));
  88. }
  89. }, this));
  90. },
  91.  
  92. closeHud: function() {
  93. this.hud.hide();
  94. delete this.hud;
  95. }
  96. });
  97.  
  98.  
  99.  
  100.  
  101. Craft.FieldManager.EditGroup = Garnish.Base.extend({
  102. $element: null,
  103. groupId: null,
  104.  
  105. $form: null,
  106. $spinner: null,
  107.  
  108. hud: null,
  109.  
  110. init: function($element, $data) {
  111. this.$element = $element;
  112. this.groupId = $data.data('groupid');
  113.  
  114. this.$element.addClass('loading');
  115.  
  116. var data = {
  117. groupId: this.groupId,
  118. };
  119.  
  120. Craft.postActionRequest('field-manager/base/get-group-modal-body', data, $.proxy(this, 'showHud'));
  121. },
  122.  
  123. showHud: function(response, textStatus) {
  124. this.$element.removeClass('loading');
  125.  
  126. if (response.success) {
  127. var $hudContents = $();
  128.  
  129. this.$form = $('<form/>');
  130. $('<input type="hidden" name="groupId" value="' + this.groupId + '">').appendTo(this.$form);
  131. $fieldsContainer = $('<div class="fields"/>').appendTo(this.$form);
  132.  
  133. $fieldsContainer.html(response.modalHtml);
  134. Craft.initUiElements($fieldsContainer);
  135.  
  136. var $buttonsOuterContainer = $('<div class="footer"/>').appendTo(this.$form);
  137.  
  138. this.$spinner = $('<div class="spinner hidden"/>').appendTo($buttonsOuterContainer);
  139.  
  140. var $buttonsContainer = $('<div class="buttons right"/>').appendTo($buttonsOuterContainer);
  141. $cancelBtn = $('<div class="btn">' + Craft.t('field-manager', 'Cancel') + '</div>').appendTo($buttonsContainer);
  142. $saveBtn = $('<input class="btn submit" type="submit" value="' + Craft.t('field-manager', 'Save') + '"/>').appendTo($buttonsContainer);
  143.  
  144. $hudContents = $hudContents.add(this.$form);
  145.  
  146. this.hud = new Garnish.HUD(this.$element, $hudContents, {
  147. bodyClass: 'body',
  148. closeOtherHUDs: false
  149. });
  150.  
  151. this.hud.on('hide', $.proxy(function() {
  152. delete this.hud;
  153. }, this));
  154.  
  155. this.addListener($saveBtn, 'activate', 'saveGroupField');
  156. this.addListener($cancelBtn, 'activate', 'closeHud');
  157. }
  158. },
  159.  
  160. saveGroupField: function(ev) {
  161. ev.preventDefault();
  162.  
  163. this.$spinner.removeClass('hidden');
  164.  
  165. var data = this.$form.serialize();
  166.  
  167. Craft.postActionRequest('fields/save-group', data, $.proxy(function(response, textStatus) {
  168. this.$spinner.addClass('hidden');
  169.  
  170. if (textStatus == 'success' && response.success) {
  171. location.href = Craft.getUrl('field-manager');
  172.  
  173. Craft.cp.displayNotice(Craft.t('field-manager', 'Field group updated.'));
  174.  
  175. this.closeHud();
  176. } else {
  177. Garnish.shake(this.hud.$hud);
  178. }
  179. }, this));
  180. },
  181.  
  182. closeHud: function() {
  183. this.hud.hide();
  184. delete this.hud;
  185. }
  186. });
  187.  
  188.  
  189.  
  190. Craft.FieldManager.CloneField = Garnish.Modal.extend({
  191. fieldId: null,
  192. groupId: null,
  193.  
  194. $body: null,
  195. $element: null,
  196. $buttons: null,
  197. $cancelBtn: null,
  198. $saveBtn: null,
  199. $footerSpinner: null,
  200.  
  201. init: function($element, $data) {
  202. this.$element = $element;
  203. this.fieldId = $data.data('id');
  204. this.groupId = $data.data('groupid');
  205.  
  206. // Build the modal
  207. var $container = $('<div class="modal fieldsettingsmodal"></div>').appendTo(Garnish.$bod),
  208. $body = $('<div class="body"><div class="spinner big"></div></div>').appendTo($container),
  209. $footer = $('<div class="footer"/>').appendTo($container);
  210.  
  211. this.base($container, this.settings);
  212.  
  213. this.$footerSpinner = $('<div class="spinner hidden"/>').appendTo($footer);
  214. this.$buttons = $('<div class="buttons rightalign first"/>').appendTo($footer);
  215. this.$cancelBtn = $('<div class="btn">'+Craft.t('field-manager', 'Cancel')+'</div>').appendTo(this.$buttons);
  216.  
  217. if (!this.fieldId) {
  218. this.$saveBtn = $('<div class="btn submit">'+Craft.t('field-manager', 'Add field')+'</div>').appendTo(this.$buttons);
  219. } else {
  220. this.$saveBtn = $('<div class="btn submit">'+Craft.t('field-manager', 'Clone')+'</div>').appendTo(this.$buttons);
  221. }
  222.  
  223. this.$body = $body;
  224.  
  225. this.addListener(this.$cancelBtn, 'activate', 'onFadeOut');
  226. this.addListener(this.$saveBtn, 'activate', 'saveSettings');
  227. },
  228.  
  229. onFadeIn: function() {
  230. var data = {
  231. fieldId: this.fieldId,
  232. groupId: this.groupId,
  233. };
  234.  
  235. Craft.postActionRequest('field-manager/base/get-field-modal-body', data, $.proxy(function(response, textStatus) {
  236. if (response.success) {
  237. this.$body.html(response.modalHtml);
  238.  
  239. Craft.initUiElements(this.$body);
  240.  
  241. new Craft.HandleGenerator('#name', '#handle');
  242. }
  243. }, this));
  244.  
  245. this.base();
  246. },
  247.  
  248. onFadeOut: function() {
  249. this.hide();
  250. this.destroy();
  251. this.$shade.remove();
  252. this.$container.remove();
  253.  
  254. this.removeListener(this.$saveBtn, 'click');
  255. this.removeListener(this.$cancelBtn, 'click');
  256. },
  257.  
  258. saveSettings: function() {
  259. var params = this.$body.find('form').serializeObject();
  260. params.fieldId = this.fieldId;
  261.  
  262. this.$footerSpinner.removeClass('hidden');
  263.  
  264. Craft.postActionRequest('field-manager/base/clone-field', params, $.proxy(function(response, textStatus) {
  265. this.$footerSpinner.addClass('hidden');
  266.  
  267. if (response.error) {
  268. $.each(response.error, function(index, value) {
  269. Craft.cp.displayError(value);
  270. });
  271. } else if (response.success) {
  272. Craft.cp.displayNotice(Craft.t('field-manager', 'Field cloned.'));
  273. location.href = Craft.getUrl('field-manager');
  274.  
  275. this.onFadeOut();
  276. } else {
  277. Craft.cp.displayError(Craft.t('field-manager', 'Could not clone field'));
  278. }
  279.  
  280. }, this));
  281.  
  282. this.removeListener(this.$saveBtn, 'click');
  283. this.removeListener(this.$cancelBtn, 'click');
  284. },
  285.  
  286. show: function() {
  287. this.base();
  288. }
  289. });
  290.  
  291.  
  292.  
  293. Craft.FieldManager.EditField = Garnish.Modal.extend({
  294. fieldId: null,
  295. groupId: null,
  296.  
  297. $body: null,
  298. $element: null,
  299. $buttons: null,
  300. $cancelBtn: null,
  301. $saveBtn: null,
  302. $footerSpinner: null,
  303.  
  304. init: function($element, $data) {
  305. this.$element = $element;
  306.  
  307. if ($data) {
  308. this.fieldId = $data.data('id');
  309. this.groupId = $data.data('groupid');
  310. }
  311.  
  312. // Build the modal
  313. var $container = $('<div class="modal fieldsettingsmodal"></div>').appendTo(Garnish.$bod),
  314. $body = $('<div class="body"><div class="spinner big"></div></div>').appendTo($container),
  315. $footer = $('<div class="footer"/>').appendTo($container);
  316.  
  317. this.base($container, this.settings);
  318.  
  319. this.$footerSpinner = $('<div class="spinner hidden"/>').appendTo($footer);
  320. this.$buttons = $('<div class="buttons rightalign first"/>').appendTo($footer);
  321. this.$cancelBtn = $('<div class="btn">' + Craft.t('field-manager', 'Cancel') + '</div>').appendTo(this.$buttons);
  322. this.$saveBtn = $('<div class="btn submit">' + Craft.t('field-manager', 'Save') + '</div>').appendTo(this.$buttons);
  323.  
  324. this.$body = $body;
  325.  
  326. this.addListener(this.$cancelBtn, 'activate', 'onFadeOut');
  327. this.addListener(this.$saveBtn, 'activate', 'saveSettings');
  328. },
  329.  
  330. onFadeIn: function() {
  331. var data = {
  332. fieldId: this.fieldId,
  333. groupId: this.groupId,
  334. };
  335.  
  336. Craft.postActionRequest('field-manager/base/get-field-modal-body', data, $.proxy(function(response, textStatus) {
  337. if (response.success) {
  338. this.$body.html(response.modalHtml);
  339. Garnish.$bod.append(response.modalJs);
  340.  
  341. Craft.initUiElements(this.$body);
  342.  
  343. new Craft.HandleGenerator('#name', '#handle');
  344. }
  345. }, this));
  346.  
  347. this.base();
  348. },
  349.  
  350. onFadeOut: function() {
  351. this.hide();
  352. this.destroy();
  353. this.$shade.remove();
  354. this.$container.remove();
  355.  
  356. this.removeListener(this.$saveBtn, 'click');
  357. this.removeListener(this.$cancelBtn, 'click');
  358. },
  359.  
  360. saveSettings: function() {
  361. var params = this.$body.find('form').serialize();
  362.  
  363. this.$footerSpinner.removeClass('hidden');
  364.  
  365. Craft.postActionRequest('field-manager/base/save-field', params, $.proxy(function(response, textStatus) {
  366. this.$footerSpinner.addClass('hidden');
  367.  
  368. if (response.error) {
  369. Garnish.shake(this.$container);
  370.  
  371. $.each(response.error, function(index, value) {
  372. Craft.cp.displayError(value);
  373. });
  374. } else if (response.success) {
  375. Craft.cp.displayNotice(Craft.t('field-manager', 'Field updated.'));
  376. location.href = Craft.getUrl('field-manager');
  377.  
  378. this.onFadeOut();
  379. } else {
  380. Garnish.shake(this.$container);
  381. Craft.cp.displayError(Craft.t('field-manager', 'Could not update field'));
  382. }
  383.  
  384. }, this));
  385.  
  386. this.removeListener(this.$saveBtn, 'click');
  387. this.removeListener(this.$cancelBtn, 'click');
  388. },
  389.  
  390. show: function() {
  391. this.base();
  392. }
  393. });
  394.  
  395. Craft.updateTranslationMethodSettings = function(type, container) {
  396. var $container = $(container);
  397. if (!Craft.supportedTranslationMethods[type] || Craft.supportedTranslationMethods[type].length == 1) {
  398. $container.addClass('hidden');
  399. } else {
  400. $container.removeClass('hidden');
  401. // Rebuild the options based on the field type's supported translation methods
  402. $container.find('select').html(
  403. ($.inArray('none', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="none">{{ "Not translatable"|t('app')|e('js') }}</option>' : '') +
  404. ($.inArray('language', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="language">{{ "Translate for each language"|t('app')|e('js') }}</option>' : '') +
  405. ($.inArray('site', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="site">{{ "Translate for each site"|t('app')|e('js') }}</option>' : '') +
  406. ($.inArray('custom', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="custom">{{ "Custom…"|t('app')|e('js') }}</option>' : '')
  407. );
  408. }
  409. }
  410.  
  411. var $fieldTypeInput = $("#{{ 'type'|namespaceInputId|e('js') }}"),
  412. $translationSettings = $("#{{ 'translation-settings'|namespaceInputId|e('js') }}");
  413.  
  414. $fieldTypeInput.change(function(e) {
  415. Craft.updateTranslationMethodSettings($(this).val(), $translationSettings);
  416. });
Add Comment
Please, Sign In to add comment